2
0

ResponseSerializationTests.swift 15 KB

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