ResponseTests.swift 18 KB

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