ValidationTests.swift 18 KB

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