AuthenticationTests.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. final class BasicAuthenticationTestCase: BaseTestCase {
  28. @MainActor
  29. func testHTTPBasicAuthenticationFailsWithInvalidCredentials() {
  30. // Given
  31. let session = Session()
  32. let endpoint = Endpoint.basicAuth()
  33. let expectation = expectation(description: "\(endpoint.url) 401")
  34. var response: DataResponse<Data?, AFError>?
  35. // When
  36. session.request(endpoint)
  37. .authenticate(username: "invalid", password: "credentials")
  38. .response { resp in
  39. response = resp
  40. expectation.fulfill()
  41. }
  42. waitForExpectations(timeout: timeout)
  43. // Then
  44. XCTAssertNotNil(response?.request)
  45. XCTAssertNotNil(response?.response)
  46. XCTAssertEqual(response?.response?.statusCode, 401)
  47. XCTAssertNil(response?.data)
  48. XCTAssertNil(response?.error)
  49. }
  50. @MainActor
  51. func testHTTPBasicAuthenticationWithValidCredentials() {
  52. // Given
  53. let session = Session()
  54. let user = "user1", password = "password"
  55. let endpoint = Endpoint.basicAuth(forUser: user, password: password)
  56. let expectation = expectation(description: "\(endpoint.url) 200")
  57. var response: DataResponse<Data?, AFError>?
  58. // When
  59. session.request(endpoint)
  60. .authenticate(username: user, password: password)
  61. .response { resp in
  62. response = resp
  63. expectation.fulfill()
  64. }
  65. waitForExpectations(timeout: timeout)
  66. // Then
  67. XCTAssertNotNil(response?.request)
  68. XCTAssertNotNil(response?.response)
  69. XCTAssertEqual(response?.response?.statusCode, 200)
  70. XCTAssertNotNil(response?.data)
  71. XCTAssertNil(response?.error)
  72. }
  73. @MainActor
  74. func testHTTPBasicAuthenticationWithStoredCredentials() {
  75. // Given
  76. let session = Session()
  77. let user = "user2", password = "password"
  78. let endpoint = Endpoint.basicAuth(forUser: user, password: password)
  79. let expectation = expectation(description: "\(endpoint.url) 200")
  80. var response: DataResponse<Data?, AFError>?
  81. // When
  82. let credential = URLCredential(user: user, password: password, persistence: .forSession)
  83. URLCredentialStorage.shared.setDefaultCredential(credential,
  84. for: .init(host: endpoint.host.rawValue,
  85. port: endpoint.port,
  86. protocol: endpoint.scheme.rawValue,
  87. realm: endpoint.host.rawValue,
  88. authenticationMethod: NSURLAuthenticationMethodHTTPBasic))
  89. session.request(endpoint)
  90. .response { resp in
  91. response = resp
  92. expectation.fulfill()
  93. }
  94. waitForExpectations(timeout: timeout)
  95. // Then
  96. XCTAssertNotNil(response?.request)
  97. XCTAssertNotNil(response?.response)
  98. XCTAssertEqual(response?.response?.statusCode, 200)
  99. XCTAssertNotNil(response?.data)
  100. XCTAssertNil(response?.error)
  101. }
  102. @MainActor
  103. func testHiddenHTTPBasicAuthentication() {
  104. // Given
  105. let session = Session()
  106. let endpoint = Endpoint.hiddenBasicAuth()
  107. let expectation = expectation(description: "\(endpoint.url) 200")
  108. var response: DataResponse<Data?, AFError>?
  109. // When
  110. session.request(endpoint)
  111. .response { resp in
  112. response = resp
  113. expectation.fulfill()
  114. }
  115. waitForExpectations(timeout: timeout)
  116. // Then
  117. XCTAssertNotNil(response?.request)
  118. XCTAssertNotNil(response?.response)
  119. XCTAssertEqual(response?.response?.statusCode, 200)
  120. XCTAssertNotNil(response?.data)
  121. XCTAssertNil(response?.error)
  122. }
  123. }
  124. // MARK: -
  125. // Disabled due to HTTPBin flakiness.
  126. final class HTTPDigestAuthenticationTestCase: BaseTestCase {
  127. @MainActor
  128. func disabled_testHTTPDigestAuthenticationWithInvalidCredentials() {
  129. // Given
  130. let session = Session()
  131. let endpoint = Endpoint.digestAuth()
  132. let expectation = expectation(description: "\(endpoint.url) 401")
  133. var response: DataResponse<Data?, AFError>?
  134. // When
  135. session.request(endpoint)
  136. .authenticate(username: "invalid", password: "credentials")
  137. .response { resp in
  138. response = resp
  139. expectation.fulfill()
  140. }
  141. waitForExpectations(timeout: timeout)
  142. // Then
  143. XCTAssertNotNil(response?.request)
  144. XCTAssertNotNil(response?.response)
  145. XCTAssertEqual(response?.response?.statusCode, 401)
  146. XCTAssertNil(response?.data)
  147. XCTAssertNil(response?.error)
  148. }
  149. @MainActor
  150. func disabled_testHTTPDigestAuthenticationWithValidCredentials() {
  151. // Given
  152. let session = Session()
  153. let user = "user", password = "password"
  154. let endpoint = Endpoint.digestAuth(forUser: user, password: password)
  155. let expectation = expectation(description: "\(endpoint.url) 200")
  156. var response: DataResponse<Data?, AFError>?
  157. // When
  158. session.request(endpoint)
  159. .authenticate(username: user, password: password)
  160. .response { resp in
  161. response = resp
  162. expectation.fulfill()
  163. }
  164. waitForExpectations(timeout: timeout)
  165. // Then
  166. XCTAssertNotNil(response?.request)
  167. XCTAssertNotNil(response?.response)
  168. XCTAssertEqual(response?.response?.statusCode, 200)
  169. XCTAssertNotNil(response?.data)
  170. XCTAssertNil(response?.error)
  171. }
  172. }