ResponseTests.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // ResponseTests.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (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 Alamofire
  23. import Foundation
  24. import XCTest
  25. class JSONResponseTestCase: BaseTestCase {
  26. func testGETRequestJSONResponse() {
  27. // Given
  28. let URLString = "http://httpbin.org/get"
  29. let expectation = expectationWithDescription("\(URLString)")
  30. var request: NSURLRequest?
  31. var response: NSHTTPURLResponse?
  32. var JSON: AnyObject?
  33. var error: NSError?
  34. // When
  35. Alamofire.request(.GET, URLString: URLString, parameters: ["foo": "bar"])
  36. .responseJSON { responseRequest, responseResponse, responseJSON, responseError in
  37. request = responseRequest
  38. response = responseResponse
  39. JSON = responseJSON
  40. error = responseError
  41. expectation.fulfill()
  42. }
  43. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  44. // Then
  45. XCTAssertNotNil(request, "request should not be nil")
  46. XCTAssertNotNil(response, "response should not be nil")
  47. XCTAssertNotNil(JSON, "JSON should not be nil")
  48. XCTAssertNil(error, "error should be nil")
  49. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  50. // - http://openradar.appspot.com/radar?id=5517037090635776
  51. if let args = JSON?["args" as NSString] as? [String: String] {
  52. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  53. } else {
  54. XCTFail("args should not be nil")
  55. }
  56. }
  57. func testPOSTRequestJSONResponse() {
  58. // Given
  59. let URLString = "http://httpbin.org/post"
  60. let expectation = expectationWithDescription("\(URLString)")
  61. var request: NSURLRequest?
  62. var response: NSHTTPURLResponse?
  63. var JSON: AnyObject?
  64. var error: NSError?
  65. // When
  66. Alamofire.request(.POST, URLString: URLString, parameters: ["foo": "bar"])
  67. .responseJSON { responseRequest, responseResponse, responseJSON, responseError in
  68. request = responseRequest
  69. response = responseResponse
  70. JSON = responseJSON
  71. error = responseError
  72. expectation.fulfill()
  73. }
  74. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  75. // Then
  76. XCTAssertNotNil(request, "request should not be nil")
  77. XCTAssertNotNil(response, "response should not be nil")
  78. XCTAssertNotNil(JSON, "JSON should not be nil")
  79. XCTAssertNil(error, "error should be nil")
  80. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  81. // - http://openradar.appspot.com/radar?id=5517037090635776
  82. if let form = JSON?["form" as NSString] as? [String: String] {
  83. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  84. } else {
  85. XCTFail("form should not be nil")
  86. }
  87. }
  88. }
  89. // MARK: -
  90. class RedirectResponseTestCase: BaseTestCase {
  91. func testThatRequestWillPerformHTTPRedirectionByDefault() {
  92. // Given
  93. let redirectURLString = "http://www.apple.com"
  94. let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"
  95. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  96. var request: NSURLRequest?
  97. var response: NSHTTPURLResponse?
  98. var data: AnyObject?
  99. var error: NSError?
  100. // When
  101. Alamofire.request(.GET, URLString: URLString)
  102. .response { responseRequest, responseResponse, responseData, responseError in
  103. request = responseRequest
  104. response = responseResponse
  105. data = responseData
  106. error = responseError
  107. expectation.fulfill()
  108. }
  109. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  110. // Then
  111. XCTAssertNotNil(request, "request should not be nil")
  112. XCTAssertNotNil(response, "response should not be nil")
  113. XCTAssertNotNil(data, "data should not be nil")
  114. XCTAssertNil(error, "error should be nil")
  115. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  116. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  117. }
  118. func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
  119. // Given
  120. let redirectURLString = "http://httpbin.org/get"
  121. let URLString = "http://httpbin.org/redirect/5"
  122. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  123. var request: NSURLRequest?
  124. var response: NSHTTPURLResponse?
  125. var data: AnyObject?
  126. var error: NSError?
  127. // When
  128. Alamofire.request(.GET, URLString: URLString)
  129. .response { responseRequest, responseResponse, responseData, responseError in
  130. request = responseRequest
  131. response = responseResponse
  132. data = responseData
  133. error = responseError
  134. expectation.fulfill()
  135. }
  136. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  137. // Then
  138. XCTAssertNotNil(request, "request should not be nil")
  139. XCTAssertNotNil(response, "response should not be nil")
  140. XCTAssertNotNil(data, "data should not be nil")
  141. XCTAssertNil(error, "error should be nil")
  142. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  143. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  144. }
  145. func testThatTaskOverrideClosureCanPerformHTTPRedirection() {
  146. // Given
  147. let redirectURLString = "http://www.apple.com"
  148. let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"
  149. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  150. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  151. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  152. return request
  153. }
  154. var request: NSURLRequest?
  155. var response: NSHTTPURLResponse?
  156. var data: AnyObject?
  157. var error: NSError?
  158. // When
  159. Alamofire.request(.GET, URLString: URLString)
  160. .response { responseRequest, responseResponse, responseData, responseError in
  161. request = responseRequest
  162. response = responseResponse
  163. data = responseData
  164. error = responseError
  165. expectation.fulfill()
  166. }
  167. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  168. // Then
  169. XCTAssertNotNil(request, "request should not be nil")
  170. XCTAssertNotNil(response, "response should not be nil")
  171. XCTAssertNotNil(data, "data should not be nil")
  172. XCTAssertNil(error, "error should be nil")
  173. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  174. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  175. }
  176. func testThatTaskOverrideClosureCanCancelHTTPRedirection() {
  177. // Given
  178. let redirectURLString = "http://www.apple.com"
  179. let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"
  180. let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
  181. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  182. delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in
  183. return nil
  184. }
  185. var request: NSURLRequest?
  186. var response: NSHTTPURLResponse?
  187. var data: AnyObject?
  188. var error: NSError?
  189. // When
  190. Alamofire.request(.GET, URLString: URLString)
  191. .response { responseRequest, responseResponse, responseData, responseError in
  192. request = responseRequest
  193. response = responseResponse
  194. data = responseData
  195. error = responseError
  196. expectation.fulfill()
  197. }
  198. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  199. // Then
  200. XCTAssertNotNil(request, "request should not be nil")
  201. XCTAssertNotNil(response, "response should not be nil")
  202. XCTAssertNotNil(data, "data should not be nil")
  203. XCTAssertNil(error, "error should be nil")
  204. XCTAssertEqual(response?.URL?.URLString ?? "", URLString, "response URL should match the origin URL")
  205. XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
  206. }
  207. func testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects() {
  208. // Given
  209. let redirectURLString = "http://httpbin.org/get"
  210. let URLString = "http://httpbin.org/redirect/5"
  211. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  212. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  213. var totalRedirectCount = 0
  214. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  215. ++totalRedirectCount
  216. return request
  217. }
  218. var request: NSURLRequest?
  219. var response: NSHTTPURLResponse?
  220. var data: AnyObject?
  221. var error: NSError?
  222. // When
  223. Alamofire.request(.GET, URLString: URLString)
  224. .response { responseRequest, responseResponse, responseData, responseError in
  225. request = responseRequest
  226. response = responseResponse
  227. data = responseData
  228. error = responseError
  229. expectation.fulfill()
  230. }
  231. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  232. // Then
  233. XCTAssertNotNil(request, "request should not be nil")
  234. XCTAssertNotNil(response, "response should not be nil")
  235. XCTAssertNotNil(data, "data should not be nil")
  236. XCTAssertNil(error, "error should be nil")
  237. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  238. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  239. XCTAssertEqual(totalRedirectCount, 5, "total redirect count should be 5")
  240. }
  241. }