AuthenticationTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // DownloadTests.swift
  2. //
  3. // Copyright (c) 2014 Alamofire (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 Foundation
  23. import Alamofire
  24. import XCTest
  25. class AlamofireAuthenticationTestCase: XCTestCase {
  26. func testHTTPBasicAuthentication() {
  27. let user = "user"
  28. let password = "password"
  29. let URL = "http://httpbin.org/basic-auth/\(user)/\(password)"
  30. let invalidCredentialsExpectation = expectationWithDescription("\(URL) 401")
  31. Alamofire.request(.GET, URL)
  32. .authenticate(user: "invalid", password: "credentials")
  33. .response { (request, response, _, error) in
  34. invalidCredentialsExpectation.fulfill()
  35. XCTAssertNotNil(request, "request should not be nil")
  36. XCTAssertNil(response, "response should be nil")
  37. XCTAssertNotNil(error, "error should not be nil")
  38. XCTAssert(error?.code == -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  39. }
  40. let validCredentialsExpectation = expectationWithDescription("\(URL) 200")
  41. Alamofire.request(.GET, URL)
  42. .authenticate(user: user, password: password)
  43. .response { (request, response, _, error) in
  44. validCredentialsExpectation.fulfill()
  45. XCTAssertNotNil(request, "request should not be nil")
  46. XCTAssertNotNil(response, "response should not be nil")
  47. XCTAssert(response?.statusCode == 200, "response status code should be 200")
  48. XCTAssertNil(error, "error should be nil")
  49. }
  50. waitForExpectationsWithTimeout(10) { (error) in
  51. XCTAssertNil(error, "\(error)")
  52. }
  53. }
  54. func testHTTPDigestAuthentication() {
  55. let qop = "auth"
  56. let user = "user"
  57. let password = "password"
  58. let URL = "http://httpbin.org/digest-auth/\(qop)/\(user)/\(password)"
  59. let invalidCredentialsExpectation = expectationWithDescription("\(URL) 401")
  60. Alamofire.request(.GET, URL)
  61. .authenticate(user: "invalid", password: "credentials")
  62. .response { (request, response, _, error) in
  63. invalidCredentialsExpectation.fulfill()
  64. XCTAssertNotNil(request, "request should not be nil")
  65. XCTAssertNil(response, "response should be nil")
  66. XCTAssertNotNil(error, "error should not be nil")
  67. XCTAssert(error?.code == -999, "error should be NSURLErrorDomain Code -999 'cancelled'")
  68. }
  69. let validCredentialsExpectation = expectationWithDescription("\(URL) 200")
  70. Alamofire.request(.GET, URL)
  71. .authenticate(user: user, password: password)
  72. .response { (request, response, _, error) in
  73. validCredentialsExpectation.fulfill()
  74. XCTAssertNotNil(request, "request should not be nil")
  75. XCTAssertNotNil(response, "response should not be nil")
  76. XCTAssert(response?.statusCode == 200, "response status code should be 200")
  77. XCTAssertNil(error, "error should be nil")
  78. }
  79. waitForExpectationsWithTimeout(10) { (error) in
  80. XCTAssertNil(error, "\(error)")
  81. }
  82. }
  83. }