AuthenticationTests.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // AuthenticationTests.swift
  3. //
  4. // Copyright (c) 2014-2016 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Alamofire
  25. import Foundation
  26. import XCTest
  27. class AuthenticationTestCase: BaseTestCase {
  28. let user = "user"
  29. let password = "password"
  30. var urlString = ""
  31. var manager: SessionManager!
  32. override func setUp() {
  33. super.setUp()
  34. manager = SessionManager(configuration: .default)
  35. // Clear out credentials
  36. let credentialStorage = URLCredentialStorage.shared
  37. for (protectionSpace, credentials) in credentialStorage.allCredentials {
  38. for (_, credential) in credentials {
  39. credentialStorage.remove(credential, for: protectionSpace)
  40. }
  41. }
  42. // Clear out cookies
  43. let cookieStorage = HTTPCookieStorage.shared
  44. cookieStorage.cookies?.forEach { cookieStorage.deleteCookie($0) }
  45. }
  46. }
  47. // MARK: -
  48. class BasicAuthenticationTestCase: AuthenticationTestCase {
  49. override func setUp() {
  50. super.setUp()
  51. urlString = "https://httpbin.org/basic-auth/\(user)/\(password)"
  52. }
  53. func testHTTPBasicAuthenticationWithInvalidCredentials() {
  54. // Given
  55. let expectation = self.expectation(description: "\(urlString) 401")
  56. var request: URLRequest?
  57. var response: HTTPURLResponse?
  58. var data: Data?
  59. var error: NSError?
  60. // When
  61. manager.request(urlString, withMethod: .get)
  62. .authenticate(user: "invalid", password: "credentials")
  63. .response { responseRequest, responseResponse, responseData, responseError in
  64. request = responseRequest
  65. response = responseResponse
  66. data = responseData
  67. error = responseError
  68. expectation.fulfill()
  69. }
  70. waitForExpectations(timeout: timeout, handler: nil)
  71. // Then
  72. XCTAssertNotNil(request, "request should not be nil")
  73. XCTAssertNotNil(response, "response should not be nil")
  74. XCTAssertEqual(response?.statusCode ?? 0, 401, "response status code should be 401")
  75. XCTAssertNotNil(data, "data should not be nil")
  76. XCTAssertNil(error, "error should be nil")
  77. }
  78. func testHTTPBasicAuthenticationWithValidCredentials() {
  79. // Given
  80. let expectation = self.expectation(description: "\(urlString) 200")
  81. var request: URLRequest?
  82. var response: HTTPURLResponse?
  83. var data: Data?
  84. var error: NSError?
  85. // When
  86. manager.request(urlString, withMethod: .get)
  87. .authenticate(user: user, password: password)
  88. .response { responseRequest, responseResponse, responseData, responseError in
  89. request = responseRequest
  90. response = responseResponse
  91. data = responseData
  92. error = responseError
  93. expectation.fulfill()
  94. }
  95. waitForExpectations(timeout: timeout, handler: nil)
  96. // Then
  97. XCTAssertNotNil(request, "request should not be nil")
  98. XCTAssertNotNil(response, "response should not be nil")
  99. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  100. XCTAssertNotNil(data, "data should not be nil")
  101. XCTAssertNil(error, "error should be nil")
  102. }
  103. func testHiddenHTTPBasicAuthentication() {
  104. // Given
  105. let authorizationHeader = Request.authorizationHeaderFrom(user: user, password: password)
  106. let urlString = "http://httpbin.org/hidden-basic-auth/\(user)/\(password)"
  107. let expectation = self.expectation(description: "\(urlString) 200")
  108. var request: URLRequest?
  109. var response: HTTPURLResponse?
  110. var data: Data?
  111. var error: NSError?
  112. // When
  113. manager.request(urlString, withMethod: .get, headers: authorizationHeader)
  114. .response { responseRequest, responseResponse, responseData, responseError in
  115. request = responseRequest
  116. response = responseResponse
  117. data = responseData
  118. error = responseError
  119. expectation.fulfill()
  120. }
  121. waitForExpectations(timeout: timeout, handler: nil)
  122. // Then
  123. XCTAssertNotNil(request, "request should not be nil")
  124. XCTAssertNotNil(response, "response should not be nil")
  125. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  126. XCTAssertNotNil(data, "data should not be nil")
  127. XCTAssertNil(error, "error should be nil")
  128. }
  129. }
  130. // MARK: -
  131. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  132. let qop = "auth"
  133. override func setUp() {
  134. super.setUp()
  135. urlString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  136. }
  137. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  138. // Given
  139. let expectation = self.expectation(description: "\(urlString) 401")
  140. var request: URLRequest?
  141. var response: HTTPURLResponse?
  142. var data: Data?
  143. var error: NSError?
  144. // When
  145. manager.request(urlString, withMethod: .get)
  146. .authenticate(user: "invalid", password: "credentials")
  147. .response { responseRequest, responseResponse, responseData, responseError in
  148. request = responseRequest
  149. response = responseResponse
  150. data = responseData
  151. error = responseError
  152. expectation.fulfill()
  153. }
  154. waitForExpectations(timeout: timeout, handler: nil)
  155. // Then
  156. XCTAssertNotNil(request, "request should not be nil")
  157. XCTAssertNotNil(response, "response should not be nil")
  158. XCTAssertEqual(response?.statusCode ?? 0, 401, "response status code should be 401")
  159. XCTAssertNotNil(data, "data should not be nil")
  160. XCTAssertNil(error, "error should be nil")
  161. }
  162. func testHTTPDigestAuthenticationWithValidCredentials() {
  163. // Given
  164. let expectation = self.expectation(description: "\(urlString) 200")
  165. var request: URLRequest?
  166. var response: HTTPURLResponse?
  167. var data: Data?
  168. var error: NSError?
  169. // When
  170. manager.request(urlString, withMethod: .get)
  171. .authenticate(user: user, password: password)
  172. .response { responseRequest, responseResponse, responseData, responseError in
  173. request = responseRequest
  174. response = responseResponse
  175. data = responseData
  176. error = responseError
  177. expectation.fulfill()
  178. }
  179. waitForExpectations(timeout: timeout, handler: nil)
  180. // Then
  181. XCTAssertNotNil(request, "request should not be nil")
  182. XCTAssertNotNil(response, "response should not be nil")
  183. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  184. XCTAssertNotNil(data, "data should not be nil")
  185. XCTAssertNil(error, "error should be nil")
  186. }
  187. }