ResponseTests.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 ResponseDataTestCase: BaseTestCase {
  26. func testThatResponseDataReturnsSuccessResultWithValidData() {
  27. // Given
  28. let URLString = "https://httpbin.org/get"
  29. let expectation = expectationWithDescription("request should succeed")
  30. var response: Response<NSData, NSError>?
  31. // When
  32. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  33. .responseData { closureResponse in
  34. response = closureResponse
  35. expectation.fulfill()
  36. }
  37. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  38. // Then
  39. if let response = response {
  40. XCTAssertNotNil(response.request, "request should not be nil")
  41. XCTAssertNotNil(response.response, "response should not be nil")
  42. XCTAssertNotNil(response.data, "data should not be nil")
  43. XCTAssertTrue(response.result.isSuccess, "result should be success")
  44. } else {
  45. XCTFail("response should not be nil")
  46. }
  47. }
  48. func testThatResponseDataReturnsFailureResultWithOptionalDataAndError() {
  49. // Given
  50. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  51. let expectation = expectationWithDescription("request should fail with 404")
  52. var response: Response<NSData, NSError>?
  53. // When
  54. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  55. .responseData { closureResponse in
  56. response = closureResponse
  57. expectation.fulfill()
  58. }
  59. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  60. // Then
  61. if let response = response {
  62. XCTAssertNotNil(response.request, "request should not be nil")
  63. XCTAssertNil(response.response, "response should be nil")
  64. XCTAssertNotNil(response.data, "data should not be nil")
  65. XCTAssertTrue(response.result.isFailure, "result should be failure")
  66. } else {
  67. XCTFail("response should not be nil")
  68. }
  69. }
  70. }
  71. // MARK: -
  72. class ResponseStringTestCase: BaseTestCase {
  73. func testThatResponseStringReturnsSuccessResultWithValidString() {
  74. // Given
  75. let URLString = "https://httpbin.org/get"
  76. let expectation = expectationWithDescription("request should succeed")
  77. var response: Response<String, NSError>?
  78. // When
  79. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  80. .responseString { closureResponse in
  81. response = closureResponse
  82. expectation.fulfill()
  83. }
  84. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  85. // Then
  86. if let response = response {
  87. XCTAssertNotNil(response.request, "request should not be nil")
  88. XCTAssertNotNil(response.response, "response should not be nil")
  89. XCTAssertNotNil(response.data, "data should not be nil")
  90. XCTAssertTrue(response.result.isSuccess, "result should be success")
  91. } else {
  92. XCTFail("response should not be nil")
  93. }
  94. }
  95. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  96. // Given
  97. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  98. let expectation = expectationWithDescription("request should fail with 404")
  99. var response: Response<String, NSError>?
  100. // When
  101. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  102. .responseString { closureResponse in
  103. response = closureResponse
  104. expectation.fulfill()
  105. }
  106. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  107. // Then
  108. if let response = response {
  109. XCTAssertNotNil(response.request, "request should not be nil")
  110. XCTAssertNil(response.response, "response should be nil")
  111. XCTAssertNotNil(response.data, "data should not be nil")
  112. XCTAssertTrue(response.result.isFailure, "result should be failure")
  113. } else {
  114. XCTFail("response should not be nil")
  115. }
  116. }
  117. }
  118. // MARK: -
  119. class ResponseJSONTestCase: BaseTestCase {
  120. func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
  121. // Given
  122. let URLString = "https://httpbin.org/get"
  123. let expectation = expectationWithDescription("request should succeed")
  124. var response: Response<AnyObject, NSError>?
  125. // When
  126. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  127. .responseJSON { closureResponse in
  128. response = closureResponse
  129. expectation.fulfill()
  130. }
  131. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  132. // Then
  133. if let response = response {
  134. XCTAssertNotNil(response.request, "request should not be nil")
  135. XCTAssertNotNil(response.response, "response should not be nil")
  136. XCTAssertNotNil(response.data, "data should not be nil")
  137. XCTAssertTrue(response.result.isSuccess, "result should be success")
  138. } else {
  139. XCTFail("response should not be nil")
  140. }
  141. }
  142. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  143. // Given
  144. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  145. let expectation = expectationWithDescription("request should fail with 404")
  146. var response: Response<AnyObject, NSError>?
  147. // When
  148. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  149. .responseJSON { closureResponse in
  150. response = closureResponse
  151. expectation.fulfill()
  152. }
  153. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  154. // Then
  155. if let response = response {
  156. XCTAssertNotNil(response.request, "request should not be nil")
  157. XCTAssertNil(response.response, "response should be nil")
  158. XCTAssertNotNil(response.data, "data should not be nil")
  159. XCTAssertTrue(response.result.isFailure, "result should be failure")
  160. } else {
  161. XCTFail("response should not be nil")
  162. }
  163. }
  164. func testThatResponseJSONReturnsSuccessResultForGETRequest() {
  165. // Given
  166. let URLString = "https://httpbin.org/get"
  167. let expectation = expectationWithDescription("request should succeed")
  168. var response: Response<AnyObject, NSError>?
  169. // When
  170. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  171. .responseJSON { closureResponse in
  172. response = closureResponse
  173. expectation.fulfill()
  174. }
  175. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  176. // Then
  177. if let response = response {
  178. XCTAssertNotNil(response.request, "request should not be nil")
  179. XCTAssertNotNil(response.response, "response should not be nil")
  180. XCTAssertNotNil(response.data, "data should not be nil")
  181. XCTAssertTrue(response.result.isSuccess, "result should be success")
  182. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  183. // - https://openradar.appspot.com/radar?id=5517037090635776
  184. if let args = response.result.value?["args" as NSString] as? [String: String] {
  185. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  186. } else {
  187. XCTFail("args should not be nil")
  188. }
  189. } else {
  190. XCTFail("response should not be nil")
  191. }
  192. }
  193. func testThatResponseJSONReturnsSuccessResultForPOSTRequest() {
  194. // Given
  195. let URLString = "https://httpbin.org/post"
  196. let expectation = expectationWithDescription("request should succeed")
  197. var response: Response<AnyObject, NSError>?
  198. // When
  199. Alamofire.request(.POST, URLString, parameters: ["foo": "bar"])
  200. .responseJSON { closureResponse in
  201. response = closureResponse
  202. expectation.fulfill()
  203. }
  204. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  205. // Then
  206. if let response = response {
  207. XCTAssertNotNil(response.request, "request should not be nil")
  208. XCTAssertNotNil(response.response, "response should not be nil")
  209. XCTAssertNotNil(response.data, "data should not be nil")
  210. XCTAssertTrue(response.result.isSuccess, "result should be success")
  211. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  212. // - https://openradar.appspot.com/radar?id=5517037090635776
  213. if let form = response.result.value?["form" as NSString] as? [String: String] {
  214. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  215. } else {
  216. XCTFail("form should not be nil")
  217. }
  218. } else {
  219. XCTFail("response should not be nil")
  220. }
  221. }
  222. }
  223. // MARK: -
  224. class RedirectResponseTestCase: BaseTestCase {
  225. func testThatRequestWillPerformHTTPRedirectionByDefault() {
  226. // Given
  227. let redirectURLString = "https://www.apple.com"
  228. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  229. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  230. var request: NSURLRequest?
  231. var response: NSHTTPURLResponse?
  232. var data: NSData?
  233. var error: NSError?
  234. // When
  235. Alamofire.request(.GET, URLString)
  236. .response { responseRequest, responseResponse, responseData, responseError in
  237. request = responseRequest
  238. response = responseResponse
  239. data = responseData
  240. error = responseError
  241. expectation.fulfill()
  242. }
  243. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  244. // Then
  245. XCTAssertNotNil(request, "request should not be nil")
  246. XCTAssertNotNil(response, "response should not be nil")
  247. XCTAssertNotNil(data, "data should not be nil")
  248. XCTAssertNil(error, "error should be nil")
  249. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  250. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  251. }
  252. func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
  253. // Given
  254. let redirectURLString = "https://httpbin.org/get"
  255. let URLString = "https://httpbin.org/redirect/5"
  256. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  257. var request: NSURLRequest?
  258. var response: NSHTTPURLResponse?
  259. var data: NSData?
  260. var error: NSError?
  261. // When
  262. Alamofire.request(.GET, URLString)
  263. .response { responseRequest, responseResponse, responseData, responseError in
  264. request = responseRequest
  265. response = responseResponse
  266. data = responseData
  267. error = responseError
  268. expectation.fulfill()
  269. }
  270. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  271. // Then
  272. XCTAssertNotNil(request, "request should not be nil")
  273. XCTAssertNotNil(response, "response should not be nil")
  274. XCTAssertNotNil(data, "data should not be nil")
  275. XCTAssertNil(error, "error should be nil")
  276. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  277. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  278. }
  279. func testThatTaskOverrideClosureCanPerformHTTPRedirection() {
  280. // Given
  281. let redirectURLString = "https://www.apple.com"
  282. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  283. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  284. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  285. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  286. return request
  287. }
  288. var request: NSURLRequest?
  289. var response: NSHTTPURLResponse?
  290. var data: NSData?
  291. var error: NSError?
  292. // When
  293. Alamofire.request(.GET, URLString)
  294. .response { responseRequest, responseResponse, responseData, responseError in
  295. request = responseRequest
  296. response = responseResponse
  297. data = responseData
  298. error = responseError
  299. expectation.fulfill()
  300. }
  301. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  302. // Then
  303. XCTAssertNotNil(request, "request should not be nil")
  304. XCTAssertNotNil(response, "response should not be nil")
  305. XCTAssertNotNil(data, "data should not be nil")
  306. XCTAssertNil(error, "error should be nil")
  307. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  308. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  309. }
  310. func testThatTaskOverrideClosureCanCancelHTTPRedirection() {
  311. // Given
  312. let redirectURLString = "https://www.apple.com"
  313. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  314. let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
  315. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  316. delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in
  317. return nil
  318. }
  319. var request: NSURLRequest?
  320. var response: NSHTTPURLResponse?
  321. var data: NSData?
  322. var error: NSError?
  323. // When
  324. Alamofire.request(.GET, URLString)
  325. .response { responseRequest, responseResponse, responseData, responseError in
  326. request = responseRequest
  327. response = responseResponse
  328. data = responseData
  329. error = responseError
  330. expectation.fulfill()
  331. }
  332. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  333. // Then
  334. XCTAssertNotNil(request, "request should not be nil")
  335. XCTAssertNotNil(response, "response should not be nil")
  336. XCTAssertNotNil(data, "data should not be nil")
  337. XCTAssertNil(error, "error should be nil")
  338. XCTAssertEqual(response?.URL?.URLString ?? "", URLString, "response URL should match the origin URL")
  339. XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
  340. }
  341. func testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects() {
  342. // Given
  343. let redirectURLString = "https://httpbin.org/get"
  344. let URLString = "https://httpbin.org/redirect/5"
  345. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  346. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  347. var totalRedirectCount = 0
  348. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  349. ++totalRedirectCount
  350. return request
  351. }
  352. var request: NSURLRequest?
  353. var response: NSHTTPURLResponse?
  354. var data: NSData?
  355. var error: NSError?
  356. // When
  357. Alamofire.request(.GET, URLString)
  358. .response { responseRequest, responseResponse, responseData, responseError in
  359. request = responseRequest
  360. response = responseResponse
  361. data = responseData
  362. error = responseError
  363. expectation.fulfill()
  364. }
  365. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  366. // Then
  367. XCTAssertNotNil(request, "request should not be nil")
  368. XCTAssertNotNil(response, "response should not be nil")
  369. XCTAssertNotNil(data, "data should not be nil")
  370. XCTAssertNil(error, "error should be nil")
  371. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  372. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  373. XCTAssertEqual(totalRedirectCount, 5, "total redirect count should be 5")
  374. }
  375. }