ValidationTests.swift 18 KB

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