ValidationTests.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 = "https://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 = "https://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. if let error = error {
  58. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  59. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  60. }
  61. }
  62. func testThatValidationForRequestWithNoAcceptableStatusCodesFails() {
  63. // Given
  64. let URLString = "https://httpbin.org/status/201"
  65. let expectation = expectationWithDescription("request should return 201 status code")
  66. var error: NSError?
  67. // When
  68. Alamofire.request(.GET, URLString)
  69. .validate(statusCode: [])
  70. .response { _, _, _, responseError in
  71. error = responseError
  72. expectation.fulfill()
  73. }
  74. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  75. // Then
  76. XCTAssertNotNil(error, "error should not be nil")
  77. if let error = error {
  78. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  79. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  80. }
  81. }
  82. }
  83. // MARK: -
  84. class ContentTypeValidationTestCase: BaseTestCase {
  85. func testThatValidationForRequestWithAcceptableContentTypeResponseSucceeds() {
  86. // Given
  87. let URLString = "https://httpbin.org/ip"
  88. let expectation = expectationWithDescription("request should succeed and return ip")
  89. var error: NSError?
  90. // When
  91. Alamofire.request(.GET, URLString)
  92. .validate(contentType: ["application/json"])
  93. .response { _, _, _, responseError in
  94. error = responseError
  95. expectation.fulfill()
  96. }
  97. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  98. // Then
  99. XCTAssertNil(error, "error should be nil")
  100. }
  101. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds() {
  102. // Given
  103. let URLString = "https://httpbin.org/ip"
  104. let expectation = expectationWithDescription("request should succeed and return ip")
  105. var error: NSError?
  106. // When
  107. Alamofire.request(.GET, URLString)
  108. .validate(contentType: ["*/*"])
  109. .validate(contentType: ["application/*"])
  110. .validate(contentType: ["*/json"])
  111. .response { _, _, _, responseError in
  112. error = responseError
  113. expectation.fulfill()
  114. }
  115. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  116. // Then
  117. XCTAssertNil(error, "error should be nil")
  118. }
  119. func testThatValidationForRequestWithUnacceptableContentTypeResponseFails() {
  120. // Given
  121. let URLString = "https://httpbin.org/xml"
  122. let expectation = expectationWithDescription("request should succeed and return xml")
  123. var error: NSError?
  124. // When
  125. Alamofire.request(.GET, URLString)
  126. .validate(contentType: ["application/octet-stream"])
  127. .response { _, _, _, responseError in
  128. error = responseError
  129. expectation.fulfill()
  130. }
  131. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  132. // Then
  133. XCTAssertNotNil(error, "error should not be nil")
  134. if let error = error {
  135. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  136. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  137. }
  138. }
  139. func testThatValidationForRequestWithNoAcceptableContentTypeResponseFails() {
  140. // Given
  141. let URLString = "https://httpbin.org/xml"
  142. let expectation = expectationWithDescription("request should succeed and return xml")
  143. var error: NSError?
  144. // When
  145. Alamofire.request(.GET, URLString)
  146. .validate(contentType: [])
  147. .response { _, _, _, responseError in
  148. error = responseError
  149. expectation.fulfill()
  150. }
  151. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  152. // Then
  153. XCTAssertNotNil(error, "error should not be nil")
  154. if let error = error {
  155. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  156. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  157. }
  158. }
  159. }
  160. // MARK: -
  161. class MultipleValidationTestCase: BaseTestCase {
  162. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  163. // Given
  164. let URLString = "https://httpbin.org/ip"
  165. let expectation = expectationWithDescription("request should succeed and return ip")
  166. var error: NSError?
  167. // When
  168. Alamofire.request(.GET, URLString)
  169. .validate(statusCode: 200..<300)
  170. .validate(contentType: ["application/json"])
  171. .response { _, _, _, responseError in
  172. error = responseError
  173. expectation.fulfill()
  174. }
  175. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  176. // Then
  177. XCTAssertNil(error, "error should be nil")
  178. }
  179. func testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFailsWithStatusCodeError() {
  180. // Given
  181. let URLString = "https://httpbin.org/xml"
  182. let expectation = expectationWithDescription("request should succeed and return xml")
  183. var error: NSError?
  184. // When
  185. Alamofire.request(.GET, URLString)
  186. .validate(statusCode: 400..<600)
  187. .validate(contentType: ["application/octet-stream"])
  188. .response { _, _, _, responseError in
  189. error = responseError
  190. expectation.fulfill()
  191. }
  192. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  193. // Then
  194. XCTAssertNotNil(error, "error should not be nil")
  195. if let error = error {
  196. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  197. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  198. }
  199. }
  200. func testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFailsWithContentTypeError() {
  201. // Given
  202. let URLString = "https://httpbin.org/xml"
  203. let expectation = expectationWithDescription("request should succeed and return xml")
  204. var error: NSError?
  205. // When
  206. Alamofire.request(.GET, URLString)
  207. .validate(contentType: ["application/octet-stream"])
  208. .validate(statusCode: 400..<600)
  209. .response { _, _, _, responseError in
  210. error = responseError
  211. expectation.fulfill()
  212. }
  213. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  214. // Then
  215. XCTAssertNotNil(error, "error should not be nil")
  216. if let error = error {
  217. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  218. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  219. }
  220. }
  221. }
  222. // MARK: -
  223. class AutomaticValidationTestCase: BaseTestCase {
  224. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  225. // Given
  226. let URL = NSURL(string: "https://httpbin.org/ip")!
  227. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  228. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  229. let expectation = expectationWithDescription("request should succeed and return ip")
  230. var error: NSError?
  231. // When
  232. Alamofire.request(mutableURLRequest)
  233. .validate()
  234. .response { _, _, _, responseError in
  235. error = responseError
  236. expectation.fulfill()
  237. }
  238. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  239. // Then
  240. XCTAssertNil(error, "error should be nil")
  241. }
  242. func testThatValidationForRequestWithUnacceptableStatusCodeResponseFails() {
  243. // Given
  244. let URLString = "https://httpbin.org/status/404"
  245. let expectation = expectationWithDescription("request should return 404 status code")
  246. var error: NSError?
  247. // When
  248. Alamofire.request(.GET, URLString)
  249. .validate()
  250. .response { _, _, _, responseError in
  251. error = responseError
  252. expectation.fulfill()
  253. }
  254. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  255. // Then
  256. XCTAssertNotNil(error, "error should not be nil")
  257. if let error = error {
  258. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  259. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  260. }
  261. }
  262. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds() {
  263. // Given
  264. let URL = NSURL(string: "https://httpbin.org/ip")!
  265. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  266. mutableURLRequest.setValue("application/*", forHTTPHeaderField: "Accept")
  267. let expectation = expectationWithDescription("request should succeed and return ip")
  268. var error: NSError?
  269. // When
  270. Alamofire.request(mutableURLRequest)
  271. .validate()
  272. .response { _, _, _, responseError in
  273. error = responseError
  274. expectation.fulfill()
  275. }
  276. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  277. // Then
  278. XCTAssertNil(error, "error should be nil")
  279. }
  280. func testThatValidationForRequestWithAcceptableComplexContentTypeResponseSucceeds() {
  281. // Given
  282. let URL = NSURL(string: "https://httpbin.org/xml")!
  283. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  284. let headerValue = "text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8,*/*;q=0.5"
  285. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: "Accept")
  286. let expectation = expectationWithDescription("request should succeed and return xml")
  287. var error: NSError?
  288. // When
  289. Alamofire.request(mutableURLRequest)
  290. .validate()
  291. .response { _, _, _, responseError in
  292. error = responseError
  293. expectation.fulfill()
  294. }
  295. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  296. // Then
  297. XCTAssertNil(error, "error should be nil")
  298. }
  299. func testThatValidationForRequestWithUnacceptableContentTypeResponseFails() {
  300. // Given
  301. let URL = NSURL(string: "https://httpbin.org/xml")!
  302. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  303. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  304. let expectation = expectationWithDescription("request should succeed and return xml")
  305. var error: NSError?
  306. // When
  307. Alamofire.request(mutableURLRequest)
  308. .validate()
  309. .response { _, _, _, responseError in
  310. error = responseError
  311. expectation.fulfill()
  312. }
  313. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  314. // Then
  315. XCTAssertNotNil(error, "error should not be nil")
  316. if let error = error {
  317. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  318. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  319. }
  320. }
  321. }