ResponseSerializationTests.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // ResponseSerializationTests.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 ResponseSerializationTestCase: BaseTestCase {
  26. let error = NSError(domain: Error.Domain, code: -10000, userInfo: nil)
  27. // MARK: - Data Response Serializer Tests
  28. func testThatDataResponseSerializerSucceedsWhenDataIsNotNil() {
  29. // Given
  30. let serializer = Request.dataResponseSerializer()
  31. let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!
  32. // When
  33. let result = serializer.serializeResponse(nil, nil, data, nil)
  34. // Then
  35. XCTAssertTrue(result.isSuccess, "result is success should be true")
  36. XCTAssertNotNil(result.value, "result value should not be nil")
  37. XCTAssertNil(result.error, "result error should be nil")
  38. }
  39. func testThatDataResponseSerializerFailsWhenDataIsNil() {
  40. // Given
  41. let serializer = Request.dataResponseSerializer()
  42. // When
  43. let result = serializer.serializeResponse(nil, nil, nil, nil)
  44. // Then
  45. XCTAssertTrue(result.isFailure, "result is failure should be true")
  46. XCTAssertNil(result.value, "result value should be nil")
  47. XCTAssertNotNil(result.error, "result error should not be nil")
  48. if let error = result.error {
  49. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  50. XCTAssertEqual(error.code, Error.Code.DataSerializationFailed.rawValue, "error code should match expected value")
  51. } else {
  52. XCTFail("error should not be nil")
  53. }
  54. }
  55. func testThatDataResponseSerializerFailsWhenErrorIsNotNil() {
  56. // Given
  57. let serializer = Request.dataResponseSerializer()
  58. // When
  59. let result = serializer.serializeResponse(nil, nil, nil, error)
  60. // Then
  61. XCTAssertTrue(result.isFailure, "result is failure should be true")
  62. XCTAssertNil(result.value, "result value should be nil")
  63. XCTAssertNotNil(result.error, "result error should not be nil")
  64. if let error = result.error {
  65. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  66. XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
  67. } else {
  68. XCTFail("error should not be nil")
  69. }
  70. }
  71. // MARK: - String Response Serializer Tests
  72. func testThatStringResponseSerializerFailsWhenDataIsNil() {
  73. // Given
  74. let serializer = Request.stringResponseSerializer()
  75. // When
  76. let result = serializer.serializeResponse(nil, nil, nil, nil)
  77. // Then
  78. XCTAssertTrue(result.isFailure, "result is failure should be true")
  79. XCTAssertNil(result.value, "result value should be nil")
  80. XCTAssertNotNil(result.error, "result error should not be nil")
  81. if let error = result.error {
  82. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  83. XCTAssertEqual(error.code, Error.Code.StringSerializationFailed.rawValue, "error code should match expected value")
  84. } else {
  85. XCTFail("error should not be nil")
  86. }
  87. }
  88. func testThatStringResponseSerializerFailsWhenDataIsEmpty() {
  89. // Given
  90. let serializer = Request.stringResponseSerializer()
  91. // When
  92. let result = serializer.serializeResponse(nil, nil, NSData(), nil)
  93. // Then
  94. XCTAssertTrue(result.isFailure, "result is failure should be true")
  95. XCTAssertNil(result.value, "result value should be nil")
  96. XCTAssertNotNil(result.error, "result error should not be nil")
  97. }
  98. func testThatStringResponseSerializerSucceedsWithUTF8DataAndNoProvidedEncoding() {
  99. let serializer = Request.stringResponseSerializer()
  100. let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!
  101. // When
  102. let result = serializer.serializeResponse(nil, nil, data, nil)
  103. // Then
  104. XCTAssertTrue(result.isSuccess, "result is success should be true")
  105. XCTAssertNotNil(result.value, "result value should not be nil")
  106. XCTAssertNil(result.error, "result error should be nil")
  107. }
  108. func testThatStringResponseSerializerSucceedsWithUTF8DataAndUTF8ProvidedEncoding() {
  109. let serializer = Request.stringResponseSerializer(encoding: NSUTF8StringEncoding)
  110. let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!
  111. // When
  112. let result = serializer.serializeResponse(nil, nil, data, nil)
  113. // Then
  114. XCTAssertTrue(result.isSuccess, "result is success should be true")
  115. XCTAssertNotNil(result.value, "result value should not be nil")
  116. XCTAssertNil(result.error, "result error should be nil")
  117. }
  118. func testThatStringResponseSerializerSucceedsWithUTF8DataUsingResponseTextEncodingName() {
  119. let serializer = Request.stringResponseSerializer()
  120. let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!
  121. let response = NSHTTPURLResponse(
  122. URL: NSURL(string: "https://httpbin.org/get")!,
  123. statusCode: 200,
  124. HTTPVersion: "HTTP/1.1",
  125. headerFields: ["Content-Type": "image/jpeg; charset=utf-8"]
  126. )
  127. // When
  128. let result = serializer.serializeResponse(nil, response, data, nil)
  129. // Then
  130. XCTAssertTrue(result.isSuccess, "result is success should be true")
  131. XCTAssertNotNil(result.value, "result value should not be nil")
  132. XCTAssertNil(result.error, "result error should be nil")
  133. }
  134. func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ProvidedEncoding() {
  135. // Given
  136. let serializer = Request.stringResponseSerializer(encoding: NSUTF8StringEncoding)
  137. let data = "random data".dataUsingEncoding(NSUTF32StringEncoding)!
  138. // When
  139. let result = serializer.serializeResponse(nil, nil, data, nil)
  140. // Then
  141. XCTAssertTrue(result.isFailure, "result is failure should be true")
  142. XCTAssertNil(result.value, "result value should be nil")
  143. XCTAssertNotNil(result.error, "result error should not be nil")
  144. if let error = result.error {
  145. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  146. XCTAssertEqual(error.code, Error.Code.StringSerializationFailed.rawValue, "error code should match expected value")
  147. } else {
  148. XCTFail("error should not be nil")
  149. }
  150. }
  151. func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ResponseEncoding() {
  152. // Given
  153. let serializer = Request.stringResponseSerializer()
  154. let data = "random data".dataUsingEncoding(NSUTF32StringEncoding)!
  155. let response = NSHTTPURLResponse(
  156. URL: NSURL(string: "https://httpbin.org/get")!,
  157. statusCode: 200,
  158. HTTPVersion: "HTTP/1.1",
  159. headerFields: ["Content-Type": "image/jpeg; charset=utf-8"]
  160. )
  161. // When
  162. let result = serializer.serializeResponse(nil, response, data, nil)
  163. // Then
  164. XCTAssertTrue(result.isFailure, "result is failure should be true")
  165. XCTAssertNil(result.value, "result value should be nil")
  166. XCTAssertNotNil(result.error, "result error should not be nil")
  167. if let error = result.error {
  168. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  169. XCTAssertEqual(error.code, Error.Code.StringSerializationFailed.rawValue, "error code should match expected value")
  170. } else {
  171. XCTFail("error should not be nil")
  172. }
  173. }
  174. func testThatStringResponseSerializerFailsWhenErrorIsNotNil() {
  175. // Given
  176. let serializer = Request.stringResponseSerializer()
  177. // When
  178. let result = serializer.serializeResponse(nil, nil, nil, error)
  179. // Then
  180. XCTAssertTrue(result.isFailure, "result is failure should be true")
  181. XCTAssertNil(result.value, "result value should be nil")
  182. XCTAssertNotNil(result.error, "result error should not be nil")
  183. if let error = result.error {
  184. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  185. XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
  186. } else {
  187. XCTFail("error should not be nil")
  188. }
  189. }
  190. // MARK: - JSON Response Serializer Tests
  191. func testThatJSONResponseSerializerFailsWhenDataIsNil() {
  192. // Given
  193. let serializer = Request.JSONResponseSerializer()
  194. // When
  195. let result = serializer.serializeResponse(nil, nil, nil, nil)
  196. // Then
  197. XCTAssertTrue(result.isFailure, "result is failure should be true")
  198. XCTAssertNil(result.value, "result value should be nil")
  199. XCTAssertNotNil(result.error, "result error should not be nil")
  200. if let error = result.error {
  201. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  202. XCTAssertEqual(error.code, Error.Code.JSONSerializationFailed.rawValue, "error code should match expected value")
  203. } else {
  204. XCTFail("error should not be nil")
  205. }
  206. }
  207. func testThatJSONResponseSerializerFailsWhenDataIsEmpty() {
  208. // Given
  209. let serializer = Request.JSONResponseSerializer()
  210. // When
  211. let result = serializer.serializeResponse(nil, nil, NSData(), nil)
  212. // Then
  213. XCTAssertTrue(result.isFailure, "result is failure should be true")
  214. XCTAssertNil(result.value, "result value should be nil")
  215. XCTAssertNotNil(result.error, "result error should not be nil")
  216. if let error = result.error {
  217. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  218. XCTAssertEqual(error.code, Error.Code.JSONSerializationFailed.rawValue, "error code should match expected value")
  219. } else {
  220. XCTFail("error should not be nil")
  221. }
  222. }
  223. func testThatJSONResponseSerializerSucceedsWhenDataIsValidJSON() {
  224. // Given
  225. let serializer = Request.stringResponseSerializer()
  226. let data = "{\"json\": true}".dataUsingEncoding(NSUTF8StringEncoding)!
  227. // When
  228. let result = serializer.serializeResponse(nil, nil, data, nil)
  229. // Then
  230. XCTAssertTrue(result.isSuccess, "result is success should be true")
  231. XCTAssertNotNil(result.value, "result value should not be nil")
  232. XCTAssertNil(result.error, "result error should be nil")
  233. }
  234. func testThatJSONResponseSerializerFailsWhenDataIsInvalidJSON() {
  235. // Given
  236. let serializer = Request.JSONResponseSerializer()
  237. let data = "definitely not valid json".dataUsingEncoding(NSUTF8StringEncoding)!
  238. // When
  239. let result = serializer.serializeResponse(nil, nil, data, nil)
  240. // Then
  241. XCTAssertTrue(result.isFailure, "result is failure should be true")
  242. XCTAssertNil(result.value, "result value should be nil")
  243. XCTAssertNotNil(result.error, "result error should not be nil")
  244. if let error = result.error {
  245. XCTAssertEqual(error.domain, NSCocoaErrorDomain, "error domain should match expected value")
  246. XCTAssertEqual(error.code, 3840, "error code should match expected value")
  247. } else {
  248. XCTFail("error should not be nil")
  249. }
  250. }
  251. func testThatJSONResponseSerializerFailsWhenErrorIsNotNil() {
  252. // Given
  253. let serializer = Request.JSONResponseSerializer()
  254. // When
  255. let result = serializer.serializeResponse(nil, nil, nil, error)
  256. // Then
  257. XCTAssertTrue(result.isFailure, "result is failure should be true")
  258. XCTAssertNil(result.value, "result value should be nil")
  259. XCTAssertNotNil(result.error, "result error should not be nil")
  260. if let error = result.error {
  261. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  262. XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
  263. } else {
  264. XCTFail("error should not be nil")
  265. }
  266. }
  267. // MARK: - Property List Response Serializer Tests
  268. func testThatPropertyListResponseSerializerFailsWhenDataIsNil() {
  269. // Given
  270. let serializer = Request.propertyListResponseSerializer()
  271. // When
  272. let result = serializer.serializeResponse(nil, nil, nil, nil)
  273. // Then
  274. XCTAssertTrue(result.isFailure, "result is failure should be true")
  275. XCTAssertNil(result.value, "result value should be nil")
  276. XCTAssertNotNil(result.error, "result error should not be nil")
  277. if let error = result.error {
  278. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  279. XCTAssertEqual(error.code, Error.Code.PropertyListSerializationFailed.rawValue, "error code should match expected value")
  280. } else {
  281. XCTFail("error should not be nil")
  282. }
  283. }
  284. func testThatPropertyListResponseSerializerFailsWhenDataIsEmpty() {
  285. // Given
  286. let serializer = Request.propertyListResponseSerializer()
  287. // When
  288. let result = serializer.serializeResponse(nil, nil, NSData(), nil)
  289. // Then
  290. XCTAssertTrue(result.isFailure, "result is failure should be true")
  291. XCTAssertNil(result.value, "result value should be nil")
  292. XCTAssertNotNil(result.error, "result error should not be nil")
  293. if let error = result.error {
  294. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  295. XCTAssertEqual(error.code, Error.Code.PropertyListSerializationFailed.rawValue, "error code should match expected value")
  296. } else {
  297. XCTFail("error should not be nil")
  298. }
  299. }
  300. func testThatPropertyListResponseSerializerSucceedsWhenDataIsValidPropertyListData() {
  301. // Given
  302. let serializer = Request.propertyListResponseSerializer()
  303. let data = NSKeyedArchiver.archivedDataWithRootObject(["foo": "bar"])
  304. // When
  305. let result = serializer.serializeResponse(nil, nil, data, nil)
  306. // Then
  307. XCTAssertTrue(result.isSuccess, "result is success should be true")
  308. XCTAssertNotNil(result.value, "result value should not be nil")
  309. XCTAssertNil(result.error, "result error should be nil")
  310. }
  311. func testThatPropertyListResponseSerializerFailsWhenDataIsInvalidPropertyListData() {
  312. // Given
  313. let serializer = Request.propertyListResponseSerializer()
  314. let data = "definitely not valid plist data".dataUsingEncoding(NSUTF8StringEncoding)!
  315. // When
  316. let result = serializer.serializeResponse(nil, nil, data, nil)
  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 {
  322. XCTAssertEqual(error.domain, NSCocoaErrorDomain, "error domain should match expected value")
  323. XCTAssertEqual(error.code, 3840, "error code should match expected value")
  324. } else {
  325. XCTFail("error should not be nil")
  326. }
  327. }
  328. func testThatPropertyListResponseSerializerFailsWhenErrorIsNotNil() {
  329. // Given
  330. let serializer = Request.propertyListResponseSerializer()
  331. // When
  332. let result = serializer.serializeResponse(nil, nil, nil, error)
  333. // Then
  334. XCTAssertTrue(result.isFailure, "result is failure should be true")
  335. XCTAssertNil(result.value, "result value should be nil")
  336. XCTAssertNotNil(result.error, "result error should not be nil")
  337. if let error = result.error {
  338. XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
  339. XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
  340. } else {
  341. XCTFail("error should not be nil")
  342. }
  343. }
  344. }