ResponseTests.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. //
  2. // ResponseTests.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Alamofire
  25. import Foundation
  26. import XCTest
  27. final class ResponseTestCase: BaseTestCase {
  28. func testThatResponseReturnsSuccessResultWithValidData() {
  29. // Given
  30. let expectation = self.expectation(description: "request should succeed")
  31. var response: DataResponse<Data?, AFError>?
  32. // When
  33. AF.request(.default, parameters: ["foo": "bar"]).response { resp in
  34. response = resp
  35. expectation.fulfill()
  36. }
  37. waitForExpectations(timeout: timeout)
  38. // Then
  39. XCTAssertNotNil(response?.request)
  40. XCTAssertNotNil(response?.response)
  41. XCTAssertNotNil(response?.data)
  42. XCTAssertNil(response?.error)
  43. XCTAssertNotNil(response?.metrics)
  44. }
  45. func testThatResponseReturnsFailureResultWithOptionalDataAndError() {
  46. // Given
  47. let urlString = String.invalidURL
  48. let expectation = self.expectation(description: "request should fail with invalid URL error")
  49. var response: DataResponse<Data?, AFError>?
  50. // When
  51. AF.request(urlString, parameters: ["foo": "bar"]).response { resp in
  52. response = resp
  53. expectation.fulfill()
  54. }
  55. waitForExpectations(timeout: timeout)
  56. // Then
  57. XCTAssertNotNil(response?.request)
  58. XCTAssertNil(response?.response)
  59. XCTAssertNil(response?.data)
  60. XCTAssertNotNil(response?.error)
  61. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  62. XCTAssertNotNil(response?.metrics)
  63. }
  64. }
  65. // MARK: -
  66. final class ResponseDataTestCase: BaseTestCase {
  67. func testThatResponseDataReturnsSuccessResultWithValidData() {
  68. // Given
  69. let expectation = self.expectation(description: "request should succeed")
  70. var response: DataResponse<Data, AFError>?
  71. // When
  72. AF.request(.default, parameters: ["foo": "bar"]).responseData { resp in
  73. response = resp
  74. expectation.fulfill()
  75. }
  76. waitForExpectations(timeout: timeout)
  77. // Then
  78. XCTAssertNotNil(response?.request)
  79. XCTAssertNotNil(response?.response)
  80. XCTAssertNotNil(response?.data)
  81. XCTAssertNotNil(response?.data)
  82. XCTAssertEqual(response?.result.isSuccess, true)
  83. XCTAssertNotNil(response?.metrics)
  84. }
  85. func testThatResponseDataReturnsFailureResultWithOptionalDataAndError() {
  86. // Given
  87. let urlString = String.invalidURL
  88. let expectation = self.expectation(description: "request should fail with invalid URL error")
  89. var response: DataResponse<Data, AFError>?
  90. // When
  91. AF.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
  92. response = resp
  93. expectation.fulfill()
  94. }
  95. waitForExpectations(timeout: timeout)
  96. // Then
  97. XCTAssertNotNil(response?.request)
  98. XCTAssertNil(response?.response)
  99. XCTAssertNil(response?.data)
  100. XCTAssertEqual(response?.result.isFailure, true)
  101. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  102. XCTAssertNotNil(response?.metrics)
  103. }
  104. }
  105. // MARK: -
  106. final class ResponseStringTestCase: BaseTestCase {
  107. func testThatResponseStringReturnsSuccessResultWithValidString() {
  108. // Given
  109. let expectation = self.expectation(description: "request should succeed")
  110. var response: DataResponse<String, AFError>?
  111. // When
  112. AF.request(.default, parameters: ["foo": "bar"]).responseString { resp in
  113. response = resp
  114. expectation.fulfill()
  115. }
  116. waitForExpectations(timeout: timeout)
  117. // Then
  118. XCTAssertNotNil(response?.request)
  119. XCTAssertNotNil(response?.response)
  120. XCTAssertNotNil(response?.data)
  121. XCTAssertNotNil(response?.data)
  122. XCTAssertEqual(response?.result.isSuccess, true)
  123. XCTAssertNotNil(response?.metrics)
  124. }
  125. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  126. // Given
  127. let urlString = String.invalidURL
  128. let expectation = self.expectation(description: "request should fail with invalid URL error")
  129. var response: DataResponse<String, AFError>?
  130. // When
  131. AF.request(urlString, parameters: ["foo": "bar"]).responseString { resp in
  132. response = resp
  133. expectation.fulfill()
  134. }
  135. waitForExpectations(timeout: timeout)
  136. // Then
  137. XCTAssertNotNil(response?.request)
  138. XCTAssertNil(response?.response)
  139. XCTAssertNil(response?.data)
  140. XCTAssertEqual(response?.result.isFailure, true)
  141. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  142. XCTAssertNotNil(response?.metrics)
  143. }
  144. }
  145. // MARK: -
  146. @available(*, deprecated)
  147. final class ResponseJSONTestCase: BaseTestCase {
  148. func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
  149. // Given
  150. let expectation = self.expectation(description: "request should succeed")
  151. var response: DataResponse<Any, AFError>?
  152. // When
  153. AF.request(.default, parameters: ["foo": "bar"]).responseJSON { resp in
  154. response = resp
  155. expectation.fulfill()
  156. }
  157. waitForExpectations(timeout: timeout)
  158. // Then
  159. XCTAssertNotNil(response?.request)
  160. XCTAssertNotNil(response?.response)
  161. XCTAssertNotNil(response?.data)
  162. XCTAssertNotNil(response?.data)
  163. XCTAssertEqual(response?.result.isSuccess, true)
  164. XCTAssertNotNil(response?.metrics)
  165. }
  166. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  167. // Given
  168. let urlString = String.invalidURL
  169. let expectation = self.expectation(description: "request should fail")
  170. var response: DataResponse<Any, AFError>?
  171. // When
  172. AF.request(urlString, parameters: ["foo": "bar"]).responseJSON { resp in
  173. response = resp
  174. expectation.fulfill()
  175. }
  176. waitForExpectations(timeout: timeout)
  177. // Then
  178. XCTAssertNotNil(response?.request)
  179. XCTAssertNil(response?.response)
  180. XCTAssertNil(response?.data)
  181. XCTAssertEqual(response?.result.isFailure, true)
  182. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  183. XCTAssertNotNil(response?.metrics)
  184. }
  185. func testThatResponseJSONReturnsSuccessResultForGETRequest() {
  186. // Given
  187. let expectation = self.expectation(description: "request should succeed")
  188. var response: DataResponse<Any, AFError>?
  189. // When
  190. AF.request(.default, parameters: ["foo": "bar"]).responseJSON { resp in
  191. response = resp
  192. expectation.fulfill()
  193. }
  194. waitForExpectations(timeout: timeout)
  195. // Then
  196. XCTAssertNotNil(response?.request)
  197. XCTAssertNotNil(response?.response)
  198. XCTAssertNotNil(response?.data)
  199. XCTAssertNotNil(response?.data)
  200. XCTAssertEqual(response?.result.isSuccess, true)
  201. XCTAssertNotNil(response?.metrics)
  202. if
  203. let responseDictionary = response?.result.success as? [String: Any],
  204. let args = responseDictionary["args"] as? [String: String] {
  205. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  206. } else {
  207. XCTFail("args should not be nil")
  208. }
  209. }
  210. func testThatResponseJSONReturnsSuccessResultForPOSTRequest() {
  211. // Given
  212. let expectation = self.expectation(description: "request should succeed")
  213. var response: DataResponse<Any, AFError>?
  214. // When
  215. AF.request(.method(.post), parameters: ["foo": "bar"]).responseJSON { resp in
  216. response = resp
  217. expectation.fulfill()
  218. }
  219. waitForExpectations(timeout: timeout)
  220. // Then
  221. XCTAssertNotNil(response?.request)
  222. XCTAssertNotNil(response?.response)
  223. XCTAssertNotNil(response?.data)
  224. XCTAssertNotNil(response?.data)
  225. XCTAssertEqual(response?.result.isSuccess, true)
  226. XCTAssertNotNil(response?.metrics)
  227. if
  228. let responseDictionary = response?.result.success as? [String: Any],
  229. let form = responseDictionary["form"] as? [String: String] {
  230. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  231. } else {
  232. XCTFail("form should not be nil")
  233. }
  234. }
  235. }
  236. final class ResponseJSONDecodableTestCase: BaseTestCase {
  237. func testThatResponseDecodableReturnsSuccessResultWithValidJSON() {
  238. // Given
  239. let url = Endpoint().url
  240. let expectation = self.expectation(description: "request should succeed")
  241. var response: DataResponse<TestResponse, AFError>?
  242. // When
  243. AF.request(url, parameters: [:]).responseDecodable(of: TestResponse.self) { resp in
  244. response = resp
  245. expectation.fulfill()
  246. }
  247. waitForExpectations(timeout: timeout)
  248. // Then
  249. XCTAssertNotNil(response?.request)
  250. XCTAssertNotNil(response?.response)
  251. XCTAssertNotNil(response?.data)
  252. XCTAssertEqual(response?.result.isSuccess, true)
  253. XCTAssertEqual(response?.result.success?.url, url.absoluteString)
  254. XCTAssertNotNil(response?.metrics)
  255. }
  256. func testThatResponseDecodableWithPassedTypeReturnsSuccessResultWithValidJSON() {
  257. // Given
  258. let url = Endpoint().url
  259. let expectation = self.expectation(description: "request should succeed")
  260. var response: DataResponse<TestResponse, AFError>?
  261. // When
  262. AF.request(url, parameters: [:]).responseDecodable(of: TestResponse.self) {
  263. response = $0
  264. expectation.fulfill()
  265. }
  266. waitForExpectations(timeout: timeout)
  267. // Then
  268. XCTAssertNotNil(response?.request)
  269. XCTAssertNotNil(response?.response)
  270. XCTAssertNotNil(response?.data)
  271. XCTAssertEqual(response?.result.isSuccess, true)
  272. XCTAssertEqual(response?.result.success?.url, url.absoluteString)
  273. XCTAssertNotNil(response?.metrics)
  274. }
  275. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  276. // Given
  277. let urlString = String.invalidURL
  278. let expectation = self.expectation(description: "request should fail")
  279. var response: DataResponse<TestResponse, AFError>?
  280. // When
  281. AF.request(urlString, parameters: [:]).responseDecodable(of: TestResponse.self) { resp in
  282. response = resp
  283. expectation.fulfill()
  284. }
  285. waitForExpectations(timeout: timeout)
  286. // Then
  287. XCTAssertNotNil(response?.request)
  288. XCTAssertNil(response?.response)
  289. XCTAssertNil(response?.data)
  290. XCTAssertEqual(response?.result.isFailure, true)
  291. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  292. XCTAssertNotNil(response?.metrics)
  293. }
  294. }
  295. // MARK: -
  296. final class ResponseMapTestCase: BaseTestCase {
  297. func testThatMapTransformsSuccessValue() {
  298. // Given
  299. let expectation = self.expectation(description: "request should succeed")
  300. var response: DataResponse<String, AFError>?
  301. // When
  302. AF.request(.default, parameters: ["foo": "bar"]).responseDecodable(of: TestResponse.self) { resp in
  303. response = resp.map { response in
  304. response.args?["foo"] ?? "invalid"
  305. }
  306. expectation.fulfill()
  307. }
  308. waitForExpectations(timeout: timeout)
  309. // Then
  310. XCTAssertNotNil(response?.request)
  311. XCTAssertNotNil(response?.response)
  312. XCTAssertNotNil(response?.data)
  313. XCTAssertEqual(response?.result.isSuccess, true)
  314. XCTAssertEqual(response?.result.success, "bar")
  315. XCTAssertNotNil(response?.metrics)
  316. }
  317. func testThatMapPreservesFailureError() {
  318. // Given
  319. let urlString = String.invalidURL
  320. let expectation = self.expectation(description: "request should fail with invalid URL error")
  321. var response: DataResponse<String, AFError>?
  322. // When
  323. AF.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
  324. response = resp.map { _ in "ignored" }
  325. expectation.fulfill()
  326. }
  327. waitForExpectations(timeout: timeout)
  328. // Then
  329. XCTAssertNotNil(response?.request)
  330. XCTAssertNil(response?.response)
  331. XCTAssertNil(response?.data)
  332. XCTAssertEqual(response?.result.isFailure, true)
  333. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  334. XCTAssertNotNil(response?.metrics)
  335. }
  336. }
  337. // MARK: -
  338. final class ResponseTryMapTestCase: BaseTestCase {
  339. func testThatTryMapTransformsSuccessValue() {
  340. // Given
  341. let expectation = self.expectation(description: "request should succeed")
  342. var response: DataResponse<String, Error>?
  343. // When
  344. AF.request(.default, parameters: ["foo": "bar"]).responseDecodable(of: TestResponse.self) { resp in
  345. response = resp.tryMap { response in
  346. response.args?["foo"] ?? "invalid"
  347. }
  348. expectation.fulfill()
  349. }
  350. waitForExpectations(timeout: timeout)
  351. // Then
  352. XCTAssertNotNil(response?.request)
  353. XCTAssertNotNil(response?.response)
  354. XCTAssertNotNil(response?.data)
  355. XCTAssertEqual(response?.result.isSuccess, true)
  356. XCTAssertEqual(response?.result.success, "bar")
  357. XCTAssertNotNil(response?.metrics)
  358. }
  359. func testThatTryMapCatchesTransformationError() {
  360. // Given
  361. struct TransformError: Error {}
  362. let expectation = self.expectation(description: "request should succeed")
  363. var response: DataResponse<String, Error>?
  364. // When
  365. AF.request(.default, parameters: ["foo": "bar"]).responseData { resp in
  366. response = resp.tryMap { _ in
  367. throw TransformError()
  368. }
  369. expectation.fulfill()
  370. }
  371. waitForExpectations(timeout: timeout)
  372. // Then
  373. XCTAssertNotNil(response?.request)
  374. XCTAssertNotNil(response?.response)
  375. XCTAssertNotNil(response?.data)
  376. XCTAssertEqual(response?.result.isFailure, true)
  377. if let error = response?.result.failure {
  378. XCTAssertTrue(error is TransformError)
  379. } else {
  380. XCTFail("tryMap should catch the transformation error")
  381. }
  382. XCTAssertNotNil(response?.metrics)
  383. }
  384. func testThatTryMapPreservesFailureError() {
  385. // Given
  386. let urlString = String.invalidURL
  387. let expectation = self.expectation(description: "request should fail with invalid URL error")
  388. var response: DataResponse<String, Error>?
  389. // When
  390. AF.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
  391. response = resp.tryMap { _ in "ignored" }
  392. expectation.fulfill()
  393. }
  394. waitForExpectations(timeout: timeout)
  395. // Then
  396. XCTAssertNotNil(response?.request)
  397. XCTAssertNil(response?.response)
  398. XCTAssertNil(response?.data)
  399. XCTAssertEqual(response?.result.isFailure, true)
  400. XCTAssertEqual(response?.error?.asAFError?.isSessionTaskError, true)
  401. XCTAssertNotNil(response?.metrics)
  402. }
  403. }
  404. // MARK: -
  405. enum TestError: Error {
  406. case error(error: AFError)
  407. }
  408. enum TransformationError: Error {
  409. case error
  410. func alwaysFails() throws -> TestError {
  411. throw TransformationError.error
  412. }
  413. }
  414. final class ResponseMapErrorTestCase: BaseTestCase {
  415. func testThatMapErrorTransformsFailureValue() {
  416. // Given
  417. let urlString = String.invalidURL
  418. let expectation = self.expectation(description: "request should not succeed")
  419. var response: DataResponse<TestResponse, TestError>?
  420. // When
  421. AF.request(urlString).responseDecodable(of: TestResponse.self) { resp in
  422. response = resp.mapError { error in
  423. TestError.error(error: error)
  424. }
  425. expectation.fulfill()
  426. }
  427. waitForExpectations(timeout: timeout)
  428. // Then
  429. XCTAssertNotNil(response?.request)
  430. XCTAssertNil(response?.response)
  431. XCTAssertNil(response?.data)
  432. XCTAssertEqual(response?.result.isFailure, true)
  433. guard let error = response?.error, case .error = error else { XCTFail(); return }
  434. XCTAssertNotNil(response?.metrics)
  435. }
  436. func testThatMapErrorPreservesSuccessValue() {
  437. // Given
  438. let expectation = self.expectation(description: "request should succeed")
  439. var response: DataResponse<Data, TestError>?
  440. // When
  441. AF.request(.default).responseData { resp in
  442. response = resp.mapError { TestError.error(error: $0) }
  443. expectation.fulfill()
  444. }
  445. waitForExpectations(timeout: timeout)
  446. // Then
  447. XCTAssertNotNil(response?.request)
  448. XCTAssertNotNil(response?.response)
  449. XCTAssertNotNil(response?.data)
  450. XCTAssertEqual(response?.result.isSuccess, true)
  451. XCTAssertNotNil(response?.metrics)
  452. }
  453. }
  454. // MARK: -
  455. final class ResponseTryMapErrorTestCase: BaseTestCase {
  456. func testThatTryMapErrorPreservesSuccessValue() {
  457. // Given
  458. let expectation = self.expectation(description: "request should succeed")
  459. var response: DataResponse<Data, Error>?
  460. // When
  461. AF.request(.default).responseData { resp in
  462. response = resp.tryMapError { TestError.error(error: $0) }
  463. expectation.fulfill()
  464. }
  465. waitForExpectations(timeout: timeout)
  466. // Then
  467. XCTAssertNotNil(response?.request)
  468. XCTAssertNotNil(response?.response)
  469. XCTAssertNotNil(response?.data)
  470. XCTAssertEqual(response?.result.isSuccess, true)
  471. XCTAssertNotNil(response?.metrics)
  472. }
  473. func testThatTryMapErrorCatchesTransformationError() {
  474. // Given
  475. let urlString = String.invalidURL
  476. let expectation = self.expectation(description: "request should fail")
  477. var response: DataResponse<Data, Error>?
  478. // When
  479. AF.request(urlString).responseData { resp in
  480. response = resp.tryMapError { _ in try TransformationError.error.alwaysFails() }
  481. expectation.fulfill()
  482. }
  483. waitForExpectations(timeout: timeout)
  484. // Then
  485. XCTAssertNotNil(response?.request)
  486. XCTAssertNil(response?.response)
  487. XCTAssertNil(response?.data)
  488. XCTAssertEqual(response?.result.isFailure, true)
  489. if let error = response?.result.failure {
  490. XCTAssertTrue(error is TransformationError)
  491. } else {
  492. XCTFail("tryMapError should catch the transformation error")
  493. }
  494. XCTAssertNotNil(response?.metrics)
  495. }
  496. func testThatTryMapErrorTransformsError() {
  497. // Given
  498. let urlString = String.invalidURL
  499. let expectation = self.expectation(description: "request should fail")
  500. var response: DataResponse<Data, Error>?
  501. // When
  502. AF.request(urlString).responseData { resp in
  503. response = resp.tryMapError { TestError.error(error: $0) }
  504. expectation.fulfill()
  505. }
  506. waitForExpectations(timeout: timeout)
  507. // Then
  508. XCTAssertNotNil(response?.request)
  509. XCTAssertNil(response?.response)
  510. XCTAssertNil(response?.data)
  511. XCTAssertEqual(response?.result.isFailure, true)
  512. XCTAssertNotNil(response?.metrics)
  513. }
  514. }