ResponseTests.swift 21 KB

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