ValidationTests.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // ValidationTests.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 StatusCodeValidationTestCase: BaseTestCase {
  26. func testThatValidationForRequestWithAcceptableStatusCodeResponseSucceeds() {
  27. // Given
  28. let URLString = "http://httpbin.org/status/200"
  29. let expectation = expectationWithDescription("request should return 200 status code")
  30. var error: NSError?
  31. // When
  32. Alamofire.request(.GET, URLString)
  33. .validate(statusCode: 200..<300)
  34. .response { _, _, _, responseError in
  35. error = responseError
  36. expectation.fulfill()
  37. }
  38. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  39. // Then
  40. XCTAssertNil(error, "error should be nil")
  41. }
  42. func testThatValidationForRequestWithUnacceptableStatusCodeResponseFails() {
  43. // Given
  44. let URLString = "http://httpbin.org/status/404"
  45. let expectation = expectationWithDescription("request should return 404 status code")
  46. var error: NSError?
  47. // When
  48. Alamofire.request(.GET, URLString)
  49. .validate(statusCode: [200])
  50. .response { _, _, _, responseError in
  51. error = responseError
  52. expectation.fulfill()
  53. }
  54. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  55. // Then
  56. XCTAssertNotNil(error, "error should not be nil")
  57. XCTAssertEqual(error?.domain ?? "", AlamofireErrorDomain, "error should be in Alamofire error domain")
  58. }
  59. func testThatValidationForRequestWithNoAcceptableStatusCodesFails() {
  60. // Given
  61. let URLString = "http://httpbin.org/status/201"
  62. let expectation = expectationWithDescription("request should return 201 status code")
  63. var error: NSError?
  64. // When
  65. Alamofire.request(.GET, URLString)
  66. .validate(statusCode: [])
  67. .response { _, _, _, responseError in
  68. error = responseError
  69. expectation.fulfill()
  70. }
  71. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  72. // Then
  73. XCTAssertNotNil(error, "error should not be nil")
  74. XCTAssertEqual(error?.domain ?? "", AlamofireErrorDomain, "error should be in Alamofire error domain")
  75. }
  76. }
  77. // MARK: -
  78. class ContentTypeValidationTestCase: BaseTestCase {
  79. func testThatValidationForRequestWithAcceptableContentTypeResponseSucceeds() {
  80. // Given
  81. let URLString = "http://httpbin.org/ip"
  82. let expectation = expectationWithDescription("request should succeed and return ip")
  83. var error: NSError?
  84. // When
  85. Alamofire.request(.GET, URLString)
  86. .validate(contentType: ["application/json"])
  87. .response { _, _, _, responseError in
  88. error = responseError
  89. expectation.fulfill()
  90. }
  91. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  92. // Then
  93. XCTAssertNil(error, "error should be nil")
  94. }
  95. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds() {
  96. // Given
  97. let URLString = "http://httpbin.org/ip"
  98. let expectation = expectationWithDescription("request should succeed and return ip")
  99. var error: NSError?
  100. // When
  101. Alamofire.request(.GET, URLString)
  102. .validate(contentType: ["*/*"])
  103. .validate(contentType: ["application/*"])
  104. .validate(contentType: ["*/json"])
  105. .response { _, _, _, responseError in
  106. error = responseError
  107. expectation.fulfill()
  108. }
  109. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  110. // Then
  111. XCTAssertNil(error, "error should be nil")
  112. }
  113. func testThatValidationForRequestWithUnacceptableContentTypeResponseFails() {
  114. // Given
  115. let URLString = "http://httpbin.org/xml"
  116. let expectation = expectationWithDescription("request should succeed and return xml")
  117. var error: NSError?
  118. // When
  119. Alamofire.request(.GET, URLString)
  120. .validate(contentType: ["application/octet-stream"])
  121. .response { _, _, _, responseError in
  122. error = responseError
  123. expectation.fulfill()
  124. }
  125. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  126. // Then
  127. XCTAssertNotNil(error, "error should not be nil")
  128. XCTAssertEqual(error?.domain ?? "", AlamofireErrorDomain, "error should be in Alamofire error domain")
  129. }
  130. func testThatValidationForRequestWithNoAcceptableContentTypeResponseFails() {
  131. // Given
  132. let URLString = "http://httpbin.org/xml"
  133. let expectation = expectationWithDescription("request should succeed and return xml")
  134. var error: NSError?
  135. // When
  136. Alamofire.request(.GET, URLString)
  137. .validate(contentType: [])
  138. .response { _, _, _, responseError in
  139. error = responseError
  140. expectation.fulfill()
  141. }
  142. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  143. // Then
  144. XCTAssertNotNil(error, "error should not be nil")
  145. XCTAssertEqual(error?.domain ?? "", AlamofireErrorDomain, "error should be in Alamofire error domain")
  146. }
  147. }
  148. // MARK: -
  149. class MultipleValidationTestCase: BaseTestCase {
  150. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  151. // Given
  152. let URLString = "http://httpbin.org/ip"
  153. let expectation = expectationWithDescription("request should succeed and return ip")
  154. var error: NSError?
  155. // When
  156. Alamofire.request(.GET, URLString)
  157. .validate(statusCode: 200..<300)
  158. .validate(contentType: ["application/json"])
  159. .response { _, _, _, responseError in
  160. error = responseError
  161. expectation.fulfill()
  162. }
  163. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  164. // Then
  165. XCTAssertNil(error, "error should be nil")
  166. }
  167. func testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFails() {
  168. // Given
  169. let URLString = "http://httpbin.org/xml"
  170. let expectation = expectationWithDescription("request should succeed and return xml")
  171. var error: NSError?
  172. // When
  173. Alamofire.request(.GET, URLString)
  174. .validate(statusCode: 400..<600)
  175. .validate(contentType: ["application/octet-stream"])
  176. .response { _, _, _, responseError in
  177. error = responseError
  178. expectation.fulfill()
  179. }
  180. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  181. // Then
  182. XCTAssertNotNil(error, "error should not be nil")
  183. XCTAssertEqual(error?.domain ?? "", AlamofireErrorDomain, "error should be in Alamofire error domain")
  184. }
  185. }
  186. // MARK: -
  187. class AutomaticValidationTestCase: BaseTestCase {
  188. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  189. // Given
  190. let URL = NSURL(string: "http://httpbin.org/ip")!
  191. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  192. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  193. let expectation = expectationWithDescription("request should succeed and return ip")
  194. var error: NSError?
  195. // When
  196. Alamofire.request(mutableURLRequest)
  197. .validate()
  198. .response { _, _, _, responseError in
  199. error = responseError
  200. expectation.fulfill()
  201. }
  202. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  203. // Then
  204. XCTAssertNil(error, "error should be nil")
  205. }
  206. func testThatValidationForRequestWithUnacceptableStatusCodeResponseFails() {
  207. // Given
  208. let URLString = "http://httpbin.org/status/404"
  209. let expectation = expectationWithDescription("request should return 404 status code")
  210. var error: NSError?
  211. // When
  212. Alamofire.request(.GET, URLString)
  213. .validate()
  214. .response { _, _, _, responseError in
  215. error = responseError
  216. expectation.fulfill()
  217. }
  218. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  219. // Then
  220. XCTAssertNotNil(error, "error should not be nil")
  221. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  222. }
  223. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds() {
  224. // Given
  225. let URL = NSURL(string: "http://httpbin.org/ip")!
  226. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  227. mutableURLRequest.setValue("application/*", forHTTPHeaderField: "Accept")
  228. let expectation = expectationWithDescription("request should succeed and return ip")
  229. var error: NSError?
  230. // When
  231. Alamofire.request(mutableURLRequest)
  232. .validate()
  233. .response { _, _, _, responseError in
  234. error = responseError
  235. expectation.fulfill()
  236. }
  237. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  238. // Then
  239. XCTAssertNil(error, "error should be nil")
  240. }
  241. func testThatValidationForRequestWithAcceptableComplexContentTypeResponseSucceeds() {
  242. // Given
  243. let URL = NSURL(string: "http://httpbin.org/xml")!
  244. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  245. let headerValue = "text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8,*/*;q=0.5"
  246. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: "Accept")
  247. let expectation = expectationWithDescription("request should succeed and return xml")
  248. var error: NSError?
  249. // When
  250. Alamofire.request(mutableURLRequest)
  251. .validate()
  252. .response { _, _, _, responseError in
  253. error = responseError
  254. expectation.fulfill()
  255. }
  256. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  257. // Then
  258. XCTAssertNil(error, "error should be nil")
  259. }
  260. func testThatValidationForRequestWithUnacceptableContentTypeResponseFails() {
  261. // Given
  262. let URL = NSURL(string: "http://httpbin.org/xml")!
  263. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  264. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  265. let expectation = expectationWithDescription("request should succeed and return xml")
  266. var error: NSError?
  267. // When
  268. Alamofire.request(mutableURLRequest)
  269. .validate()
  270. .response { _, _, _, responseError in
  271. error = responseError
  272. expectation.fulfill()
  273. }
  274. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  275. // Then
  276. XCTAssertNotNil(error, "error should not be nil")
  277. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  278. }
  279. }