ResponseTests.swift 18 KB

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