AuthenticationTests.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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: Error?
  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: Error?
  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 urlString = "http://httpbin.org/hidden-basic-auth/\(user)/\(password)"
  106. let expectation = self.expectation(description: "\(urlString) 200")
  107. var headers: [String: String]?
  108. if let authorizationHeader = Request.authorizationHeaderFrom(user: user, password: password) {
  109. headers = [authorizationHeader.key: authorizationHeader.value]
  110. }
  111. var request: URLRequest?
  112. var response: HTTPURLResponse?
  113. var data: Data?
  114. var error: Error?
  115. // When
  116. manager.request(urlString, withMethod: .get, headers: headers)
  117. .response { responseRequest, responseResponse, responseData, responseError in
  118. request = responseRequest
  119. response = responseResponse
  120. data = responseData
  121. error = responseError
  122. expectation.fulfill()
  123. }
  124. waitForExpectations(timeout: timeout, handler: nil)
  125. // Then
  126. XCTAssertNotNil(request, "request should not be nil")
  127. XCTAssertNotNil(response, "response should not be nil")
  128. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  129. XCTAssertNotNil(data, "data should not be nil")
  130. XCTAssertNil(error, "error should be nil")
  131. }
  132. }
  133. // MARK: -
  134. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  135. let qop = "auth"
  136. override func setUp() {
  137. super.setUp()
  138. urlString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  139. }
  140. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  141. // Given
  142. let expectation = self.expectation(description: "\(urlString) 401")
  143. var request: URLRequest?
  144. var response: HTTPURLResponse?
  145. var data: Data?
  146. var error: Error?
  147. // When
  148. manager.request(urlString, withMethod: .get)
  149. .authenticate(user: "invalid", password: "credentials")
  150. .response { responseRequest, responseResponse, responseData, responseError in
  151. request = responseRequest
  152. response = responseResponse
  153. data = responseData
  154. error = responseError
  155. expectation.fulfill()
  156. }
  157. waitForExpectations(timeout: timeout, handler: nil)
  158. // Then
  159. XCTAssertNotNil(request, "request should not be nil")
  160. XCTAssertNotNil(response, "response should not be nil")
  161. XCTAssertEqual(response?.statusCode ?? 0, 401, "response status code should be 401")
  162. XCTAssertNotNil(data, "data should not be nil")
  163. XCTAssertNil(error, "error should be nil")
  164. }
  165. func testHTTPDigestAuthenticationWithValidCredentials() {
  166. // Given
  167. let expectation = self.expectation(description: "\(urlString) 200")
  168. var request: URLRequest?
  169. var response: HTTPURLResponse?
  170. var data: Data?
  171. var error: Error?
  172. // When
  173. manager.request(urlString, withMethod: .get)
  174. .authenticate(user: user, password: password)
  175. .response { responseRequest, responseResponse, responseData, responseError in
  176. request = responseRequest
  177. response = responseResponse
  178. data = responseData
  179. error = responseError
  180. expectation.fulfill()
  181. }
  182. waitForExpectations(timeout: timeout, handler: nil)
  183. // Then
  184. XCTAssertNotNil(request, "request should not be nil")
  185. XCTAssertNotNil(response, "response should not be nil")
  186. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  187. XCTAssertNotNil(data, "data should not be nil")
  188. XCTAssertNil(error, "error should be nil")
  189. }
  190. }