AuthenticationTests.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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: 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 response: DataResponse<Data?>?
  57. // When
  58. manager.request(urlString)
  59. .authenticate(withUsername: "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. // TODO: What data was there before?
  70. XCTAssertNil(response?.data)
  71. XCTAssertNil(response?.error)
  72. }
  73. func testHTTPBasicAuthenticationWithValidCredentials() {
  74. // Given
  75. let expectation = self.expectation(description: "\(urlString) 200")
  76. var response: DataResponse<Data?>?
  77. // When
  78. manager.request(urlString)
  79. .authenticate(withUsername: user, password: password)
  80. .response { resp in
  81. response = resp
  82. expectation.fulfill()
  83. }
  84. waitForExpectations(timeout: timeout, handler: nil)
  85. // Then
  86. XCTAssertNotNil(response?.request)
  87. XCTAssertNotNil(response?.response)
  88. XCTAssertEqual(response?.response?.statusCode, 200)
  89. XCTAssertNotNil(response?.data)
  90. XCTAssertNil(response?.error)
  91. }
  92. // TODO: How did this test work, this returns an initial 404?
  93. // func testHiddenHTTPBasicAuthentication() {
  94. // // Given
  95. // let urlString = "http://httpbin.org/hidden-basic-auth/\(user)/\(password)"
  96. // let expectation = self.expectation(description: "\(urlString) 200")
  97. // let headers = HTTPHeaders.authorization(withUsername: user, password: password)
  98. //
  99. // var response: DataResponse<Data?>?
  100. //
  101. // // When
  102. // manager.request(urlString, headers: headers)
  103. // .response { resp in
  104. // response = resp
  105. // expectation.fulfill()
  106. // }
  107. //
  108. // waitForExpectations(timeout: timeout, handler: nil)
  109. //
  110. // // Then
  111. // XCTAssertNotNil(response?.request)
  112. // XCTAssertNotNil(response?.response)
  113. // XCTAssertEqual(response?.response?.statusCode, 200)
  114. // XCTAssertNotNil(response?.data)
  115. // XCTAssertNil(response?.error)
  116. // }
  117. }
  118. // MARK: -
  119. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  120. let qop = "auth"
  121. override func setUp() {
  122. super.setUp()
  123. urlString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  124. }
  125. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  126. // Given
  127. let expectation = self.expectation(description: "\(urlString) 401")
  128. var response: DataResponse<Data?>?
  129. // When
  130. manager.request(urlString)
  131. .authenticate(withUsername: "invalid", password: "credentials")
  132. .response { resp in
  133. response = resp
  134. expectation.fulfill()
  135. }
  136. waitForExpectations(timeout: timeout, handler: nil)
  137. // Then
  138. XCTAssertNotNil(response?.request)
  139. XCTAssertNotNil(response?.response)
  140. XCTAssertEqual(response?.response?.statusCode, 401)
  141. // TODO: What data was there before, failing auth should return an empty body?
  142. XCTAssertNil(response?.data)
  143. XCTAssertNil(response?.error)
  144. }
  145. func testHTTPDigestAuthenticationWithValidCredentials() {
  146. // Given
  147. let expectation = self.expectation(description: "\(urlString) 200")
  148. var response: DataResponse<Data?>?
  149. // When
  150. manager.request(urlString)
  151. .authenticate(withUsername: user, password: password)
  152. .response { resp in
  153. response = resp
  154. expectation.fulfill()
  155. }
  156. waitForExpectations(timeout: timeout, handler: nil)
  157. // Then
  158. XCTAssertNotNil(response?.request)
  159. XCTAssertNotNil(response?.response)
  160. XCTAssertEqual(response?.response?.statusCode, 200)
  161. XCTAssertNotNil(response?.data)
  162. XCTAssertNil(response?.error)
  163. }
  164. }