RequestTests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // RequestTests.swift
  2. //
  3. // Copyright (c) 2014 Alamofire (http://alamofire.org)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. import Alamofire
  24. import XCTest
  25. class AlamofireRequestInitializationTestCase: XCTestCase {
  26. func testRequestClassMethodWithMethodAndURL() {
  27. let URL = "http://httpbin.org/"
  28. let request = Alamofire.request(.GET, URL)
  29. XCTAssertNotNil(request.request, "request should not be nil")
  30. XCTAssertEqual(request.request.URL, NSURL(string: URL), "request URL should be equal")
  31. XCTAssertNil(request.response, "response should be nil")
  32. }
  33. func testRequestClassMethodWithMethodAndURLAndParameters() {
  34. let URL = "http://httpbin.org/get"
  35. let request = Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  36. XCTAssertNotNil(request.request, "request should not be nil")
  37. XCTAssertNotEqual(request.request.URL, NSURL(string: URL), "request URL should be equal")
  38. XCTAssertEqual(request.request.URL.query!, "foo=bar", "query is incorrect")
  39. XCTAssertNil(request.response, "response should be nil")
  40. }
  41. }
  42. class AlamofireRequestResponseTestCase: XCTestCase {
  43. func testRequestResponse() {
  44. let URL = "http://httpbin.org/get"
  45. let serializer = Alamofire.Request.stringResponseSerializer(encoding: NSUTF8StringEncoding)
  46. let expectation = expectationWithDescription("\(URL)")
  47. Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  48. .response(serializer: serializer){ (request, response, string, error) in
  49. expectation.fulfill()
  50. XCTAssertNotNil(request, "request should not be nil")
  51. XCTAssertNotNil(response, "response should not be nil")
  52. XCTAssertNotNil(string, "string should not be nil")
  53. XCTAssertNil(error, "error should be nil")
  54. }
  55. waitForExpectationsWithTimeout(10) { (error) in
  56. XCTAssertNil(error, "\(error)")
  57. }
  58. }
  59. }
  60. class AlamofireRequestDescriptionTestCase: XCTestCase {
  61. func testRequestDescription() {
  62. let URL = "http://httpbin.org/get"
  63. let request = Alamofire.request(.GET, URL)
  64. XCTAssertEqual(request.description, "GET http://httpbin.org/get", "incorrect request description")
  65. let expectation = expectationWithDescription("\(URL)")
  66. request.response { (_, response,_,_) in
  67. expectation.fulfill()
  68. XCTAssertEqual(request.description, "GET http://httpbin.org/get (\(response!.statusCode))", "incorrect request description")
  69. }
  70. waitForExpectationsWithTimeout(10) { (error) in
  71. XCTAssertNil(error, "\(error)")
  72. }
  73. }
  74. }
  75. class AlamofireRequestDebugDescriptionTestCase: XCTestCase {
  76. private func cURLCommandComponents(request: Request) -> [String] {
  77. return request.debugDescription.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).filter { $0 != "" && $0 != "\\" }
  78. }
  79. // MARK: -
  80. func testGETRequestDebugDescription() {
  81. let URL = "http://httpbin.org/get"
  82. let request = Alamofire.request(.GET, URL)
  83. let components = cURLCommandComponents(request)
  84. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"])
  85. XCTAssert(!contains(components, "-X"), "command should not contain explicit -X flag")
  86. XCTAssertEqual(components.last!, "\"\(URL)\"")
  87. }
  88. func testPOSTRequestDebugDescription() {
  89. let URL = "http://httpbin.org/post"
  90. let request = Alamofire.request(.POST, URL)
  91. let components = cURLCommandComponents(request)
  92. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"])
  93. XCTAssertEqual(components[3..<5], ["-X", "POST"])
  94. XCTAssertEqual(components.last!, "\"\(URL)\"")
  95. }
  96. func testPOSTRequestWithJSONParametersDebugDescription() {
  97. let URL = "http://httpbin.org/post"
  98. let request = Alamofire.request(.POST, URL, parameters: ["foo": "bar"], encoding: .JSON)
  99. let components = cURLCommandComponents(request)
  100. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"])
  101. XCTAssertEqual(components[3..<5], ["-X", "POST"])
  102. XCTAssert(request.debugDescription.rangeOfString("-H \"Content-Type: application/json\"") != nil)
  103. XCTAssert(request.debugDescription.rangeOfString("-d \"{\"foo\":\"bar\"}\"") != nil)
  104. XCTAssertEqual(components.last!, "\"\(URL)\"")
  105. }
  106. }