ManagerTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // ManagerTests.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 ManagerTestCase: BaseTestCase {
  26. func testSetStartRequestsImmediatelyToFalseAndResumeRequest() {
  27. // Given
  28. let manager = Alamofire.Manager()
  29. manager.startRequestsImmediately = false
  30. let URL = NSURL(string: "https://httpbin.org/get")!
  31. let URLRequest = NSURLRequest(URL: URL)
  32. let expectation = expectationWithDescription("\(URL)")
  33. var response: NSHTTPURLResponse?
  34. // When
  35. manager.request(URLRequest)
  36. .response { _, responseResponse, _, _ in
  37. response = responseResponse
  38. expectation.fulfill()
  39. }
  40. .resume()
  41. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  42. // Then
  43. XCTAssertNotNil(response, "response should not be nil")
  44. XCTAssertTrue(response?.statusCode == 200, "response status code should be 200")
  45. }
  46. func testReleasingManagerWithPendingRequestDeinitializesSuccessfully() {
  47. // Given
  48. var manager: Manager? = Alamofire.Manager()
  49. manager?.startRequestsImmediately = false
  50. let URL = NSURL(string: "https://httpbin.org/get")!
  51. let URLRequest = NSURLRequest(URL: URL)
  52. // When
  53. let request = manager?.request(URLRequest)
  54. manager = nil
  55. // Then
  56. XCTAssertTrue(request?.task.state == .Suspended, "request task state should be '.Suspended'")
  57. XCTAssertNil(manager, "manager should be nil")
  58. }
  59. func testReleasingManagerWithPendingCanceledRequestDeinitializesSuccessfully() {
  60. // Given
  61. var manager: Manager? = Alamofire.Manager()
  62. manager!.startRequestsImmediately = false
  63. let URL = NSURL(string: "https://httpbin.org/get")!
  64. let URLRequest = NSURLRequest(URL: URL)
  65. // When
  66. let request = manager!.request(URLRequest)
  67. request.cancel()
  68. manager = nil
  69. // Then
  70. let state = request.task.state
  71. XCTAssertTrue(state == .Canceling || state == .Completed, "state should be .Canceling or .Completed")
  72. XCTAssertNil(manager, "manager should be nil")
  73. }
  74. }