ValidationTests.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. @testable 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. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceedsWhenResponseIsNil() {
  160. // Given
  161. class MockManager: Manager {
  162. override func request(URLRequest: URLRequestConvertible) -> Request {
  163. var dataTask: NSURLSessionDataTask!
  164. dispatch_sync(queue) {
  165. dataTask = self.session.dataTaskWithRequest(URLRequest.URLRequest)
  166. }
  167. let request = MockRequest(session: session, task: dataTask)
  168. delegate[request.delegate.task] = request.delegate
  169. if startRequestsImmediately {
  170. request.resume()
  171. }
  172. return request
  173. }
  174. }
  175. class MockRequest: Request {
  176. override var response: NSHTTPURLResponse? {
  177. return MockHTTPURLResponse(
  178. URL: NSURL(string: request!.URLString)!,
  179. statusCode: 204,
  180. HTTPVersion: "HTTP/1.1",
  181. headerFields: nil
  182. )
  183. }
  184. }
  185. class MockHTTPURLResponse: NSHTTPURLResponse {
  186. override var MIMEType: String? { return nil }
  187. }
  188. let manager: Manager = {
  189. let configuration: NSURLSessionConfiguration = {
  190. let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
  191. configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
  192. return configuration
  193. }()
  194. return MockManager(configuration: configuration)
  195. }()
  196. let URLString = "https://httpbin.org/delete"
  197. let expectation = expectationWithDescription("request should be stubbed and return 204 status code")
  198. var response: NSHTTPURLResponse?
  199. var data: NSData?
  200. var error: NSError?
  201. // When
  202. manager.request(.DELETE, URLString)
  203. .validate(contentType: ["*/*"])
  204. .response { _, responseResponse, responseData, responseError in
  205. response = responseResponse
  206. data = responseData
  207. error = responseError
  208. expectation.fulfill()
  209. }
  210. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  211. // Then
  212. XCTAssertNotNil(response, "response should not be nil")
  213. XCTAssertNotNil(data, "data should not be nil")
  214. XCTAssertNil(error, "error should be nil")
  215. if let response = response {
  216. XCTAssertEqual(response.statusCode, 204, "response status code should be 204")
  217. XCTAssertNil(response.MIMEType, "response mime type should be nil")
  218. }
  219. }
  220. }
  221. // MARK: -
  222. class MultipleValidationTestCase: BaseTestCase {
  223. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  224. // Given
  225. let URLString = "https://httpbin.org/ip"
  226. let expectation = expectationWithDescription("request should succeed and return ip")
  227. var error: NSError?
  228. // When
  229. Alamofire.request(.GET, URLString)
  230. .validate(statusCode: 200..<300)
  231. .validate(contentType: ["application/json"])
  232. .response { _, _, _, responseError in
  233. error = responseError
  234. expectation.fulfill()
  235. }
  236. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  237. // Then
  238. XCTAssertNil(error, "error should be nil")
  239. }
  240. func testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFailsWithStatusCodeError() {
  241. // Given
  242. let URLString = "https://httpbin.org/xml"
  243. let expectation = expectationWithDescription("request should succeed and return xml")
  244. var error: NSError?
  245. // When
  246. Alamofire.request(.GET, URLString)
  247. .validate(statusCode: 400..<600)
  248. .validate(contentType: ["application/octet-stream"])
  249. .response { _, _, _, responseError in
  250. error = responseError
  251. expectation.fulfill()
  252. }
  253. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  254. // Then
  255. XCTAssertNotNil(error, "error should not be nil")
  256. if let error = error {
  257. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  258. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  259. }
  260. }
  261. func testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFailsWithContentTypeError() {
  262. // Given
  263. let URLString = "https://httpbin.org/xml"
  264. let expectation = expectationWithDescription("request should succeed and return xml")
  265. var error: NSError?
  266. // When
  267. Alamofire.request(.GET, URLString)
  268. .validate(contentType: ["application/octet-stream"])
  269. .validate(statusCode: 400..<600)
  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. if let error = error {
  278. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  279. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  280. }
  281. }
  282. }
  283. // MARK: -
  284. class AutomaticValidationTestCase: BaseTestCase {
  285. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  286. // Given
  287. let URL = NSURL(string: "https://httpbin.org/ip")!
  288. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  289. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  290. let expectation = expectationWithDescription("request should succeed and return ip")
  291. var error: NSError?
  292. // When
  293. Alamofire.request(mutableURLRequest)
  294. .validate()
  295. .response { _, _, _, responseError in
  296. error = responseError
  297. expectation.fulfill()
  298. }
  299. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  300. // Then
  301. XCTAssertNil(error, "error should be nil")
  302. }
  303. func testThatValidationForRequestWithUnacceptableStatusCodeResponseFails() {
  304. // Given
  305. let URLString = "https://httpbin.org/status/404"
  306. let expectation = expectationWithDescription("request should return 404 status code")
  307. var error: NSError?
  308. // When
  309. Alamofire.request(.GET, URLString)
  310. .validate()
  311. .response { _, _, _, responseError in
  312. error = responseError
  313. expectation.fulfill()
  314. }
  315. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  316. // Then
  317. XCTAssertNotNil(error, "error should not be nil")
  318. if let error = error {
  319. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  320. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  321. }
  322. }
  323. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds() {
  324. // Given
  325. let URL = NSURL(string: "https://httpbin.org/ip")!
  326. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  327. mutableURLRequest.setValue("application/*", forHTTPHeaderField: "Accept")
  328. let expectation = expectationWithDescription("request should succeed and return ip")
  329. var error: NSError?
  330. // When
  331. Alamofire.request(mutableURLRequest)
  332. .validate()
  333. .response { _, _, _, responseError in
  334. error = responseError
  335. expectation.fulfill()
  336. }
  337. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  338. // Then
  339. XCTAssertNil(error, "error should be nil")
  340. }
  341. func testThatValidationForRequestWithAcceptableComplexContentTypeResponseSucceeds() {
  342. // Given
  343. let URL = NSURL(string: "https://httpbin.org/xml")!
  344. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  345. let headerValue = "text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8,*/*;q=0.5"
  346. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: "Accept")
  347. let expectation = expectationWithDescription("request should succeed and return xml")
  348. var error: NSError?
  349. // When
  350. Alamofire.request(mutableURLRequest)
  351. .validate()
  352. .response { _, _, _, responseError in
  353. error = responseError
  354. expectation.fulfill()
  355. }
  356. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  357. // Then
  358. XCTAssertNil(error, "error should be nil")
  359. }
  360. func testThatValidationForRequestWithUnacceptableContentTypeResponseFails() {
  361. // Given
  362. let URL = NSURL(string: "https://httpbin.org/xml")!
  363. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  364. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  365. let expectation = expectationWithDescription("request should succeed and return xml")
  366. var error: NSError?
  367. // When
  368. Alamofire.request(mutableURLRequest)
  369. .validate()
  370. .response { _, _, _, responseError in
  371. error = responseError
  372. expectation.fulfill()
  373. }
  374. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  375. // Then
  376. XCTAssertNotNil(error, "error should not be nil")
  377. if let error = error {
  378. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  379. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  380. }
  381. }
  382. }