AuthenticationTests.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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()
  31. let endpoint = Endpoint.basicAuth()
  32. let expectation = 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. response = resp
  39. expectation.fulfill()
  40. }
  41. waitForExpectations(timeout: timeout)
  42. // Then
  43. XCTAssertNotNil(response?.request)
  44. XCTAssertNotNil(response?.response)
  45. XCTAssertEqual(response?.response?.statusCode, 401)
  46. XCTAssertNil(response?.data)
  47. XCTAssertNil(response?.error)
  48. }
  49. func testHTTPBasicAuthenticationWithValidCredentials() {
  50. // Given
  51. let session = Session()
  52. let user = "user1", password = "password"
  53. let endpoint = Endpoint.basicAuth(forUser: user, password: password)
  54. let expectation = expectation(description: "\(endpoint.url) 200")
  55. var response: DataResponse<Data?, AFError>?
  56. // When
  57. session.request(endpoint)
  58. .authenticate(username: user, password: password)
  59. .response { resp in
  60. response = resp
  61. expectation.fulfill()
  62. }
  63. waitForExpectations(timeout: timeout)
  64. // Then
  65. XCTAssertNotNil(response?.request)
  66. XCTAssertNotNil(response?.response)
  67. XCTAssertEqual(response?.response?.statusCode, 200)
  68. XCTAssertNotNil(response?.data)
  69. XCTAssertNil(response?.error)
  70. }
  71. func testHTTPBasicAuthenticationWithStoredCredentials() {
  72. // Given
  73. let session = Session()
  74. let user = "user2", password = "password"
  75. let endpoint = Endpoint.basicAuth(forUser: user, password: password)
  76. let expectation = expectation(description: "\(endpoint.url) 200")
  77. var response: DataResponse<Data?, AFError>?
  78. // When
  79. let credential = URLCredential(user: user, password: password, persistence: .forSession)
  80. URLCredentialStorage.shared.setDefaultCredential(credential,
  81. for: .init(host: endpoint.host.rawValue,
  82. port: endpoint.port,
  83. protocol: endpoint.scheme.rawValue,
  84. realm: endpoint.host.rawValue,
  85. authenticationMethod: NSURLAuthenticationMethodHTTPBasic))
  86. session.request(endpoint)
  87. .response { resp in
  88. response = resp
  89. expectation.fulfill()
  90. }
  91. waitForExpectations(timeout: timeout)
  92. // Then
  93. XCTAssertNotNil(response?.request)
  94. XCTAssertNotNil(response?.response)
  95. XCTAssertEqual(response?.response?.statusCode, 200)
  96. XCTAssertNotNil(response?.data)
  97. XCTAssertNil(response?.error)
  98. }
  99. func testHiddenHTTPBasicAuthentication() {
  100. // Given
  101. let session = Session()
  102. let endpoint = Endpoint.hiddenBasicAuth()
  103. let expectation = expectation(description: "\(endpoint.url) 200")
  104. var response: DataResponse<Data?, AFError>?
  105. // When
  106. session.request(endpoint)
  107. .response { resp in
  108. response = resp
  109. expectation.fulfill()
  110. }
  111. waitForExpectations(timeout: timeout)
  112. // Then
  113. XCTAssertNotNil(response?.request)
  114. XCTAssertNotNil(response?.response)
  115. XCTAssertEqual(response?.response?.statusCode, 200)
  116. XCTAssertNotNil(response?.data)
  117. XCTAssertNil(response?.error)
  118. }
  119. }
  120. // MARK: -
  121. // Disabled due to HTTPBin flakiness.
  122. final class HTTPDigestAuthenticationTestCase: BaseTestCase {
  123. func _testHTTPDigestAuthenticationWithInvalidCredentials() {
  124. // Given
  125. let session = Session()
  126. let endpoint = Endpoint.digestAuth()
  127. let expectation = 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()
  147. let user = "user", password = "password"
  148. let endpoint = Endpoint.digestAuth(forUser: user, password: password)
  149. let expectation = 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. }