ManagerTests.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. }
  75. // MARK: -
  76. class ManagerConfigurationHeadersTestCase: BaseTestCase {
  77. enum ConfigurationType {
  78. case Default, Ephemeral, Background
  79. }
  80. func testThatDefaultConfigurationHeadersAreSentWithRequest() {
  81. // Given, When, Then
  82. executeAuthorizationHeaderTestForConfigurationType(.Default)
  83. }
  84. func testThatEphemeralConfigurationHeadersAreSentWithRequest() {
  85. // Given, When, Then
  86. executeAuthorizationHeaderTestForConfigurationType(.Ephemeral)
  87. }
  88. func testThatBackgroundConfigurationHeadersAreSentWithRequest() {
  89. // Given, When, Then
  90. executeAuthorizationHeaderTestForConfigurationType(.Background)
  91. }
  92. private func executeAuthorizationHeaderTestForConfigurationType(type: ConfigurationType) {
  93. // Given
  94. let manager: Manager = {
  95. let configuration: NSURLSessionConfiguration = {
  96. let configuration: NSURLSessionConfiguration
  97. switch type {
  98. case .Default:
  99. configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
  100. case .Ephemeral:
  101. configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
  102. case .Background:
  103. let identifier = "com.alamofire.test.manager-configuration-tests"
  104. #if os(iOS) || os(watchOS)
  105. configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier)
  106. #else
  107. if #available(OSX 10.10, *) {
  108. configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier)
  109. } else {
  110. configuration = NSURLSessionConfiguration.backgroundSessionConfiguration(identifier)
  111. }
  112. #endif
  113. }
  114. var headers = Alamofire.Manager.defaultHTTPHeaders
  115. headers["Authorization"] = "Bearer 123456"
  116. configuration.HTTPAdditionalHeaders = headers
  117. return configuration
  118. }()
  119. return Manager(configuration: configuration)
  120. }()
  121. let expectation = expectationWithDescription("request should complete successfully")
  122. var request: NSURLRequest?
  123. var response: NSHTTPURLResponse?
  124. var result: Result<AnyObject>?
  125. // When
  126. manager.request(.GET, "https://httpbin.org/headers")
  127. .responseJSON { responseRequest, responseResponse, responseResult in
  128. request = responseRequest
  129. response = responseResponse
  130. result = responseResult
  131. expectation.fulfill()
  132. }
  133. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  134. // Then
  135. XCTAssertNotNil(request, "request should not be nil")
  136. XCTAssertNotNil(response, "response should not be nil")
  137. if let result = result {
  138. XCTAssertTrue(result.isSuccess, "result should be a success")
  139. if let
  140. headers = result.value?["headers" as NSString] as? [String: String],
  141. authorization = headers["Authorization"]
  142. {
  143. XCTAssertEqual(authorization, "Bearer 123456", "authorization header value does not match")
  144. } else {
  145. XCTFail("failed to extract authorization header value")
  146. }
  147. } else {
  148. XCTFail("result should not be nil")
  149. }
  150. }
  151. }