ResponseTests.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // ResponseTests.swift
  2. //
  3. // Copyright (c) 2014–2016 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 ResponseDataTestCase: BaseTestCase {
  26. func testThatResponseDataReturnsSuccessResultWithValidData() {
  27. // Given
  28. let URLString = "https://httpbin.org/get"
  29. let expectation = expectationWithDescription("request should succeed")
  30. var response: Response<NSData, NSError>?
  31. // When
  32. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  33. .responseData { closureResponse in
  34. response = closureResponse
  35. expectation.fulfill()
  36. }
  37. waitForExpectationsWithTimeout(timeout, handler: nil)
  38. // Then
  39. if let response = response {
  40. XCTAssertNotNil(response.request, "request should not be nil")
  41. XCTAssertNotNil(response.response, "response should not be nil")
  42. XCTAssertNotNil(response.data, "data should not be nil")
  43. XCTAssertTrue(response.result.isSuccess, "result should be success")
  44. } else {
  45. XCTFail("response should not be nil")
  46. }
  47. }
  48. func testThatResponseDataReturnsFailureResultWithOptionalDataAndError() {
  49. // Given
  50. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  51. let expectation = expectationWithDescription("request should fail with 404")
  52. var response: Response<NSData, NSError>?
  53. // When
  54. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  55. .responseData { closureResponse in
  56. response = closureResponse
  57. expectation.fulfill()
  58. }
  59. waitForExpectationsWithTimeout(timeout, handler: nil)
  60. // Then
  61. if let response = response {
  62. XCTAssertNotNil(response.request, "request should not be nil")
  63. XCTAssertNil(response.response, "response should be nil")
  64. XCTAssertNotNil(response.data, "data should not be nil")
  65. XCTAssertTrue(response.result.isFailure, "result should be failure")
  66. } else {
  67. XCTFail("response should not be nil")
  68. }
  69. }
  70. }
  71. // MARK: -
  72. class ResponseStringTestCase: BaseTestCase {
  73. func testThatResponseStringReturnsSuccessResultWithValidString() {
  74. // Given
  75. let URLString = "https://httpbin.org/get"
  76. let expectation = expectationWithDescription("request should succeed")
  77. var response: Response<String, NSError>?
  78. // When
  79. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  80. .responseString { closureResponse in
  81. response = closureResponse
  82. expectation.fulfill()
  83. }
  84. waitForExpectationsWithTimeout(timeout, handler: nil)
  85. // Then
  86. if let response = response {
  87. XCTAssertNotNil(response.request, "request should not be nil")
  88. XCTAssertNotNil(response.response, "response should not be nil")
  89. XCTAssertNotNil(response.data, "data should not be nil")
  90. XCTAssertTrue(response.result.isSuccess, "result should be success")
  91. } else {
  92. XCTFail("response should not be nil")
  93. }
  94. }
  95. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  96. // Given
  97. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  98. let expectation = expectationWithDescription("request should fail with 404")
  99. var response: Response<String, NSError>?
  100. // When
  101. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  102. .responseString { closureResponse in
  103. response = closureResponse
  104. expectation.fulfill()
  105. }
  106. waitForExpectationsWithTimeout(timeout, handler: nil)
  107. // Then
  108. if let response = response {
  109. XCTAssertNotNil(response.request, "request should not be nil")
  110. XCTAssertNil(response.response, "response should be nil")
  111. XCTAssertNotNil(response.data, "data should not be nil")
  112. XCTAssertTrue(response.result.isFailure, "result should be failure")
  113. } else {
  114. XCTFail("response should not be nil")
  115. }
  116. }
  117. }
  118. // MARK: -
  119. class ResponseJSONTestCase: BaseTestCase {
  120. func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
  121. // Given
  122. let URLString = "https://httpbin.org/get"
  123. let expectation = expectationWithDescription("request should succeed")
  124. var response: Response<AnyObject, NSError>?
  125. // When
  126. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  127. .responseJSON { closureResponse in
  128. response = closureResponse
  129. expectation.fulfill()
  130. }
  131. waitForExpectationsWithTimeout(timeout, handler: nil)
  132. // Then
  133. if let response = response {
  134. XCTAssertNotNil(response.request, "request should not be nil")
  135. XCTAssertNotNil(response.response, "response should not be nil")
  136. XCTAssertNotNil(response.data, "data should not be nil")
  137. XCTAssertTrue(response.result.isSuccess, "result should be success")
  138. } else {
  139. XCTFail("response should not be nil")
  140. }
  141. }
  142. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  143. // Given
  144. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  145. let expectation = expectationWithDescription("request should fail with 404")
  146. var response: Response<AnyObject, NSError>?
  147. // When
  148. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  149. .responseJSON { closureResponse in
  150. response = closureResponse
  151. expectation.fulfill()
  152. }
  153. waitForExpectationsWithTimeout(timeout, handler: nil)
  154. // Then
  155. if let response = response {
  156. XCTAssertNotNil(response.request, "request should not be nil")
  157. XCTAssertNil(response.response, "response should be nil")
  158. XCTAssertNotNil(response.data, "data should not be nil")
  159. XCTAssertTrue(response.result.isFailure, "result should be failure")
  160. } else {
  161. XCTFail("response should not be nil")
  162. }
  163. }
  164. func testThatResponseJSONReturnsSuccessResultForGETRequest() {
  165. // Given
  166. let URLString = "https://httpbin.org/get"
  167. let expectation = expectationWithDescription("request should succeed")
  168. var response: Response<AnyObject, NSError>?
  169. // When
  170. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  171. .responseJSON { closureResponse in
  172. response = closureResponse
  173. expectation.fulfill()
  174. }
  175. waitForExpectationsWithTimeout(timeout, handler: nil)
  176. // Then
  177. if let response = response {
  178. XCTAssertNotNil(response.request, "request should not be nil")
  179. XCTAssertNotNil(response.response, "response should not be nil")
  180. XCTAssertNotNil(response.data, "data should not be nil")
  181. XCTAssertTrue(response.result.isSuccess, "result should be success")
  182. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  183. // - https://openradar.appspot.com/radar?id=5517037090635776
  184. if let args = response.result.value?["args" as NSString] as? [String: String] {
  185. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  186. } else {
  187. XCTFail("args should not be nil")
  188. }
  189. } else {
  190. XCTFail("response should not be nil")
  191. }
  192. }
  193. func testThatResponseJSONReturnsSuccessResultForPOSTRequest() {
  194. // Given
  195. let URLString = "https://httpbin.org/post"
  196. let expectation = expectationWithDescription("request should succeed")
  197. var response: Response<AnyObject, NSError>?
  198. // When
  199. Alamofire.request(.POST, URLString, parameters: ["foo": "bar"])
  200. .responseJSON { closureResponse in
  201. response = closureResponse
  202. expectation.fulfill()
  203. }
  204. waitForExpectationsWithTimeout(timeout, handler: nil)
  205. // Then
  206. if let response = response {
  207. XCTAssertNotNil(response.request, "request should not be nil")
  208. XCTAssertNotNil(response.response, "response should not be nil")
  209. XCTAssertNotNil(response.data, "data should not be nil")
  210. XCTAssertTrue(response.result.isSuccess, "result should be success")
  211. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  212. // - https://openradar.appspot.com/radar?id=5517037090635776
  213. if let form = response.result.value?["form" as NSString] as? [String: String] {
  214. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  215. } else {
  216. XCTFail("form should not be nil")
  217. }
  218. } else {
  219. XCTFail("response should not be nil")
  220. }
  221. }
  222. }