AuthenticationTests.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // AuthenticationTests.swift
  2. //
  3. // Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Alamofire
  23. import Foundation
  24. import XCTest
  25. class AuthenticationTestCase: BaseTestCase {
  26. let user = "user"
  27. let password = "password"
  28. var URLString = ""
  29. var manager: Manager!
  30. override func setUp() {
  31. super.setUp()
  32. manager = Manager(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
  33. // Clear out credentials
  34. let credentialStorage = NSURLCredentialStorage.sharedCredentialStorage()
  35. for (protectionSpace, credentials) in credentialStorage.allCredentials {
  36. for (_, credential) in credentials {
  37. credentialStorage.removeCredential(credential, forProtectionSpace: protectionSpace)
  38. }
  39. }
  40. // Clear out cookies
  41. let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
  42. cookieStorage.cookies?.forEach { cookieStorage.deleteCookie($0) }
  43. }
  44. }
  45. // MARK: -
  46. class BasicAuthenticationTestCase: AuthenticationTestCase {
  47. override func setUp() {
  48. super.setUp()
  49. URLString = "https://httpbin.org/basic-auth/\(user)/\(password)"
  50. }
  51. func testHTTPBasicAuthenticationWithInvalidCredentials() {
  52. // Given
  53. let expectation = expectationWithDescription("\(URLString) 401")
  54. var request: NSURLRequest?
  55. var response: NSHTTPURLResponse?
  56. var data: NSData?
  57. var error: NSError?
  58. // When
  59. manager.request(.GET, URLString)
  60. .authenticate(user: "invalid", password: "credentials")
  61. .response { responseRequest, responseResponse, responseData, responseError in
  62. request = responseRequest
  63. response = responseResponse
  64. data = responseData
  65. error = responseError
  66. expectation.fulfill()
  67. }
  68. waitForExpectationsWithTimeout(timeout, handler: nil)
  69. // Then
  70. XCTAssertNotNil(request, "request should not be nil")
  71. XCTAssertNil(response, "response should be nil")
  72. XCTAssertNotNil(data, "data should not be nil")
  73. XCTAssertNotNil(error, "error should not be nil")
  74. if let code = error?.code {
  75. XCTAssertEqual(code, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  76. }
  77. }
  78. func testHTTPBasicAuthenticationWithValidCredentials() {
  79. // Given
  80. let expectation = expectationWithDescription("\(URLString) 200")
  81. var request: NSURLRequest?
  82. var response: NSHTTPURLResponse?
  83. var data: NSData?
  84. var error: NSError?
  85. // When
  86. manager.request(.GET, URLString)
  87. .authenticate(user: user, password: password)
  88. .response { responseRequest, responseResponse, responseData, responseError in
  89. request = responseRequest
  90. response = responseResponse
  91. data = responseData
  92. error = responseError
  93. expectation.fulfill()
  94. }
  95. waitForExpectationsWithTimeout(timeout, handler: nil)
  96. // Then
  97. XCTAssertNotNil(request, "request should not be nil")
  98. XCTAssertNotNil(response, "response should not be nil")
  99. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  100. XCTAssertNotNil(data, "data should not be nil")
  101. XCTAssertNil(error, "error should be nil")
  102. }
  103. }
  104. // MARK: -
  105. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  106. let qop = "auth"
  107. override func setUp() {
  108. super.setUp()
  109. URLString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  110. }
  111. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  112. // Given
  113. let expectation = expectationWithDescription("\(URLString) 401")
  114. var request: NSURLRequest?
  115. var response: NSHTTPURLResponse?
  116. var data: NSData?
  117. var error: NSError?
  118. // When
  119. manager.request(.GET, URLString)
  120. .authenticate(user: "invalid", password: "credentials")
  121. .response { responseRequest, responseResponse, responseData, responseError in
  122. request = responseRequest
  123. response = responseResponse
  124. data = responseData
  125. error = responseError
  126. expectation.fulfill()
  127. }
  128. waitForExpectationsWithTimeout(timeout, handler: nil)
  129. // Then
  130. XCTAssertNotNil(request, "request should not be nil")
  131. XCTAssertNil(response, "response should be nil")
  132. XCTAssertNotNil(data, "data should not be nil")
  133. XCTAssertNotNil(error, "error should not be nil")
  134. if let code = error?.code {
  135. XCTAssertEqual(code, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  136. }
  137. }
  138. func testHTTPDigestAuthenticationWithValidCredentials() {
  139. // Given
  140. let expectation = expectationWithDescription("\(URLString) 200")
  141. var request: NSURLRequest?
  142. var response: NSHTTPURLResponse?
  143. var data: NSData?
  144. var error: NSError?
  145. // When
  146. manager.request(.GET, URLString)
  147. .authenticate(user: user, password: password)
  148. .response { responseRequest, responseResponse, responseData, responseError in
  149. request = responseRequest
  150. response = responseResponse
  151. data = responseData
  152. error = responseError
  153. expectation.fulfill()
  154. }
  155. waitForExpectationsWithTimeout(timeout, handler: nil)
  156. // Then
  157. XCTAssertNotNil(request, "request should not be nil")
  158. XCTAssertNotNil(response, "response should not be nil")
  159. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  160. XCTAssertNotNil(data, "data should not be nil")
  161. XCTAssertNil(error, "error should be nil")
  162. }
  163. }