AuthenticationTests.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // AuthenticationTests.swift
  2. //
  3. // Copyright (c) 2014–2015 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(defaultTimeout, 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. XCTAssertEqual(error?.code ?? 0, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  69. }
  70. func testHTTPBasicAuthenticationWithValidCredentials() {
  71. // Given
  72. let expectation = expectationWithDescription("\(URLString) 200")
  73. var request: NSURLRequest?
  74. var response: NSHTTPURLResponse?
  75. var data: NSData?
  76. var error: NSError?
  77. // When
  78. Alamofire.request(.GET, URLString)
  79. .authenticate(user: user, password: password)
  80. .response { responseRequest, responseResponse, responseData, responseError in
  81. request = responseRequest
  82. response = responseResponse
  83. data = responseData
  84. error = responseError
  85. expectation.fulfill()
  86. }
  87. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  88. // Then
  89. XCTAssertNotNil(request, "request should not be nil")
  90. XCTAssertNotNil(response, "response should not be nil")
  91. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  92. XCTAssertNotNil(data, "data should not be nil")
  93. XCTAssertNil(error, "error should be nil")
  94. }
  95. }
  96. // MARK: -
  97. class HTTPDigestAuthenticationTestCase: AuthenticationTestCase {
  98. let qop = "auth"
  99. override func setUp() {
  100. super.setUp()
  101. URLString = "https://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  102. }
  103. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  104. // Given
  105. let expectation = expectationWithDescription("\(URLString) 401")
  106. var request: NSURLRequest?
  107. var response: NSHTTPURLResponse?
  108. var data: NSData?
  109. var error: NSError?
  110. // When
  111. Alamofire.request(.GET, URLString)
  112. .authenticate(user: "invalid", password: "credentials")
  113. .response { responseRequest, responseResponse, responseData, responseError in
  114. request = responseRequest
  115. response = responseResponse
  116. data = responseData
  117. error = responseError
  118. expectation.fulfill()
  119. }
  120. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  121. // Then
  122. XCTAssertNotNil(request, "request should not be nil")
  123. XCTAssertNil(response, "response should be nil")
  124. XCTAssertNotNil(data, "data should not be nil")
  125. XCTAssertNotNil(error, "error should not be nil")
  126. XCTAssertEqual(error?.code ?? 0, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  127. }
  128. func testHTTPDigestAuthenticationWithValidCredentials() {
  129. // Given
  130. let expectation = expectationWithDescription("\(URLString) 200")
  131. var request: NSURLRequest?
  132. var response: NSHTTPURLResponse?
  133. var data: NSData?
  134. var error: NSError?
  135. // When
  136. Alamofire.request(.GET, URLString)
  137. .authenticate(user: user, password: password)
  138. .response { responseRequest, responseResponse, responseData, responseError in
  139. request = responseRequest
  140. response = responseResponse
  141. data = responseData
  142. error = responseError
  143. expectation.fulfill()
  144. }
  145. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  146. // Then
  147. XCTAssertNotNil(request, "request should not be nil")
  148. XCTAssertNotNil(response, "response should not be nil")
  149. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  150. XCTAssertNotNil(data, "data should not be nil")
  151. XCTAssertNil(error, "error should be nil")
  152. }
  153. }