ValidationTests.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // DownloadTests.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 Foundation
  23. import Alamofire
  24. import XCTest
  25. class AlamofireStatusCodeValidationTestCase: XCTestCase {
  26. func testValidationForRequestWithAcceptableStatusCodeResponse() {
  27. let URL = "http://httpbin.org/status/200"
  28. let expectation = expectationWithDescription("\(URL)")
  29. Alamofire.request(.GET, URL)
  30. .validate(statusCode: 200..<300)
  31. .response { _, _, _, error in
  32. XCTAssertNil(error, "error should be nil")
  33. expectation.fulfill()
  34. }
  35. waitForExpectationsWithTimeout(10) { error in
  36. XCTAssertNil(error, "\(error)")
  37. }
  38. }
  39. func testValidationForRequestWithUnacceptableStatusCodeResponse() {
  40. let URL = "http://httpbin.org/status/404"
  41. let expectation = expectationWithDescription("\(URL)")
  42. Alamofire.request(.GET, URL)
  43. .validate(statusCode: [200])
  44. .response { _, _, _, error in
  45. XCTAssertNotNil(error, "error should not be nil")
  46. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  47. expectation.fulfill()
  48. }
  49. waitForExpectationsWithTimeout(10) { error in
  50. XCTAssertNil(error, "\(error)")
  51. }
  52. }
  53. func testValidationForRequestWithNoAcceptableStatusCodes() {
  54. let URL = "http://httpbin.org/status/201"
  55. let expectation = expectationWithDescription("\(URL)")
  56. Alamofire.request(.GET, URL)
  57. .validate(statusCode: [])
  58. .response { _, _, _, error in
  59. XCTAssertNotNil(error, "error should not be nil")
  60. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  61. expectation.fulfill()
  62. }
  63. waitForExpectationsWithTimeout(10) { error in
  64. XCTAssertNil(error, "\(error)")
  65. }
  66. }
  67. }
  68. class AlamofireContentTypeValidationTestCase: XCTestCase {
  69. func testValidationForRequestWithAcceptableContentTypeResponse() {
  70. let URL = "http://httpbin.org/ip"
  71. let expectation = expectationWithDescription("\(URL)")
  72. Alamofire.request(.GET, URL)
  73. .validate(contentType: ["application/json"])
  74. .response { _, _, _, error in
  75. XCTAssertNil(error, "error should be nil")
  76. expectation.fulfill()
  77. }
  78. waitForExpectationsWithTimeout(10) { error in
  79. XCTAssertNil(error, "\(error)")
  80. }
  81. }
  82. func testValidationForRequestWithAcceptableWildcardContentTypeResponse() {
  83. let URL = "http://httpbin.org/ip"
  84. let expectation = expectationWithDescription("\(URL)")
  85. Alamofire.request(.GET, URL)
  86. .validate(contentType: ["*/*"])
  87. .validate(contentType: ["application/*"])
  88. .validate(contentType: ["*/json"])
  89. .response { _, _, _, error in
  90. XCTAssertNil(error, "error should be nil")
  91. expectation.fulfill()
  92. }
  93. waitForExpectationsWithTimeout(10) { error in
  94. XCTAssertNil(error, "\(error)")
  95. }
  96. }
  97. func testValidationForRequestWithUnacceptableContentTypeResponse() {
  98. let URL = "http://httpbin.org/xml"
  99. let expectation = expectationWithDescription("\(URL)")
  100. Alamofire.request(.GET, URL)
  101. .validate(contentType: ["application/octet-stream"])
  102. .response { _, _, _, error in
  103. XCTAssertNotNil(error, "error should not be nil")
  104. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  105. expectation.fulfill()
  106. }
  107. waitForExpectationsWithTimeout(10) { error in
  108. XCTAssertNil(error, "\(error)")
  109. }
  110. }
  111. func testValidationForRequestWithNoAcceptableContentTypeResponse() {
  112. let URL = "http://httpbin.org/xml"
  113. let expectation = expectationWithDescription("\(URL)")
  114. Alamofire.request(.GET, URL)
  115. .validate(contentType: [])
  116. .response { _, _, _, error in
  117. XCTAssertNotNil(error, "error should not be nil")
  118. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  119. expectation.fulfill()
  120. }
  121. waitForExpectationsWithTimeout(10) { error in
  122. XCTAssertNil(error, "\(error)")
  123. }
  124. }
  125. }
  126. class AlamofireMultipleValidationTestCase: XCTestCase {
  127. func testValidationForRequestWithAcceptableStatusCodeAndContentTypeResponse() {
  128. let URL = "http://httpbin.org/ip"
  129. let expectation = expectationWithDescription("\(URL)")
  130. Alamofire.request(.GET, URL)
  131. .validate(statusCode: 200..<300)
  132. .validate(contentType: ["application/json"])
  133. .response { _, _, _, error in
  134. XCTAssertNil(error, "error should be nil")
  135. expectation.fulfill()
  136. }
  137. waitForExpectationsWithTimeout(10) { error in
  138. XCTAssertNil(error, "\(error)")
  139. }
  140. }
  141. func testValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponse() {
  142. let URL = "http://httpbin.org/xml"
  143. let expectation = expectationWithDescription("\(URL)")
  144. Alamofire.request(.GET, URL)
  145. .validate(statusCode: 400..<600)
  146. .validate(contentType: ["application/octet-stream"])
  147. .response { _, _, _, error in
  148. XCTAssertNotNil(error, "error should not be nil")
  149. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  150. expectation.fulfill()
  151. }
  152. waitForExpectationsWithTimeout(10) { error in
  153. XCTAssertNil(error, "\(error)")
  154. }
  155. }
  156. }
  157. class AlamofireAutomaticValidationTestCase: XCTestCase {
  158. func testValidationForRequestWithAcceptableStatusCodeAndContentTypeResponse() {
  159. let URL = NSURL(string: "http://httpbin.org/ip")!
  160. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  161. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  162. let expectation = expectationWithDescription("\(URL)")
  163. Alamofire.request(.GET, URL)
  164. .validate()
  165. .response { _, _, _, error in
  166. XCTAssertNil(error, "error should be nil")
  167. expectation.fulfill()
  168. }
  169. waitForExpectationsWithTimeout(10) { error in
  170. XCTAssertNil(error, "\(error)")
  171. }
  172. }
  173. func testValidationForRequestWithUnacceptableStatusCodeResponse() {
  174. let URL = "http://httpbin.org/status/404"
  175. let expectation = expectationWithDescription("\(URL)")
  176. Alamofire.request(.GET, URL)
  177. .validate()
  178. .response { _, _, _, error in
  179. XCTAssertNotNil(error, "error should not be nil")
  180. XCTAssertEqual(error!.domain, AlamofireErrorDomain, "error should be in Alamofire error domain")
  181. expectation.fulfill()
  182. }
  183. waitForExpectationsWithTimeout(10) { error in
  184. XCTAssertNil(error, "\(error)")
  185. }
  186. }
  187. func testValidationForRequestWithAcceptableWildcardContentTypeResponse() {
  188. let URL = NSURL(string: "http://httpbin.org/ip")!
  189. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  190. mutableURLRequest.setValue("application/*", forHTTPHeaderField: "Accept")
  191. let expectation = expectationWithDescription("\(URL)")
  192. Alamofire.request(.GET, URL)
  193. .validate()
  194. .response { _, _, _, error in
  195. XCTAssertNil(error, "error should be nil")
  196. expectation.fulfill()
  197. }
  198. waitForExpectationsWithTimeout(10) { error in
  199. XCTAssertNil(error, "\(error)")
  200. }
  201. }
  202. func testValidationForRequestWithAcceptableComplexContentTypeResponse() {
  203. let URL = NSURL(string: "http://httpbin.org/xml")!
  204. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  205. mutableURLRequest.setValue("text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8,*/*;q=0.5", forHTTPHeaderField: "Accept")
  206. let expectation = expectationWithDescription("\(URL)")
  207. Alamofire.request(.GET, URL)
  208. .validate()
  209. .response { _, _, _, error in
  210. XCTAssertNil(error, "error should be nil")
  211. expectation.fulfill()
  212. }
  213. waitForExpectationsWithTimeout(10) { error in
  214. XCTAssertNil(error, "\(error)")
  215. }
  216. }
  217. func testValidationForRequestWithUnacceptableContentTypeResponse() {
  218. let URL = NSURL(string: "http://httpbin.org/xml")!
  219. let mutableURLRequest = NSMutableURLRequest(URL: URL)
  220. mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
  221. let expectation = expectationWithDescription("\(URL)")
  222. Alamofire.request(.GET, URL)
  223. .validate()
  224. .response { _, _, _, error in
  225. XCTAssertNil(error, "error should be nil")
  226. expectation.fulfill()
  227. }
  228. waitForExpectationsWithTimeout(10) { error in
  229. XCTAssertNil(error, "\(error)")
  230. }
  231. }
  232. }