AuthenticationTests.swift 6.9 KB

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