ResponseSerializationTests.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. //
  2. // ResponseSerializationTests.swift
  3. //
  4. // Copyright (c) 2014-2016 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. class ResponseSerializationTestCase: BaseTestCase {
  28. // MARK: - Properties
  29. let error = AFError.responseSerializationFailed(reason: .inputDataNil)
  30. // MARK: - Tests - Data Response Serializer
  31. func testThatDataResponseSerializerSucceedsWhenDataIsNotNil() {
  32. // Given
  33. let serializer = Request.dataResponseSerializer()
  34. let data = "data".data(using: String.Encoding.utf8)!
  35. // When
  36. let result = serializer.serializeResponse(nil, nil, data, nil)
  37. // Then
  38. XCTAssertTrue(result.isSuccess, "result is success should be true")
  39. XCTAssertNotNil(result.value, "result value should not be nil")
  40. XCTAssertNil(result.error, "result error should be nil")
  41. }
  42. func testThatDataResponseSerializerFailsWhenDataIsNil() {
  43. // Given
  44. let serializer = Request.dataResponseSerializer()
  45. // When
  46. let result = serializer.serializeResponse(nil, nil, nil, nil)
  47. // Then
  48. XCTAssertTrue(result.isFailure, "result is failure should be true")
  49. XCTAssertNil(result.value, "result value should be nil")
  50. XCTAssertNotNil(result.error, "result error should not be nil")
  51. if let error = result.error as? AFError {
  52. XCTAssertTrue(error.isInputDataNil)
  53. } else {
  54. XCTFail("error should not be nil")
  55. }
  56. }
  57. func testThatDataResponseSerializerFailsWhenErrorIsNotNil() {
  58. // Given
  59. let serializer = Request.dataResponseSerializer()
  60. // When
  61. let result = serializer.serializeResponse(nil, nil, nil, error)
  62. // Then
  63. XCTAssertTrue(result.isFailure, "result is failure should be true")
  64. XCTAssertNil(result.value, "result value should be nil")
  65. XCTAssertNotNil(result.error, "result error should not be nil")
  66. if let error = result.error as? AFError {
  67. XCTAssertTrue(error.isInputDataNil)
  68. } else {
  69. XCTFail("error should not be nil")
  70. }
  71. }
  72. func testThatDataResponseSerializerFailsWhenDataIsNilWithNon204ResponseStatusCode() {
  73. // Given
  74. let serializer = Request.dataResponseSerializer()
  75. let url = URL(string: "https://httpbin.org/get")!
  76. let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
  77. // When
  78. let result = serializer.serializeResponse(nil, response, nil, nil)
  79. // Then
  80. XCTAssertTrue(result.isFailure, "result is failure should be true")
  81. XCTAssertNil(result.value, "result value should be nil")
  82. XCTAssertNotNil(result.error, "result error should not be nil")
  83. if let error = result.error as? AFError {
  84. XCTAssertTrue(error.isInputDataNil)
  85. } else {
  86. XCTFail("error should not be nil")
  87. }
  88. }
  89. func testThatDataResponseSerializerSucceedsWhenDataIsNilWith204ResponseStatusCode() {
  90. // Given
  91. let serializer = Request.dataResponseSerializer()
  92. let url = URL(string: "https://httpbin.org/get")!
  93. let response = HTTPURLResponse(url: url, statusCode: 204, httpVersion: "HTTP/1.1", headerFields: nil)
  94. // When
  95. let result = serializer.serializeResponse(nil, response, nil, nil)
  96. // Then
  97. XCTAssertTrue(result.isSuccess, "result is success should be true")
  98. XCTAssertNotNil(result.value, "result value should not be nil")
  99. XCTAssertNil(result.error, "result error should be nil")
  100. if let data = result.value {
  101. XCTAssertEqual(data.count, 0, "data length should be zero")
  102. }
  103. }
  104. // MARK: - Tests - String Response Serializer
  105. func testThatStringResponseSerializerFailsWhenDataIsNil() {
  106. // Given
  107. let serializer = Request.stringResponseSerializer()
  108. // When
  109. let result = serializer.serializeResponse(nil, nil, nil, nil)
  110. // Then
  111. XCTAssertTrue(result.isFailure, "result is failure should be true")
  112. XCTAssertNil(result.value, "result value should be nil")
  113. XCTAssertNotNil(result.error, "result error should not be nil")
  114. if let error = result.error as? AFError {
  115. XCTAssertTrue(error.isInputDataNil)
  116. } else {
  117. XCTFail("error should not be nil")
  118. }
  119. }
  120. func testThatStringResponseSerializerSucceedsWhenDataIsEmpty() {
  121. // Given
  122. let serializer = Request.stringResponseSerializer()
  123. // When
  124. let result = serializer.serializeResponse(nil, nil, Data(), nil)
  125. // Then
  126. XCTAssertTrue(result.isSuccess, "result is success should be true")
  127. XCTAssertNotNil(result.value, "result value should not be nil")
  128. XCTAssertNil(result.error, "result error should be nil")
  129. }
  130. func testThatStringResponseSerializerSucceedsWithUTF8DataAndNoProvidedEncoding() {
  131. let serializer = Request.stringResponseSerializer()
  132. let data = "data".data(using: String.Encoding.utf8)!
  133. // When
  134. let result = serializer.serializeResponse(nil, nil, data, nil)
  135. // Then
  136. XCTAssertTrue(result.isSuccess, "result is success should be true")
  137. XCTAssertNotNil(result.value, "result value should not be nil")
  138. XCTAssertNil(result.error, "result error should be nil")
  139. }
  140. func testThatStringResponseSerializerSucceedsWithUTF8DataAndUTF8ProvidedEncoding() {
  141. let serializer = Request.stringResponseSerializer(encoding: String.Encoding.utf8)
  142. let data = "data".data(using: String.Encoding.utf8)!
  143. // When
  144. let result = serializer.serializeResponse(nil, nil, data, nil)
  145. // Then
  146. XCTAssertTrue(result.isSuccess, "result is success should be true")
  147. XCTAssertNotNil(result.value, "result value should not be nil")
  148. XCTAssertNil(result.error, "result error should be nil")
  149. }
  150. func testThatStringResponseSerializerSucceedsWithUTF8DataUsingResponseTextEncodingName() {
  151. let serializer = Request.stringResponseSerializer()
  152. let data = "data".data(using: String.Encoding.utf8)!
  153. let response = HTTPURLResponse(
  154. url: URL(string: "https://httpbin.org/get")!,
  155. statusCode: 200,
  156. httpVersion: "HTTP/1.1",
  157. headerFields: ["Content-Type": "image/jpeg; charset=utf-8"]
  158. )
  159. // When
  160. let result = serializer.serializeResponse(nil, response, data, nil)
  161. // Then
  162. XCTAssertTrue(result.isSuccess, "result is success should be true")
  163. XCTAssertNotNil(result.value, "result value should not be nil")
  164. XCTAssertNil(result.error, "result error should be nil")
  165. }
  166. func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ProvidedEncoding() {
  167. // Given
  168. let serializer = Request.stringResponseSerializer(encoding: String.Encoding.utf8)
  169. let data = "random data".data(using: String.Encoding.utf32)!
  170. // When
  171. let result = serializer.serializeResponse(nil, nil, data, nil)
  172. // Then
  173. XCTAssertTrue(result.isFailure, "result is failure should be true")
  174. XCTAssertNil(result.value, "result value should be nil")
  175. XCTAssertNotNil(result.error, "result error should not be nil")
  176. if let error = result.error as? AFError, let failedEncoding = error.failedStringEncoding {
  177. XCTAssertTrue(error.isStringSerializationFailed)
  178. XCTAssertEqual(failedEncoding, String.Encoding.utf8)
  179. } else {
  180. XCTFail("error should not be nil")
  181. }
  182. }
  183. func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ResponseEncoding() {
  184. // Given
  185. let serializer = Request.stringResponseSerializer()
  186. let data = "random data".data(using: String.Encoding.utf32)!
  187. let response = HTTPURLResponse(
  188. url: URL(string: "https://httpbin.org/get")!,
  189. statusCode: 200,
  190. httpVersion: "HTTP/1.1",
  191. headerFields: ["Content-Type": "image/jpeg; charset=utf-8"]
  192. )
  193. // When
  194. let result = serializer.serializeResponse(nil, response, data, nil)
  195. // Then
  196. XCTAssertTrue(result.isFailure, "result is failure should be true")
  197. XCTAssertNil(result.value, "result value should be nil")
  198. XCTAssertNotNil(result.error, "result error should not be nil")
  199. if let error = result.error as? AFError, let failedEncoding = error.failedStringEncoding {
  200. XCTAssertTrue(error.isStringSerializationFailed)
  201. XCTAssertEqual(failedEncoding, String.Encoding.utf8)
  202. } else {
  203. XCTFail("error should not be nil")
  204. }
  205. }
  206. func testThatStringResponseSerializerFailsWhenErrorIsNotNil() {
  207. // Given
  208. let serializer = Request.stringResponseSerializer()
  209. // When
  210. let result = serializer.serializeResponse(nil, nil, nil, error)
  211. // Then
  212. XCTAssertTrue(result.isFailure, "result is failure should be true")
  213. XCTAssertNil(result.value, "result value should be nil")
  214. XCTAssertNotNil(result.error, "result error should not be nil")
  215. if let error = result.error as? AFError {
  216. XCTAssertTrue(error.isInputDataNil)
  217. } else {
  218. XCTFail("error should not be nil")
  219. }
  220. }
  221. func testThatStringResponseSerializerFailsWhenDataIsNilWithNon204ResponseStatusCode() {
  222. // Given
  223. let serializer = Request.stringResponseSerializer()
  224. let url = URL(string: "https://httpbin.org/get")!
  225. let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
  226. // When
  227. let result = serializer.serializeResponse(nil, response, nil, nil)
  228. // Then
  229. XCTAssertTrue(result.isFailure, "result is failure should be true")
  230. XCTAssertNil(result.value, "result value should be nil")
  231. XCTAssertNotNil(result.error, "result error should not be nil")
  232. if let error = result.error as? AFError {
  233. XCTAssertTrue(error.isInputDataNil)
  234. } else {
  235. XCTFail("error should not be nil")
  236. }
  237. }
  238. func testThatStringResponseSerializerSucceedsWhenDataIsNilWith204ResponseStatusCode() {
  239. // Given
  240. let serializer = Request.stringResponseSerializer()
  241. let url = URL(string: "https://httpbin.org/get")!
  242. let response = HTTPURLResponse(url: url, statusCode: 204, httpVersion: "HTTP/1.1", headerFields: nil)
  243. // When
  244. let result = serializer.serializeResponse(nil, response, nil, nil)
  245. // Then
  246. XCTAssertTrue(result.isSuccess, "result is success should be true")
  247. XCTAssertNotNil(result.value, "result value should not be nil")
  248. XCTAssertNil(result.error, "result error should be nil")
  249. if let string = result.value {
  250. XCTAssertEqual(string, "", "string should be equal to empty string")
  251. }
  252. }
  253. // MARK: - Tests - JSON Response Serializer
  254. func testThatJSONResponseSerializerFailsWhenDataIsNil() {
  255. // Given
  256. let serializer = Request.JSONResponseSerializer()
  257. // When
  258. let result = serializer.serializeResponse(nil, nil, nil, nil)
  259. // Then
  260. XCTAssertTrue(result.isFailure, "result is failure should be true")
  261. XCTAssertNil(result.value, "result value should be nil")
  262. XCTAssertNotNil(result.error, "result error should not be nil")
  263. if let error = result.error as? AFError {
  264. XCTAssertTrue(error.isInputDataNilOrZeroLength)
  265. } else {
  266. XCTFail("error should not be nil")
  267. }
  268. }
  269. func testThatJSONResponseSerializerFailsWhenDataIsEmpty() {
  270. // Given
  271. let serializer = Request.JSONResponseSerializer()
  272. // When
  273. let result = serializer.serializeResponse(nil, nil, Data(), nil)
  274. // Then
  275. XCTAssertTrue(result.isFailure, "result is failure should be true")
  276. XCTAssertNil(result.value, "result value should be nil")
  277. XCTAssertNotNil(result.error, "result error should not be nil")
  278. if let error = result.error as? AFError {
  279. XCTAssertTrue(error.isInputDataNilOrZeroLength)
  280. } else {
  281. XCTFail("error should not be nil")
  282. }
  283. }
  284. func testThatJSONResponseSerializerSucceedsWhenDataIsValidJSON() {
  285. // Given
  286. let serializer = Request.JSONResponseSerializer()
  287. let data = "{\"json\": true}".data(using: String.Encoding.utf8)!
  288. // When
  289. let result = serializer.serializeResponse(nil, nil, data, nil)
  290. // Then
  291. XCTAssertTrue(result.isSuccess, "result is success should be true")
  292. XCTAssertNotNil(result.value, "result value should not be nil")
  293. XCTAssertNil(result.error, "result error should be nil")
  294. }
  295. func testThatJSONResponseSerializerFailsWhenDataIsInvalidJSON() {
  296. // Given
  297. let serializer = Request.JSONResponseSerializer()
  298. let data = "definitely not valid json".data(using: String.Encoding.utf8)!
  299. // When
  300. let result = serializer.serializeResponse(nil, nil, data, nil)
  301. // Then
  302. XCTAssertTrue(result.isFailure, "result is failure should be true")
  303. XCTAssertNil(result.value, "result value should be nil")
  304. XCTAssertNotNil(result.error, "result error should not be nil")
  305. if let error = result.error as? AFError, let underlyingError = error.underlyingError as? CocoaError {
  306. XCTAssertTrue(error.isJSONSerializationFailed)
  307. XCTAssertEqual(underlyingError.errorCode, 3840)
  308. } else {
  309. XCTFail("error should not be nil")
  310. }
  311. }
  312. func testThatJSONResponseSerializerFailsWhenErrorIsNotNil() {
  313. // Given
  314. let serializer = Request.JSONResponseSerializer()
  315. // When
  316. let result = serializer.serializeResponse(nil, nil, nil, error)
  317. // Then
  318. XCTAssertTrue(result.isFailure, "result is failure should be true")
  319. XCTAssertNil(result.value, "result value should be nil")
  320. XCTAssertNotNil(result.error, "result error should not be nil")
  321. if let error = result.error as? AFError {
  322. XCTAssertTrue(error.isInputDataNil)
  323. } else {
  324. XCTFail("error should not be nil")
  325. }
  326. }
  327. func testThatJSONResponseSerializerFailsWhenDataIsNilWithNon204ResponseStatusCode() {
  328. // Given
  329. let serializer = Request.JSONResponseSerializer()
  330. let url = URL(string: "https://httpbin.org/get")!
  331. let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
  332. // When
  333. let result = serializer.serializeResponse(nil, response, nil, nil)
  334. // Then
  335. XCTAssertTrue(result.isFailure, "result is failure should be true")
  336. XCTAssertNil(result.value, "result value should be nil")
  337. XCTAssertNotNil(result.error, "result error should not be nil")
  338. if let error = result.error as? AFError {
  339. XCTAssertTrue(error.isInputDataNilOrZeroLength)
  340. } else {
  341. XCTFail("error should not be nil")
  342. }
  343. }
  344. func testThatJSONResponseSerializerSucceedsWhenDataIsNilWith204ResponseStatusCode() {
  345. // Given
  346. let serializer = Request.JSONResponseSerializer()
  347. let url = URL(string: "https://httpbin.org/get")!
  348. let response = HTTPURLResponse(url: url, statusCode: 204, httpVersion: "HTTP/1.1", headerFields: nil)
  349. // When
  350. let result = serializer.serializeResponse(nil, response, nil, nil)
  351. // Then
  352. XCTAssertTrue(result.isSuccess, "result is success should be true")
  353. XCTAssertNotNil(result.value, "result value should not be nil")
  354. XCTAssertNil(result.error, "result error should be nil")
  355. if let json = result.value as? NSNull {
  356. XCTAssertEqual(json, NSNull(), "json should be equal to NSNull")
  357. }
  358. }
  359. // MARK: - Tests - Property List Response Serializer
  360. func testThatPropertyListResponseSerializerFailsWhenDataIsNil() {
  361. // Given
  362. let serializer = Request.propertyListResponseSerializer()
  363. // When
  364. let result = serializer.serializeResponse(nil, nil, nil, nil)
  365. // Then
  366. XCTAssertTrue(result.isFailure, "result is failure should be true")
  367. XCTAssertNil(result.value, "result value should be nil")
  368. XCTAssertNotNil(result.error, "result error should not be nil")
  369. if let error = result.error as? AFError {
  370. XCTAssertTrue(error.isInputDataNilOrZeroLength)
  371. } else {
  372. XCTFail("error should not be nil")
  373. }
  374. }
  375. func testThatPropertyListResponseSerializerFailsWhenDataIsEmpty() {
  376. // Given
  377. let serializer = Request.propertyListResponseSerializer()
  378. // When
  379. let result = serializer.serializeResponse(nil, nil, Data(), nil)
  380. // Then
  381. XCTAssertTrue(result.isFailure, "result is failure should be true")
  382. XCTAssertNil(result.value, "result value should be nil")
  383. XCTAssertNotNil(result.error, "result error should not be nil")
  384. if let error = result.error as? AFError {
  385. XCTAssertTrue(error.isInputDataNilOrZeroLength)
  386. } else {
  387. XCTFail("error should not be nil")
  388. }
  389. }
  390. func testThatPropertyListResponseSerializerSucceedsWhenDataIsValidPropertyListData() {
  391. // Given
  392. let serializer = Request.propertyListResponseSerializer()
  393. let data = NSKeyedArchiver.archivedData(withRootObject: ["foo": "bar"])
  394. // When
  395. let result = serializer.serializeResponse(nil, nil, data, nil)
  396. // Then
  397. XCTAssertTrue(result.isSuccess, "result is success should be true")
  398. XCTAssertNotNil(result.value, "result value should not be nil")
  399. XCTAssertNil(result.error, "result error should be nil")
  400. }
  401. func testThatPropertyListResponseSerializerFailsWhenDataIsInvalidPropertyListData() {
  402. // Given
  403. let serializer = Request.propertyListResponseSerializer()
  404. let data = "definitely not valid plist data".data(using: String.Encoding.utf8)!
  405. // When
  406. let result = serializer.serializeResponse(nil, nil, data, nil)
  407. // Then
  408. XCTAssertTrue(result.isFailure, "result is failure should be true")
  409. XCTAssertNil(result.value, "result value should be nil")
  410. XCTAssertNotNil(result.error, "result error should not be nil")
  411. if let error = result.error as? AFError, let underlyingError = error.underlyingError as? CocoaError {
  412. XCTAssertTrue(error.isPropertyListSerializationFailed)
  413. XCTAssertEqual(underlyingError.errorCode, 3840)
  414. } else {
  415. XCTFail("error should not be nil")
  416. }
  417. }
  418. func testThatPropertyListResponseSerializerFailsWhenErrorIsNotNil() {
  419. // Given
  420. let serializer = Request.propertyListResponseSerializer()
  421. // When
  422. let result = serializer.serializeResponse(nil, nil, nil, error)
  423. // Then
  424. XCTAssertTrue(result.isFailure, "result is failure should be true")
  425. XCTAssertNil(result.value, "result value should be nil")
  426. XCTAssertNotNil(result.error, "result error should not be nil")
  427. if let error = result.error as? AFError {
  428. XCTAssertTrue(error.isInputDataNil)
  429. } else {
  430. XCTFail("error should not be nil")
  431. }
  432. }
  433. func testThatPropertyListResponseSerializerFailsWhenDataIsNilWithNon204ResponseStatusCode() {
  434. // Given
  435. let serializer = Request.propertyListResponseSerializer()
  436. let url = URL(string: "https://httpbin.org/get")!
  437. let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
  438. // When
  439. let result = serializer.serializeResponse(nil, response, nil, nil)
  440. // Then
  441. XCTAssertTrue(result.isFailure, "result is failure should be true")
  442. XCTAssertNil(result.value, "result value should be nil")
  443. XCTAssertNotNil(result.error, "result error should not be nil")
  444. if let error = result.error as? AFError {
  445. XCTAssertTrue(error.isInputDataNilOrZeroLength)
  446. } else {
  447. XCTFail("error should not be nil")
  448. }
  449. }
  450. func testThatPropertyListResponseSerializerSucceedsWhenDataIsNilWith204ResponseStatusCode() {
  451. // Given
  452. let serializer = Request.propertyListResponseSerializer()
  453. let url = URL(string: "https://httpbin.org/get")!
  454. let response = HTTPURLResponse(url: url, statusCode: 204, httpVersion: "HTTP/1.1", headerFields: nil)
  455. // When
  456. let result = serializer.serializeResponse(nil, response, nil, nil)
  457. // Then
  458. XCTAssertTrue(result.isSuccess, "result is success should be true")
  459. XCTAssertNotNil(result.value, "result value should not be nil")
  460. XCTAssertNil(result.error, "result error should be nil")
  461. if let plist = result.value as? NSNull {
  462. XCTAssertEqual(plist, NSNull(), "plist should be equal to NSNull")
  463. }
  464. }
  465. }