AuthenticationTests.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 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. // MARK: Setup and Teardown
  44. override func setUp() {
  45. super.setUp()
  46. self.URLString = "http://httpbin.org/basic-auth/\(user)/\(password)"
  47. }
  48. // MARK: Tests
  49. func testHTTPBasicAuthenticationWithInvalidCredentials() {
  50. // Given
  51. let expectation = expectationWithDescription("\(self.URLString) 401")
  52. var request: NSURLRequest?
  53. var response: NSHTTPURLResponse?
  54. var data: AnyObject?
  55. var error: NSError?
  56. // When
  57. Alamofire.request(.GET, URLString: self.URLString)
  58. .authenticate(user: "invalid", password: "credentials")
  59. .response { responseRequest, responseResponse, responseData, responseError in
  60. request = responseRequest
  61. response = responseResponse
  62. data = responseData
  63. error = responseError
  64. expectation.fulfill()
  65. }
  66. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  67. // Then
  68. XCTAssertNotNil(request, "request should not be nil")
  69. XCTAssertNil(response, "response should be nil")
  70. XCTAssertNotNil(data, "data should not be nil")
  71. XCTAssertNotNil(error, "error should not be nil")
  72. XCTAssertEqual(error?.code ?? 0, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  73. }
  74. func testHTTPBasicAuthenticationWithValidCredentials() {
  75. // Given
  76. let expectation = expectationWithDescription("\(self.URLString) 200")
  77. var request: NSURLRequest?
  78. var response: NSHTTPURLResponse?
  79. var data: AnyObject?
  80. var error: NSError?
  81. // When
  82. Alamofire.request(.GET, URLString: self.URLString)
  83. .authenticate(user: self.user, password: self.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(self.defaultTimeout, 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. // MARK: Properties
  103. let qop = "auth"
  104. // MARK: Setup and Teardown
  105. override func setUp() {
  106. super.setUp()
  107. self.URLString = "http://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  108. }
  109. // MARK: Tests
  110. func testHTTPDigestAuthenticationWithInvalidCredentials() {
  111. // Given
  112. let expectation = expectationWithDescription("\(self.URLString) 401")
  113. var request: NSURLRequest?
  114. var response: NSHTTPURLResponse?
  115. var data: AnyObject?
  116. var error: NSError?
  117. // When
  118. Alamofire.request(.GET, URLString: self.URLString)
  119. .authenticate(user: "invalid", password: "credentials")
  120. .response { responseRequest, responseResponse, responseData, responseError in
  121. request = responseRequest
  122. response = responseResponse
  123. data = responseData
  124. error = responseError
  125. expectation.fulfill()
  126. }
  127. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  128. // Then
  129. XCTAssertNotNil(request, "request should not be nil")
  130. XCTAssertNil(response, "response should be nil")
  131. XCTAssertNotNil(data, "data should not be nil")
  132. XCTAssertNotNil(error, "error should not be nil")
  133. XCTAssertEqual(error?.code ?? 0, -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  134. }
  135. func testHTTPDigestAuthenticationWithValidCredentials() {
  136. // Given
  137. let expectation = expectationWithDescription("\(self.URLString) 200")
  138. var request: NSURLRequest?
  139. var response: NSHTTPURLResponse?
  140. var data: AnyObject?
  141. var error: NSError?
  142. // When
  143. Alamofire.request(.GET, URLString: self.URLString)
  144. .authenticate(user: self.user, password: self.password)
  145. .response { responseRequest, responseResponse, responseData, responseError in
  146. request = responseRequest
  147. response = responseResponse
  148. data = responseData
  149. error = responseError
  150. expectation.fulfill()
  151. }
  152. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  153. // Then
  154. XCTAssertNotNil(request, "request should not be nil")
  155. XCTAssertNotNil(response, "response should not be nil")
  156. XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
  157. XCTAssertNotNil(data, "data should not be nil")
  158. XCTAssertNil(error, "error should be nil")
  159. }
  160. }