ResponseSerializationTests.swift 14 KB

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