ResponseTests.swift 10 KB

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