AuthenticationTests.swift 6.3 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. func testHiddenHTTPBasicAuthentication() {
  93. // Given
  94. let urlString = "http://httpbin.org/hidden-basic-auth/\(user)/\(password)"
  95. let expectation = self.expectation(description: "\(urlString) 200")
  96. let headers = HTTPHeaders.authorization(withUsername: user, password: password)
  97. var response: DataResponse<Data?>?
  98. // When
  99. manager.request(urlString, headers: headers)
  100. .response { resp in
  101. response = resp
  102. expectation.fulfill()
  103. }
  104. waitForExpectations(timeout: timeout, handler: nil)
  105. // Then
  106. XCTAssertNotNil(response?.request)
  107. XCTAssertNotNil(response?.response)
  108. XCTAssertEqual(response?.response?.statusCode, 200)
  109. XCTAssertNotNil(response?.data)
  110. XCTAssertNil(response?.error)
  111. }
  112. }
  113. // MARK: -
  114. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  115. let qop = "auth"
  116. override func setUp() {
  117. super.setUp()
  118. urlString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  119. }
  120. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  121. // Given
  122. let expectation = self.expectation(description: "\(urlString) 401")
  123. var response: DataResponse<Data?>?
  124. // When
  125. manager.request(urlString)
  126. .authenticate(withUsername: "invalid", password: "credentials")
  127. .response { resp in
  128. response = resp
  129. expectation.fulfill()
  130. }
  131. waitForExpectations(timeout: timeout, handler: nil)
  132. // Then
  133. XCTAssertNotNil(response?.request)
  134. XCTAssertNotNil(response?.response)
  135. XCTAssertEqual(response?.response?.statusCode, 401)
  136. // TODO: What data was there before, failing auth should return an empty body?
  137. XCTAssertNil(response?.data)
  138. XCTAssertNil(response?.error)
  139. }
  140. func testHTTPDigestAuthenticationWithValidCredentials() {
  141. // Given
  142. let expectation = self.expectation(description: "\(urlString) 200")
  143. var response: DataResponse<Data?>?
  144. // When
  145. manager.request(urlString)
  146. .authenticate(withUsername: user, password: password)
  147. .response { resp in
  148. response = resp
  149. expectation.fulfill()
  150. }
  151. waitForExpectations(timeout: timeout, handler: nil)
  152. // Then
  153. XCTAssertNotNil(response?.request)
  154. XCTAssertNotNil(response?.response)
  155. XCTAssertEqual(response?.response?.statusCode, 200)
  156. XCTAssertNotNil(response?.data)
  157. XCTAssertNil(response?.error)
  158. }
  159. }