AuthenticationTests.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // AuthenticationTests.swift
  3. //
  4. // Copyright (c) 2014-2018 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: Session!
  32. override func setUp() {
  33. super.setUp()
  34. manager = Session(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 response: DataResponse<Data?, AFError>?
  57. // When
  58. manager.request(urlString)
  59. .authenticate(username: "invalid", password: "credentials")
  60. .response { resp in
  61. response = resp
  62. expectation.fulfill()
  63. }
  64. waitForExpectations(timeout: timeout, handler: nil)
  65. // Then
  66. XCTAssertNotNil(response?.request)
  67. XCTAssertNotNil(response?.response)
  68. XCTAssertEqual(response?.response?.statusCode, 401)
  69. XCTAssertNil(response?.data)
  70. XCTAssertNil(response?.error)
  71. }
  72. func testHTTPBasicAuthenticationWithValidCredentials() {
  73. // Given
  74. let expectation = self.expectation(description: "\(urlString) 200")
  75. var response: DataResponse<Data?, AFError>?
  76. // When
  77. manager.request(urlString)
  78. .authenticate(username: user, password: password)
  79. .response { resp in
  80. response = resp
  81. expectation.fulfill()
  82. }
  83. waitForExpectations(timeout: timeout, handler: nil)
  84. // Then
  85. XCTAssertNotNil(response?.request)
  86. XCTAssertNotNil(response?.response)
  87. XCTAssertEqual(response?.response?.statusCode, 200)
  88. XCTAssertNotNil(response?.data)
  89. XCTAssertNil(response?.error)
  90. }
  91. func testHiddenHTTPBasicAuthentication() {
  92. // Given
  93. let urlString = "http://httpbin.org/hidden-basic-auth/\(user)/\(password)"
  94. let expectation = self.expectation(description: "\(urlString) 200")
  95. let headers: HTTPHeaders = [.authorization(username: user, password: password)]
  96. var response: DataResponse<Data?, AFError>?
  97. // When
  98. manager.request(urlString, headers: headers)
  99. .response { resp in
  100. response = resp
  101. expectation.fulfill()
  102. }
  103. waitForExpectations(timeout: timeout, handler: nil)
  104. // Then
  105. XCTAssertNotNil(response?.request)
  106. XCTAssertNotNil(response?.response)
  107. XCTAssertEqual(response?.response?.statusCode, 200)
  108. XCTAssertNotNil(response?.data)
  109. XCTAssertNil(response?.error)
  110. }
  111. }
  112. // MARK: -
  113. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  114. let qop = "auth"
  115. override func setUp() {
  116. super.setUp()
  117. urlString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  118. }
  119. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  120. // Given
  121. let expectation = self.expectation(description: "\(urlString) 401")
  122. var response: DataResponse<Data?, AFError>?
  123. // When
  124. manager.request(urlString)
  125. .authenticate(username: "invalid", password: "credentials")
  126. .response { resp in
  127. response = resp
  128. expectation.fulfill()
  129. }
  130. waitForExpectations(timeout: timeout, handler: nil)
  131. // Then
  132. XCTAssertNotNil(response?.request)
  133. XCTAssertNotNil(response?.response)
  134. XCTAssertEqual(response?.response?.statusCode, 401)
  135. XCTAssertNil(response?.data)
  136. XCTAssertNil(response?.error)
  137. }
  138. func testHTTPDigestAuthenticationWithValidCredentials() {
  139. // Given
  140. let expectation = self.expectation(description: "\(urlString) 200")
  141. var response: DataResponse<Data?, AFError>?
  142. // When
  143. manager.request(urlString)
  144. .authenticate(username: user, password: password)
  145. .response { resp in
  146. response = resp
  147. expectation.fulfill()
  148. }
  149. waitForExpectations(timeout: timeout, handler: nil)
  150. // Then
  151. XCTAssertNotNil(response?.request)
  152. XCTAssertNotNil(response?.response)
  153. XCTAssertEqual(response?.response?.statusCode, 200)
  154. XCTAssertNotNil(response?.data)
  155. XCTAssertNil(response?.error)
  156. }
  157. }