ValidationTests.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // ValidationTests.swift
  2. //
  3. // Copyright (c) 2014–2016 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(timeout, 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(timeout, 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. } else {
  61. XCTFail("error should be an NSError")
  62. }
  63. }
  64. func testThatValidationForRequestWithNoAcceptableStatusCodesFails() {
  65. // Given
  66. let URLString = "https://httpbin.org/status/201"
  67. let expectation = expectationWithDescription("request should return 201 status code")
  68. var error: NSError?
  69. // When
  70. Alamofire.request(.GET, URLString)
  71. .validate(statusCode: [])
  72. .response { _, _, _, responseError in
  73. error = responseError
  74. expectation.fulfill()
  75. }
  76. waitForExpectationsWithTimeout(timeout, handler: nil)
  77. // Then
  78. XCTAssertNotNil(error, "error should not be nil")
  79. if let error = error {
  80. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  81. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  82. } else {
  83. XCTFail("error should be an NSError")
  84. }
  85. }
  86. }
  87. // MARK: -
  88. class ContentTypeValidationTestCase: BaseTestCase {
  89. func testThatValidationForRequestWithAcceptableContentTypeResponseSucceeds() {
  90. // Given
  91. let URLString = "https://httpbin.org/ip"
  92. let expectation = expectationWithDescription("request should succeed and return ip")
  93. var error: NSError?
  94. // When
  95. Alamofire.request(.GET, URLString)
  96. .validate(contentType: ["application/json"])
  97. .validate(contentType: ["application/json;charset=utf8"])
  98. .validate(contentType: ["application/json;q=0.8;charset=utf8"])
  99. .response { _, _, _, responseError in
  100. error = responseError
  101. expectation.fulfill()
  102. }
  103. waitForExpectationsWithTimeout(timeout, handler: nil)
  104. // Then
  105. XCTAssertNil(error, "error should be nil")
  106. }
  107. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds() {
  108. // Given
  109. let URLString = "https://httpbin.org/ip"
  110. let expectation = expectationWithDescription("request should succeed and return ip")
  111. var error: NSError?
  112. // When
  113. Alamofire.request(.GET, URLString)
  114. .validate(contentType: ["*/*"])
  115. .validate(contentType: ["application/*"])
  116. .validate(contentType: ["*/json"])
  117. .response { _, _, _, responseError in
  118. error = responseError
  119. expectation.fulfill()
  120. }
  121. waitForExpectationsWithTimeout(timeout, handler: nil)
  122. // Then
  123. XCTAssertNil(error, "error should be nil")
  124. }
  125. func testThatValidationForRequestWithUnacceptableContentTypeResponseFails() {
  126. // Given
  127. let URLString = "https://httpbin.org/xml"
  128. let expectation = expectationWithDescription("request should succeed and return xml")
  129. var error: NSError?
  130. // When
  131. Alamofire.request(.GET, URLString)
  132. .validate(contentType: ["application/octet-stream"])
  133. .response { _, _, _, responseError in
  134. error = responseError
  135. expectation.fulfill()
  136. }
  137. waitForExpectationsWithTimeout(timeout, handler: nil)
  138. // Then
  139. XCTAssertNotNil(error, "error should not be nil")
  140. if let error = error {
  141. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  142. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  143. } else {
  144. XCTFail("error should be an NSError")
  145. }
  146. }
  147. func testThatValidationForRequestWithNoAcceptableContentTypeResponseFails() {
  148. // Given
  149. let URLString = "https://httpbin.org/xml"
  150. let expectation = expectationWithDescription("request should succeed and return xml")
  151. var error: NSError?
  152. // When
  153. Alamofire.request(.GET, URLString)
  154. .validate(contentType: [])
  155. .response { _, _, _, responseError in
  156. error = responseError
  157. expectation.fulfill()
  158. }
  159. waitForExpectationsWithTimeout(timeout, handler: nil)
  160. // Then
  161. XCTAssertNotNil(error, "error should not be nil")
  162. if let error = error {
  163. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  164. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  165. } else {
  166. XCTFail("error should be an NSError")
  167. }
  168. }
  169. func testThatValidationForRequestWithNoAcceptableContentTypeResponseSucceedsWhenNoDataIsReturned() {
  170. // Given
  171. let URLString = "https://httpbin.org/status/204"
  172. let expectation = expectationWithDescription("request should succeed and return no data")
  173. var error: NSError?
  174. // When
  175. Alamofire.request(.GET, URLString)
  176. .validate(contentType: [])
  177. .response { _, response, data, responseError in
  178. error = responseError
  179. expectation.fulfill()
  180. }
  181. waitForExpectationsWithTimeout(timeout, handler: nil)
  182. // Then
  183. XCTAssertNil(error, "error should be nil")
  184. }
  185. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceedsWhenResponseIsNil() {
  186. // Given
  187. class MockManager: Manager {
  188. override func request(URLRequest: URLRequestConvertible) -> Request {
  189. var dataTask: NSURLSessionDataTask!
  190. dispatch_sync(queue) {
  191. dataTask = self.session.dataTaskWithRequest(URLRequest.URLRequest)
  192. }
  193. let request = MockRequest(session: session, task: dataTask)
  194. delegate[request.delegate.task] = request.delegate
  195. if startRequestsImmediately {
  196. request.resume()
  197. }
  198. return request
  199. }
  200. }
  201. class MockRequest: Request {
  202. override var response: NSHTTPURLResponse? {
  203. return MockHTTPURLResponse(
  204. URL: NSURL(string: request!.URLString)!,
  205. statusCode: 204,
  206. HTTPVersion: "HTTP/1.1",
  207. headerFields: nil
  208. )
  209. }
  210. }
  211. class MockHTTPURLResponse: NSHTTPURLResponse {
  212. override var MIMEType: String? { return nil }
  213. }
  214. let manager: Manager = {
  215. let configuration: NSURLSessionConfiguration = {
  216. let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
  217. configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
  218. return configuration
  219. }()
  220. return MockManager(configuration: configuration)
  221. }()
  222. let URLString = "https://httpbin.org/delete"
  223. let expectation = expectationWithDescription("request should be stubbed and return 204 status code")
  224. var response: NSHTTPURLResponse?
  225. var data: NSData?
  226. var error: NSError?
  227. // When
  228. manager.request(.DELETE, URLString)
  229. .validate(contentType: ["*/*"])
  230. .response { _, responseResponse, responseData, responseError in
  231. response = responseResponse
  232. data = responseData
  233. error = responseError
  234. expectation.fulfill()
  235. }
  236. waitForExpectationsWithTimeout(timeout, handler: nil)
  237. // Then
  238. XCTAssertNotNil(response, "response should not be nil")
  239. XCTAssertNotNil(data, "data should not be nil")
  240. XCTAssertNil(error, "error should be nil")
  241. if let response = response {
  242. XCTAssertEqual(response.statusCode, 204, "response status code should be 204")
  243. XCTAssertNil(response.MIMEType, "response mime type should be nil")
  244. }
  245. }
  246. }
  247. // MARK: -
  248. class MultipleValidationTestCase: BaseTestCase {
  249. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  250. // Given
  251. let URLString = "https://httpbin.org/ip"
  252. let expectation = expectationWithDescription("request should succeed and return ip")
  253. var error: NSError?
  254. // When
  255. Alamofire.request(.GET, URLString)
  256. .validate(statusCode: 200..<300)
  257. .validate(contentType: ["application/json"])
  258. .response { _, _, _, responseError in
  259. error = responseError
  260. expectation.fulfill()
  261. }
  262. waitForExpectationsWithTimeout(timeout, handler: nil)
  263. // Then
  264. XCTAssertNil(error, "error should be nil")
  265. }
  266. func testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFailsWithStatusCodeError() {
  267. // Given
  268. let URLString = "https://httpbin.org/xml"
  269. let expectation = expectationWithDescription("request should succeed and return xml")
  270. var error: NSError?
  271. // When
  272. Alamofire.request(.GET, URLString)
  273. .validate(statusCode: 400..<600)
  274. .validate(contentType: ["application/octet-stream"])
  275. .response { _, _, _, responseError in
  276. error = responseError
  277. expectation.fulfill()
  278. }
  279. waitForExpectationsWithTimeout(timeout, handler: nil)
  280. // Then
  281. XCTAssertNotNil(error, "error should not be nil")
  282. if let error = error {
  283. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  284. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  285. } else {
  286. XCTFail("error should be an NSError")
  287. }
  288. }
  289. func testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFailsWithContentTypeError() {
  290. // Given
  291. let URLString = "https://httpbin.org/xml"
  292. let expectation = expectationWithDescription("request should succeed and return xml")
  293. var error: NSError?
  294. // When
  295. Alamofire.request(.GET, URLString)
  296. .validate(contentType: ["application/octet-stream"])
  297. .validate(statusCode: 400..<600)
  298. .response { _, _, _, responseError in
  299. error = responseError
  300. expectation.fulfill()
  301. }
  302. waitForExpectationsWithTimeout(timeout, handler: nil)
  303. // Then
  304. XCTAssertNotNil(error, "error should not be nil")
  305. if let error = error {
  306. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  307. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  308. } else {
  309. XCTFail("error should be an NSError")
  310. }
  311. }
  312. }
  313. // MARK: -
  314. class AutomaticValidationTestCase: BaseTestCase {
  315. func testThatValidationForRequestWithAcceptableStatusCodeAndContentTypeResponseSucceeds() {
  316. // Given
  317. let URL = NSURL(string: "https://httpbin.org/ip")!
  318. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  319. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  320. let expectation = expectationWithDescription("request should succeed and return ip")
  321. var error: NSError?
  322. // When
  323. Alamofire.request(mutableURLRequest)
  324. .validate()
  325. .response { _, _, _, responseError in
  326. error = responseError
  327. expectation.fulfill()
  328. }
  329. waitForExpectationsWithTimeout(timeout, handler: nil)
  330. // Then
  331. XCTAssertNil(error, "error should be nil")
  332. }
  333. func testThatValidationForRequestWithUnacceptableStatusCodeResponseFails() {
  334. // Given
  335. let URLString = "https://httpbin.org/status/404"
  336. let expectation = expectationWithDescription("request should return 404 status code")
  337. var error: NSError?
  338. // When
  339. Alamofire.request(.GET, URLString)
  340. .validate()
  341. .response { _, _, _, responseError in
  342. error = responseError
  343. expectation.fulfill()
  344. }
  345. waitForExpectationsWithTimeout(timeout, handler: nil)
  346. // Then
  347. XCTAssertNotNil(error, "error should not be nil")
  348. if let error = error {
  349. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  350. XCTAssertEqual(error.code, Error.Code.StatusCodeValidationFailed.rawValue, "code should be status code validation failure")
  351. } else {
  352. XCTFail("error should be an NSError")
  353. }
  354. }
  355. func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds() {
  356. // Given
  357. let URL = NSURL(string: "https://httpbin.org/ip")!
  358. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  359. mutableURLRequest.setValue("application/*", forHTTPHeaderField: "Accept")
  360. let expectation = expectationWithDescription("request should succeed and return ip")
  361. var error: NSError?
  362. // When
  363. Alamofire.request(mutableURLRequest)
  364. .validate()
  365. .response { _, _, _, responseError in
  366. error = responseError
  367. expectation.fulfill()
  368. }
  369. waitForExpectationsWithTimeout(timeout, handler: nil)
  370. // Then
  371. XCTAssertNil(error, "error should be nil")
  372. }
  373. func testThatValidationForRequestWithAcceptableComplexContentTypeResponseSucceeds() {
  374. // Given
  375. let URL = NSURL(string: "https://httpbin.org/xml")!
  376. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  377. let headerValue = "text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8,*/*;q=0.5"
  378. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: "Accept")
  379. let expectation = expectationWithDescription("request should succeed and return xml")
  380. var error: NSError?
  381. // When
  382. Alamofire.request(mutableURLRequest)
  383. .validate()
  384. .response { _, _, _, responseError in
  385. error = responseError
  386. expectation.fulfill()
  387. }
  388. waitForExpectationsWithTimeout(timeout, handler: nil)
  389. // Then
  390. XCTAssertNil(error, "error should be nil")
  391. }
  392. func testThatValidationForRequestWithUnacceptableContentTypeResponseFails() {
  393. // Given
  394. let URL = NSURL(string: "https://httpbin.org/xml")!
  395. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  396. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  397. let expectation = expectationWithDescription("request should succeed and return xml")
  398. var error: NSError?
  399. // When
  400. Alamofire.request(mutableURLRequest)
  401. .validate()
  402. .response { _, _, _, responseError in
  403. error = responseError
  404. expectation.fulfill()
  405. }
  406. waitForExpectationsWithTimeout(timeout, handler: nil)
  407. // Then
  408. XCTAssertNotNil(error, "error should not be nil")
  409. if let error = error {
  410. XCTAssertEqual(error.domain, Error.Domain, "domain should be Alamofire error domain")
  411. XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
  412. } else {
  413. XCTFail("error should be an NSError")
  414. }
  415. }
  416. }