ResponseTests.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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.nonexistentDomain
  48. let expectation = self.expectation(description: "request should fail with invalid hostname 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. XCTAssertEqual(response?.error?.isHostURLError, true)
  63. XCTAssertNotNil(response?.metrics)
  64. }
  65. }
  66. // MARK: -
  67. final class ResponseDataTestCase: BaseTestCase {
  68. func testThatResponseDataReturnsSuccessResultWithValidData() {
  69. // Given
  70. let expectation = self.expectation(description: "request should succeed")
  71. var response: DataResponse<Data, AFError>?
  72. // When
  73. AF.request(.default, parameters: ["foo": "bar"]).responseData { resp in
  74. response = resp
  75. expectation.fulfill()
  76. }
  77. waitForExpectations(timeout: timeout)
  78. // Then
  79. XCTAssertNotNil(response?.request)
  80. XCTAssertNotNil(response?.response)
  81. XCTAssertNotNil(response?.data)
  82. XCTAssertNotNil(response?.data)
  83. XCTAssertEqual(response?.result.isSuccess, true)
  84. XCTAssertNotNil(response?.metrics)
  85. }
  86. func testThatResponseDataReturnsFailureResultWithOptionalDataAndError() {
  87. // Given
  88. let urlString = String.nonexistentDomain
  89. let expectation = self.expectation(description: "request should fail with 404")
  90. var response: DataResponse<Data, AFError>?
  91. // When
  92. AF.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
  93. response = resp
  94. expectation.fulfill()
  95. }
  96. waitForExpectations(timeout: timeout)
  97. // Then
  98. XCTAssertNotNil(response?.request)
  99. XCTAssertNil(response?.response)
  100. XCTAssertNil(response?.data)
  101. XCTAssertEqual(response?.result.isFailure, true)
  102. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  103. XCTAssertEqual(response?.error?.isHostURLError, true)
  104. XCTAssertNotNil(response?.metrics)
  105. }
  106. }
  107. // MARK: -
  108. final class ResponseStringTestCase: BaseTestCase {
  109. func testThatResponseStringReturnsSuccessResultWithValidString() {
  110. // Given
  111. let expectation = self.expectation(description: "request should succeed")
  112. var response: DataResponse<String, AFError>?
  113. // When
  114. AF.request(.default, parameters: ["foo": "bar"]).responseString { resp in
  115. response = resp
  116. expectation.fulfill()
  117. }
  118. waitForExpectations(timeout: timeout)
  119. // Then
  120. XCTAssertNotNil(response?.request)
  121. XCTAssertNotNil(response?.response)
  122. XCTAssertNotNil(response?.data)
  123. XCTAssertNotNil(response?.data)
  124. XCTAssertEqual(response?.result.isSuccess, true)
  125. XCTAssertNotNil(response?.metrics)
  126. }
  127. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  128. // Given
  129. let urlString = String.nonexistentDomain
  130. let expectation = self.expectation(description: "request should fail with 404")
  131. var response: DataResponse<String, AFError>?
  132. // When
  133. AF.request(urlString, parameters: ["foo": "bar"]).responseString { resp in
  134. response = resp
  135. expectation.fulfill()
  136. }
  137. waitForExpectations(timeout: timeout)
  138. // Then
  139. XCTAssertNotNil(response?.request)
  140. XCTAssertNil(response?.response)
  141. XCTAssertNil(response?.data)
  142. XCTAssertEqual(response?.result.isFailure, true)
  143. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  144. XCTAssertEqual(response?.error?.isHostURLError, true)
  145. XCTAssertNotNil(response?.metrics)
  146. }
  147. }
  148. // MARK: -
  149. @available(*, deprecated)
  150. final class ResponseJSONTestCase: BaseTestCase {
  151. func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
  152. // Given
  153. let expectation = self.expectation(description: "request should succeed")
  154. var response: DataResponse<Any, AFError>?
  155. // When
  156. AF.request(.default, parameters: ["foo": "bar"]).responseJSON { resp in
  157. response = resp
  158. expectation.fulfill()
  159. }
  160. waitForExpectations(timeout: timeout)
  161. // Then
  162. XCTAssertNotNil(response?.request)
  163. XCTAssertNotNil(response?.response)
  164. XCTAssertNotNil(response?.data)
  165. XCTAssertNotNil(response?.data)
  166. XCTAssertEqual(response?.result.isSuccess, true)
  167. XCTAssertNotNil(response?.metrics)
  168. }
  169. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  170. // Given
  171. let urlString = String.nonexistentDomain
  172. let expectation = self.expectation(description: "request should fail")
  173. var response: DataResponse<Any, AFError>?
  174. // When
  175. AF.request(urlString, parameters: ["foo": "bar"]).responseJSON { resp in
  176. response = resp
  177. expectation.fulfill()
  178. }
  179. waitForExpectations(timeout: timeout)
  180. // Then
  181. XCTAssertNotNil(response?.request)
  182. XCTAssertNil(response?.response)
  183. XCTAssertNil(response?.data)
  184. XCTAssertEqual(response?.result.isFailure, true)
  185. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  186. XCTAssertEqual(response?.error?.isHostURLError, true)
  187. XCTAssertNotNil(response?.metrics)
  188. }
  189. func testThatResponseJSONReturnsSuccessResultForGETRequest() {
  190. // Given
  191. let expectation = self.expectation(description: "request should succeed")
  192. var response: DataResponse<Any, AFError>?
  193. // When
  194. AF.request(.default, parameters: ["foo": "bar"]).responseJSON { resp in
  195. response = resp
  196. expectation.fulfill()
  197. }
  198. waitForExpectations(timeout: timeout)
  199. // Then
  200. XCTAssertNotNil(response?.request)
  201. XCTAssertNotNil(response?.response)
  202. XCTAssertNotNil(response?.data)
  203. XCTAssertNotNil(response?.data)
  204. XCTAssertEqual(response?.result.isSuccess, true)
  205. XCTAssertNotNil(response?.metrics)
  206. if
  207. let responseDictionary = response?.result.success as? [String: Any],
  208. let args = responseDictionary["args"] as? [String: String] {
  209. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  210. } else {
  211. XCTFail("args should not be nil")
  212. }
  213. }
  214. func testThatResponseJSONReturnsSuccessResultForPOSTRequest() {
  215. // Given
  216. let expectation = self.expectation(description: "request should succeed")
  217. var response: DataResponse<Any, AFError>?
  218. // When
  219. AF.request(.method(.post), parameters: ["foo": "bar"]).responseJSON { resp in
  220. response = resp
  221. expectation.fulfill()
  222. }
  223. waitForExpectations(timeout: timeout)
  224. // Then
  225. XCTAssertNotNil(response?.request)
  226. XCTAssertNotNil(response?.response)
  227. XCTAssertNotNil(response?.data)
  228. XCTAssertNotNil(response?.data)
  229. XCTAssertEqual(response?.result.isSuccess, true)
  230. XCTAssertNotNil(response?.metrics)
  231. if
  232. let responseDictionary = response?.result.success as? [String: Any],
  233. let form = responseDictionary["form"] as? [String: String] {
  234. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  235. } else {
  236. XCTFail("form should not be nil")
  237. }
  238. }
  239. }
  240. final class ResponseJSONDecodableTestCase: BaseTestCase {
  241. func testThatResponseDecodableReturnsSuccessResultWithValidJSON() {
  242. // Given
  243. let url = Endpoint().url
  244. let expectation = self.expectation(description: "request should succeed")
  245. var response: DataResponse<TestResponse, AFError>?
  246. // When
  247. AF.request(url, parameters: [:]).responseDecodable(of: TestResponse.self) { resp in
  248. response = resp
  249. expectation.fulfill()
  250. }
  251. waitForExpectations(timeout: timeout)
  252. // Then
  253. XCTAssertNotNil(response?.request)
  254. XCTAssertNotNil(response?.response)
  255. XCTAssertNotNil(response?.data)
  256. XCTAssertEqual(response?.result.isSuccess, true)
  257. XCTAssertEqual(response?.result.success?.url, url.absoluteString)
  258. XCTAssertNotNil(response?.metrics)
  259. }
  260. func testThatResponseDecodableWithPassedTypeReturnsSuccessResultWithValidJSON() {
  261. // Given
  262. let url = Endpoint().url
  263. let expectation = self.expectation(description: "request should succeed")
  264. var response: DataResponse<TestResponse, AFError>?
  265. // When
  266. AF.request(url, parameters: [:]).responseDecodable(of: TestResponse.self) {
  267. response = $0
  268. expectation.fulfill()
  269. }
  270. waitForExpectations(timeout: timeout)
  271. // Then
  272. XCTAssertNotNil(response?.request)
  273. XCTAssertNotNil(response?.response)
  274. XCTAssertNotNil(response?.data)
  275. XCTAssertEqual(response?.result.isSuccess, true)
  276. XCTAssertEqual(response?.result.success?.url, url.absoluteString)
  277. XCTAssertNotNil(response?.metrics)
  278. }
  279. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  280. // Given
  281. let urlString = String.nonexistentDomain
  282. let expectation = self.expectation(description: "request should fail")
  283. var response: DataResponse<TestResponse, AFError>?
  284. // When
  285. AF.request(urlString, parameters: [:]).responseDecodable(of: TestResponse.self) { resp in
  286. response = resp
  287. expectation.fulfill()
  288. }
  289. waitForExpectations(timeout: timeout)
  290. // Then
  291. XCTAssertNotNil(response?.request)
  292. XCTAssertNil(response?.response)
  293. XCTAssertNil(response?.data)
  294. XCTAssertEqual(response?.result.isFailure, true)
  295. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  296. XCTAssertEqual(response?.error?.isHostURLError, true)
  297. XCTAssertNotNil(response?.metrics)
  298. }
  299. }
  300. // MARK: -
  301. final class ResponseMapTestCase: BaseTestCase {
  302. func testThatMapTransformsSuccessValue() {
  303. // Given
  304. let expectation = self.expectation(description: "request should succeed")
  305. var response: DataResponse<String, AFError>?
  306. // When
  307. AF.request(.default, parameters: ["foo": "bar"]).responseDecodable(of: TestResponse.self) { resp in
  308. response = resp.map { response in
  309. response.args?["foo"] ?? "invalid"
  310. }
  311. expectation.fulfill()
  312. }
  313. waitForExpectations(timeout: timeout)
  314. // Then
  315. XCTAssertNotNil(response?.request)
  316. XCTAssertNotNil(response?.response)
  317. XCTAssertNotNil(response?.data)
  318. XCTAssertEqual(response?.result.isSuccess, true)
  319. XCTAssertEqual(response?.result.success, "bar")
  320. XCTAssertNotNil(response?.metrics)
  321. }
  322. func testThatMapPreservesFailureError() {
  323. // Given
  324. let urlString = String.nonexistentDomain
  325. let expectation = self.expectation(description: "request should fail with 404")
  326. var response: DataResponse<String, AFError>?
  327. // When
  328. AF.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
  329. response = resp.map { _ in "ignored" }
  330. expectation.fulfill()
  331. }
  332. waitForExpectations(timeout: timeout)
  333. // Then
  334. XCTAssertNotNil(response?.request)
  335. XCTAssertNil(response?.response)
  336. XCTAssertNil(response?.data)
  337. XCTAssertEqual(response?.result.isFailure, true)
  338. XCTAssertEqual(response?.error?.isSessionTaskError, true)
  339. XCTAssertEqual(response?.error?.isHostURLError, true)
  340. XCTAssertNotNil(response?.metrics)
  341. }
  342. }
  343. // MARK: -
  344. final class ResponseTryMapTestCase: BaseTestCase {
  345. func testThatTryMapTransformsSuccessValue() {
  346. // Given
  347. let expectation = self.expectation(description: "request should succeed")
  348. var response: DataResponse<String, Error>?
  349. // When
  350. AF.request(.default, parameters: ["foo": "bar"]).responseDecodable(of: TestResponse.self) { resp in
  351. response = resp.tryMap { response in
  352. response.args?["foo"] ?? "invalid"
  353. }
  354. expectation.fulfill()
  355. }
  356. waitForExpectations(timeout: timeout)
  357. // Then
  358. XCTAssertNotNil(response?.request)
  359. XCTAssertNotNil(response?.response)
  360. XCTAssertNotNil(response?.data)
  361. XCTAssertEqual(response?.result.isSuccess, true)
  362. XCTAssertEqual(response?.result.success, "bar")
  363. XCTAssertNotNil(response?.metrics)
  364. }
  365. func testThatTryMapCatchesTransformationError() {
  366. // Given
  367. struct TransformError: Error {}
  368. let expectation = self.expectation(description: "request should succeed")
  369. var response: DataResponse<String, Error>?
  370. // When
  371. AF.request(.default, parameters: ["foo": "bar"]).responseData { resp in
  372. response = resp.tryMap { _ in
  373. throw TransformError()
  374. }
  375. expectation.fulfill()
  376. }
  377. waitForExpectations(timeout: timeout)
  378. // Then
  379. XCTAssertNotNil(response?.request)
  380. XCTAssertNotNil(response?.response)
  381. XCTAssertNotNil(response?.data)
  382. XCTAssertEqual(response?.result.isFailure, true)
  383. if let error = response?.result.failure {
  384. XCTAssertTrue(error is TransformError)
  385. } else {
  386. XCTFail("tryMap should catch the transformation error")
  387. }
  388. XCTAssertNotNil(response?.metrics)
  389. }
  390. func testThatTryMapPreservesFailureError() {
  391. // Given
  392. let urlString = String.nonexistentDomain
  393. let expectation = self.expectation(description: "request should fail with 404")
  394. var response: DataResponse<String, Error>?
  395. // When
  396. AF.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
  397. response = resp.tryMap { _ in "ignored" }
  398. expectation.fulfill()
  399. }
  400. waitForExpectations(timeout: timeout)
  401. // Then
  402. XCTAssertNotNil(response?.request)
  403. XCTAssertNil(response?.response)
  404. XCTAssertNil(response?.data)
  405. XCTAssertEqual(response?.result.isFailure, true)
  406. XCTAssertEqual(response?.error?.asAFError?.isSessionTaskError, true)
  407. XCTAssertEqual(response?.error?.asAFError?.isHostURLError, true)
  408. XCTAssertNotNil(response?.metrics)
  409. }
  410. }
  411. // MARK: -
  412. enum TestError: Error {
  413. case error(error: AFError)
  414. }
  415. enum TransformationError: Error {
  416. case error
  417. func alwaysFails() throws -> TestError {
  418. throw TransformationError.error
  419. }
  420. }
  421. final class ResponseMapErrorTestCase: BaseTestCase {
  422. func testThatMapErrorTransformsFailureValue() {
  423. // Given
  424. let urlString = String.nonexistentDomain
  425. let expectation = self.expectation(description: "request should not succeed")
  426. var response: DataResponse<TestResponse, TestError>?
  427. // When
  428. AF.request(urlString).responseDecodable(of: TestResponse.self) { resp in
  429. response = resp.mapError { error in
  430. TestError.error(error: error)
  431. }
  432. expectation.fulfill()
  433. }
  434. waitForExpectations(timeout: timeout)
  435. // Then
  436. XCTAssertNotNil(response?.request)
  437. XCTAssertNil(response?.response)
  438. XCTAssertNil(response?.data)
  439. XCTAssertEqual(response?.result.isFailure, true)
  440. guard let error = response?.error, case .error = error else { XCTFail(); return }
  441. XCTAssertNotNil(response?.metrics)
  442. }
  443. func testThatMapErrorPreservesSuccessValue() {
  444. // Given
  445. let expectation = self.expectation(description: "request should succeed")
  446. var response: DataResponse<Data, TestError>?
  447. // When
  448. AF.request(.default).responseData { resp in
  449. response = resp.mapError { TestError.error(error: $0) }
  450. expectation.fulfill()
  451. }
  452. waitForExpectations(timeout: timeout)
  453. // Then
  454. XCTAssertNotNil(response?.request)
  455. XCTAssertNotNil(response?.response)
  456. XCTAssertNotNil(response?.data)
  457. XCTAssertEqual(response?.result.isSuccess, true)
  458. XCTAssertNotNil(response?.metrics)
  459. }
  460. }
  461. // MARK: -
  462. final class ResponseTryMapErrorTestCase: BaseTestCase {
  463. func testThatTryMapErrorPreservesSuccessValue() {
  464. // Given
  465. let expectation = self.expectation(description: "request should succeed")
  466. var response: DataResponse<Data, Error>?
  467. // When
  468. AF.request(.default).responseData { resp in
  469. response = resp.tryMapError { TestError.error(error: $0) }
  470. expectation.fulfill()
  471. }
  472. waitForExpectations(timeout: timeout)
  473. // Then
  474. XCTAssertNotNil(response?.request)
  475. XCTAssertNotNil(response?.response)
  476. XCTAssertNotNil(response?.data)
  477. XCTAssertEqual(response?.result.isSuccess, true)
  478. XCTAssertNotNil(response?.metrics)
  479. }
  480. func testThatTryMapErrorCatchesTransformationError() {
  481. // Given
  482. let urlString = String.nonexistentDomain
  483. let expectation = self.expectation(description: "request should fail")
  484. var response: DataResponse<Data, Error>?
  485. // When
  486. AF.request(urlString).responseData { resp in
  487. response = resp.tryMapError { _ in try TransformationError.error.alwaysFails() }
  488. expectation.fulfill()
  489. }
  490. waitForExpectations(timeout: timeout)
  491. // Then
  492. XCTAssertNotNil(response?.request)
  493. XCTAssertNil(response?.response)
  494. XCTAssertNil(response?.data)
  495. XCTAssertEqual(response?.result.isFailure, true)
  496. if let error = response?.result.failure {
  497. XCTAssertTrue(error is TransformationError)
  498. } else {
  499. XCTFail("tryMapError should catch the transformation error")
  500. }
  501. XCTAssertNotNil(response?.metrics)
  502. }
  503. func testThatTryMapErrorTransformsError() {
  504. // Given
  505. let urlString = String.nonexistentDomain
  506. let expectation = self.expectation(description: "request should fail")
  507. var response: DataResponse<Data, Error>?
  508. // When
  509. AF.request(urlString).responseData { resp in
  510. response = resp.tryMapError { TestError.error(error: $0) }
  511. expectation.fulfill()
  512. }
  513. waitForExpectations(timeout: timeout)
  514. // Then
  515. XCTAssertNotNil(response?.request)
  516. XCTAssertNil(response?.response)
  517. XCTAssertNil(response?.data)
  518. XCTAssertEqual(response?.result.isFailure, true)
  519. guard let error = response?.error as? TestError,
  520. case let .error(underlyingError) = error
  521. else { XCTFail(); return }
  522. XCTAssertEqual(underlyingError.asAFError?.isSessionTaskError, true)
  523. XCTAssertEqual(underlyingError.asAFError?.isHostURLError, true)
  524. XCTAssertNotNil(response?.metrics)
  525. }
  526. }