AuthenticationTests.swift 7.0 KB

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