2
0

AuthenticationTests.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = Alamofire.Manager(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
  33. let credentialStorage = NSURLCredentialStorage.sharedCredentialStorage()
  34. for (protectionSpace, credentials) in credentialStorage.allCredentials {
  35. for (_, credential) in credentials {
  36. credentialStorage.removeCredential(credential, forProtectionSpace: protectionSpace)
  37. }
  38. }
  39. }
  40. }
  41. // MARK: -
  42. class BasicAuthenticationTestCase: AuthenticationTestCase {
  43. override func setUp() {
  44. super.setUp()
  45. URLString = "https://httpbin.org/basic-auth/\(user)/\(password)"
  46. }
  47. func testHTTPBasicAuthenticationWithInvalidCredentials() {
  48. // Given
  49. let expectation = expectationWithDescription("\(URLString) 401")
  50. var request: NSURLRequest?
  51. var response: NSHTTPURLResponse?
  52. var data: NSData?
  53. var error: NSError?
  54. // When
  55. manager.request(.GET, URLString)
  56. .authenticate(user: "invalid", password: "credentials")
  57. .response { responseRequest, responseResponse, responseData, responseError in
  58. request = responseRequest
  59. response = responseResponse
  60. data = responseData
  61. error = responseError
  62. expectation.fulfill()
  63. }
  64. waitForExpectationsWithTimeout(timeout, handler: nil)
  65. // Then
  66. XCTAssertNotNil(request, "request should not be nil")
  67. XCTAssertNil(response, "response should be nil")
  68. XCTAssertNotNil(data, "data should not be nil")
  69. XCTAssertNotNil(error, "error should not be nil")
  70. if let code = error?.code {
  71. XCTAssertEqual(code, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  72. }
  73. }
  74. func testHTTPBasicAuthenticationWithValidCredentials() {
  75. // Given
  76. let expectation = expectationWithDescription("\(URLString) 200")
  77. var request: NSURLRequest?
  78. var response: NSHTTPURLResponse?
  79. var data: NSData?
  80. var error: NSError?
  81. // When
  82. manager.request(.GET, URLString)
  83. .authenticate(user: user, password: password)
  84. .response { responseRequest, responseResponse, responseData, responseError in
  85. request = responseRequest
  86. response = responseResponse
  87. data = responseData
  88. error = responseError
  89. expectation.fulfill()
  90. }
  91. waitForExpectationsWithTimeout(timeout, handler: nil)
  92. // Then
  93. XCTAssertNotNil(request, "request should not be nil")
  94. XCTAssertNotNil(response, "response should not be nil")
  95. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  96. XCTAssertNotNil(data, "data should not be nil")
  97. XCTAssertNil(error, "error should be nil")
  98. }
  99. }
  100. // MARK: -
  101. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  102. let qop = "auth"
  103. override func setUp() {
  104. super.setUp()
  105. URLString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  106. }
  107. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  108. // Given
  109. let expectation = expectationWithDescription("\(URLString) 401")
  110. var request: NSURLRequest?
  111. var response: NSHTTPURLResponse?
  112. var data: NSData?
  113. var error: NSError?
  114. // When
  115. manager.request(.GET, URLString)
  116. .authenticate(user: "invalid", password: "credentials")
  117. .response { responseRequest, responseResponse, responseData, responseError in
  118. request = responseRequest
  119. response = responseResponse
  120. data = responseData
  121. error = responseError
  122. expectation.fulfill()
  123. }
  124. waitForExpectationsWithTimeout(timeout, handler: nil)
  125. // Then
  126. XCTAssertNotNil(request, "request should not be nil")
  127. XCTAssertNil(response, "response should be nil")
  128. XCTAssertNotNil(data, "data should not be nil")
  129. XCTAssertNotNil(error, "error should not be nil")
  130. if let code = error?.code {
  131. XCTAssertEqual(code, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  132. }
  133. }
  134. func testHTTPDigestAuthenticationWithValidCredentials() {
  135. // Given
  136. let expectation = expectationWithDescription("\(URLString) 200")
  137. var request: NSURLRequest?
  138. var response: NSHTTPURLResponse?
  139. var data: NSData?
  140. var error: NSError?
  141. // When
  142. manager.request(.GET, URLString)
  143. .authenticate(user: user, password: password)
  144. .response { responseRequest, responseResponse, responseData, responseError in
  145. request = responseRequest
  146. response = responseResponse
  147. data = responseData
  148. error = responseError
  149. expectation.fulfill()
  150. }
  151. waitForExpectationsWithTimeout(timeout, handler: nil)
  152. // Then
  153. XCTAssertNotNil(request, "request should not be nil")
  154. XCTAssertNotNil(response, "response should not be nil")
  155. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  156. XCTAssertNotNil(data, "data should not be nil")
  157. XCTAssertNil(error, "error should be nil")
  158. }
  159. }