RequestTests.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. let manager = Alamofire.Manager(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
  77. // MARK: -
  78. override func setUp() {
  79. manager.startRequestsImmediately = false
  80. if let cookieStorage = manager.session.configuration.HTTPCookieStorage {
  81. for cookie in cookieStorage.cookies ?? [] {
  82. cookieStorage.deleteCookie(cookie as NSHTTPCookie)
  83. }
  84. }
  85. }
  86. // MARK: -
  87. func testGETRequestDebugDescription() {
  88. let URL = "http://httpbin.org/get"
  89. let request = manager.request(.GET, URL)
  90. let components = cURLCommandComponents(request)
  91. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  92. XCTAssert(!contains(components, "-X"), "command should not contain explicit -X flag")
  93. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  94. }
  95. func testPOSTRequestDebugDescription() {
  96. let URL = "http://httpbin.org/post"
  97. let request = manager.request(.POST, URL)
  98. let components = cURLCommandComponents(request)
  99. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  100. XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag")
  101. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  102. }
  103. func testPOSTRequestWithJSONParametersDebugDescription() {
  104. let URL = "http://httpbin.org/post"
  105. let request = manager.request(.POST, URL, parameters: ["foo": "bar"], encoding: .JSON)
  106. let components = cURLCommandComponents(request)
  107. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  108. XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag")
  109. XCTAssert(request.debugDescription.rangeOfString("-H \"Content-Type: application/json\"") != nil)
  110. XCTAssert(request.debugDescription.rangeOfString("-d \"{\\\"foo\\\":\\\"bar\\\"}\"") != nil)
  111. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  112. }
  113. func testPOSTRequestWithCookieDebugDescription() {
  114. let URL = "http://httpbin.org/post"
  115. let properties = [
  116. NSHTTPCookieDomain: "httpbin.org",
  117. NSHTTPCookiePath: "/post",
  118. NSHTTPCookieName: "foo",
  119. NSHTTPCookieValue: "bar",
  120. ]
  121. let cookie = NSHTTPCookie(properties: properties)!
  122. manager.session.configuration.HTTPCookieStorage?.setCookie(cookie)
  123. let request = manager.request(.POST, URL)
  124. let components = cURLCommandComponents(request)
  125. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  126. XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag")
  127. XCTAssert(components[5..<7] == ["-b", "\"foo=bar\""], "command should contain -b flag")
  128. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  129. }
  130. // MARK: -
  131. private func cURLCommandComponents(request: Request) -> [String] {
  132. return request.debugDescription.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).filter { $0 != "" && $0 != "\\" }
  133. }
  134. }