AuthenticationTests.swift 7.2 KB

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