AuthenticationTests.swift 6.6 KB

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