ResponseTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // ResponseTests.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 JSONResponseTestCase: BaseTestCase {
  26. func testGETRequestJSONResponse() {
  27. // Given
  28. let URL = "http://httpbin.org/get"
  29. let expectation = expectationWithDescription("\(URL)")
  30. var request: NSURLRequest?
  31. var response: NSHTTPURLResponse?
  32. var JSON: AnyObject?
  33. var error: NSError?
  34. // When
  35. Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  36. .responseJSON { responseRequest, responseResponse, responseJSON, responseError in
  37. request = responseRequest
  38. response = responseResponse
  39. JSON = responseJSON
  40. error = responseError
  41. expectation.fulfill()
  42. }
  43. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  44. // Then
  45. XCTAssertNotNil(request, "request should not be nil")
  46. XCTAssertNotNil(response, "response should not be nil")
  47. XCTAssertNotNil(JSON, "JSON should not be nil")
  48. XCTAssertNil(error, "error should be nil")
  49. if let args = JSON?["args"] as? NSObject {
  50. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  51. } else {
  52. XCTFail("args should not be nil")
  53. }
  54. }
  55. func testPOSTRequestJSONResponse() {
  56. // Given
  57. let URL = "http://httpbin.org/post"
  58. let expectation = expectationWithDescription("\(URL)")
  59. var request: NSURLRequest?
  60. var response: NSHTTPURLResponse?
  61. var JSON: AnyObject?
  62. var error: NSError?
  63. // When
  64. Alamofire.request(.POST, URL, parameters: ["foo": "bar"])
  65. .responseJSON { responseRequest, responseResponse, responseJSON, responseError in
  66. request = responseRequest
  67. response = responseResponse
  68. JSON = responseJSON
  69. error = responseError
  70. expectation.fulfill()
  71. }
  72. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  73. // Then
  74. XCTAssertNotNil(request, "request should not be nil")
  75. XCTAssertNotNil(response, "response should not be nil")
  76. XCTAssertNotNil(JSON, "JSON should not be nil")
  77. XCTAssertNil(error, "error should be nil")
  78. if let form = JSON?["form"] as? NSObject {
  79. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  80. } else {
  81. XCTFail("form should not be nil")
  82. }
  83. }
  84. }
  85. // MARK: -
  86. class RedirectResponseTestCase: BaseTestCase {
  87. func testThatRequestWillPerformHTTPRedirectionByDefault() {
  88. // Given
  89. let redirectURLString = "http://www.apple.com"
  90. let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"
  91. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  92. var request: NSURLRequest?
  93. var response: NSHTTPURLResponse?
  94. var data: AnyObject?
  95. var error: NSError?
  96. // When
  97. Alamofire.request(.GET, URLString)
  98. .response { responseRequest, responseResponse, responseData, responseError in
  99. request = responseRequest
  100. response = responseResponse
  101. data = responseData
  102. error = responseError
  103. expectation.fulfill()
  104. }
  105. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  106. // Then
  107. XCTAssertNotNil(request, "request should not be nil")
  108. XCTAssertNotNil(response, "response should not be nil")
  109. XCTAssertNotNil(data, "data should not be nil")
  110. XCTAssertNil(error, "error should be nil")
  111. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  112. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  113. }
  114. func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
  115. // Given
  116. let redirectURLString = "http://httpbin.org/get"
  117. let URLString = "http://httpbin.org/redirect/5"
  118. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  119. var request: NSURLRequest?
  120. var response: NSHTTPURLResponse?
  121. var data: AnyObject?
  122. var error: NSError?
  123. // When
  124. Alamofire.request(.GET, URLString)
  125. .response { responseRequest, responseResponse, responseData, responseError in
  126. request = responseRequest
  127. response = responseResponse
  128. data = responseData
  129. error = responseError
  130. expectation.fulfill()
  131. }
  132. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  133. // Then
  134. XCTAssertNotNil(request, "request should not be nil")
  135. XCTAssertNotNil(response, "response should not be nil")
  136. XCTAssertNotNil(data, "data should not be nil")
  137. XCTAssertNil(error, "error should be nil")
  138. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  139. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  140. }
  141. func testThatTaskOverrideClosureCanPerformHTTPRedirection() {
  142. // Given
  143. let redirectURLString = "http://www.apple.com"
  144. let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"
  145. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  146. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  147. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  148. return request
  149. }
  150. var request: NSURLRequest?
  151. var response: NSHTTPURLResponse?
  152. var data: AnyObject?
  153. var error: NSError?
  154. // When
  155. Alamofire.request(.GET, URLString)
  156. .response { responseRequest, responseResponse, responseData, responseError in
  157. request = responseRequest
  158. response = responseResponse
  159. data = responseData
  160. error = responseError
  161. expectation.fulfill()
  162. }
  163. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  164. // Then
  165. XCTAssertNotNil(request, "request should not be nil")
  166. XCTAssertNotNil(response, "response should not be nil")
  167. XCTAssertNotNil(data, "data should not be nil")
  168. XCTAssertNil(error, "error should be nil")
  169. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  170. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  171. }
  172. func testThatTaskOverrideClosureCanCancelHTTPRedirection() {
  173. // Given
  174. let redirectURLString = "http://www.apple.com"
  175. let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"
  176. let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
  177. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  178. delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in
  179. return nil
  180. }
  181. var request: NSURLRequest?
  182. var response: NSHTTPURLResponse?
  183. var data: AnyObject?
  184. var error: NSError?
  185. // When
  186. Alamofire.request(.GET, URLString)
  187. .response { responseRequest, responseResponse, responseData, responseError in
  188. request = responseRequest
  189. response = responseResponse
  190. data = responseData
  191. error = responseError
  192. expectation.fulfill()
  193. }
  194. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  195. // Then
  196. XCTAssertNotNil(request, "request should not be nil")
  197. XCTAssertNotNil(response, "response should not be nil")
  198. XCTAssertNotNil(data, "data should not be nil")
  199. XCTAssertNil(error, "error should be nil")
  200. XCTAssertEqual(response?.URL?.URLString ?? "", URLString, "response URL should match the origin URL")
  201. XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
  202. }
  203. func testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects() {
  204. // Given
  205. let redirectURLString = "http://httpbin.org/get"
  206. let URLString = "http://httpbin.org/redirect/5"
  207. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  208. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  209. var totalRedirectCount = 0
  210. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  211. ++totalRedirectCount
  212. return request
  213. }
  214. var request: NSURLRequest?
  215. var response: NSHTTPURLResponse?
  216. var data: AnyObject?
  217. var error: NSError?
  218. // When
  219. Alamofire.request(.GET, URLString)
  220. .response { responseRequest, responseResponse, responseData, responseError in
  221. request = responseRequest
  222. response = responseResponse
  223. data = responseData
  224. error = responseError
  225. expectation.fulfill()
  226. }
  227. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  228. // Then
  229. XCTAssertNotNil(request, "request should not be nil")
  230. XCTAssertNotNil(response, "response should not be nil")
  231. XCTAssertNotNil(data, "data should not be nil")
  232. XCTAssertNil(error, "error should be nil")
  233. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  234. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  235. XCTAssertEqual(totalRedirectCount, 5, "total redirect count should be 5")
  236. }
  237. }