SessionDelegateTests.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // SessionDelegateTests.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. @testable import Alamofire
  25. import Foundation
  26. import XCTest
  27. class SessionDelegateTestCase: BaseTestCase {
  28. var manager: Session!
  29. // MARK: - Setup and Teardown
  30. override func setUp() {
  31. super.setUp()
  32. manager = Session(configuration: .ephemeral)
  33. }
  34. // MARK: - Tests - Redirects
  35. // Disabled due to HTTPBin issue: https://github.com/postmanlabs/httpbin/issues/617
  36. func _testThatRequestWillPerformHTTPRedirectionByDefault() {
  37. // Given
  38. let redirectURLString = "https://www.apple.com/"
  39. let urlString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  40. let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
  41. var response: DataResponse<Data?, AFError>?
  42. // When
  43. manager.request(urlString)
  44. .response { resp in
  45. response = resp
  46. expectation.fulfill()
  47. }
  48. waitForExpectations(timeout: timeout, handler: nil)
  49. // Then
  50. XCTAssertNotNil(response?.request)
  51. XCTAssertNotNil(response?.response)
  52. XCTAssertNotNil(response?.data)
  53. XCTAssertNil(response?.error)
  54. XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
  55. XCTAssertEqual(response?.response?.statusCode, 200)
  56. }
  57. // Disabled due to HTTPBin issue: https://github.com/postmanlabs/httpbin/issues/617
  58. func _testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
  59. // Given
  60. let redirectURLString = "https://httpbin.org/get"
  61. let urlString = "https://httpbin.org/redirect/5"
  62. let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
  63. var response: DataResponse<Data?, AFError>?
  64. // When
  65. manager.request(urlString)
  66. .response { resp in
  67. response = resp
  68. expectation.fulfill()
  69. }
  70. waitForExpectations(timeout: timeout, handler: nil)
  71. // Then
  72. XCTAssertNotNil(response?.request)
  73. XCTAssertNotNil(response?.response)
  74. XCTAssertNotNil(response?.data)
  75. XCTAssertNil(response?.error)
  76. XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
  77. XCTAssertEqual(response?.response?.statusCode, 200)
  78. }
  79. // MARK: - Tests - Notification
  80. func testThatAppropriateNotificationsAreCalledWithRequestForDataRequest() {
  81. // Given
  82. let session = Session(startRequestsImmediately: false)
  83. var resumedRequest: Request?
  84. var resumedTaskRequest: Request?
  85. var completedTaskRequest: Request?
  86. var completedRequest: Request?
  87. var requestResponse: DataResponse<Data?, AFError>?
  88. let expect = expectation(description: "request should complete")
  89. // When
  90. let request = session.request("https://httpbin.org/get").response { response in
  91. requestResponse = response
  92. expect.fulfill()
  93. }
  94. expectation(forNotification: Request.didResumeNotification, object: nil) { notification in
  95. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  96. resumedRequest = notification.request
  97. return true
  98. }
  99. expectation(forNotification: Request.didResumeTaskNotification, object: nil) { notification in
  100. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  101. resumedTaskRequest = notification.request
  102. return true
  103. }
  104. expectation(forNotification: Request.didCompleteTaskNotification, object: nil) { notification in
  105. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  106. completedTaskRequest = notification.request
  107. return true
  108. }
  109. expectation(forNotification: Request.didFinishNotification, object: nil) { notification in
  110. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  111. completedRequest = notification.request
  112. return true
  113. }
  114. request.resume()
  115. waitForExpectations(timeout: timeout, handler: nil)
  116. // Then
  117. XCTAssertNotNil(resumedRequest)
  118. XCTAssertNotNil(resumedTaskRequest)
  119. XCTAssertNotNil(completedTaskRequest)
  120. XCTAssertNotNil(completedRequest)
  121. XCTAssertEqual(resumedRequest, completedRequest)
  122. XCTAssertEqual(resumedTaskRequest, completedTaskRequest)
  123. XCTAssertEqual(requestResponse?.response?.statusCode, 200)
  124. }
  125. func testThatDidCompleteNotificationIsCalledWithRequestForDownloadRequests() {
  126. // Given
  127. let session = Session(startRequestsImmediately: false)
  128. var resumedRequest: Request?
  129. var resumedTaskRequest: Request?
  130. var completedTaskRequest: Request?
  131. var completedRequest: Request?
  132. var requestResponse: DownloadResponse<URL?, AFError>?
  133. let expect = expectation(description: "request should complete")
  134. // When
  135. let request = session.download("https://httpbin.org/get").response { response in
  136. requestResponse = response
  137. expect.fulfill()
  138. }
  139. expectation(forNotification: Request.didResumeNotification, object: nil) { notification in
  140. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  141. resumedRequest = notification.request
  142. return true
  143. }
  144. expectation(forNotification: Request.didResumeTaskNotification, object: nil) { notification in
  145. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  146. resumedTaskRequest = notification.request
  147. return true
  148. }
  149. expectation(forNotification: Request.didCompleteTaskNotification, object: nil) { notification in
  150. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  151. completedTaskRequest = notification.request
  152. return true
  153. }
  154. expectation(forNotification: Request.didFinishNotification, object: nil) { notification in
  155. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  156. completedRequest = notification.request
  157. return true
  158. }
  159. request.resume()
  160. waitForExpectations(timeout: timeout, handler: nil)
  161. // Then
  162. XCTAssertNotNil(resumedRequest)
  163. XCTAssertNotNil(resumedTaskRequest)
  164. XCTAssertNotNil(completedTaskRequest)
  165. XCTAssertNotNil(completedRequest)
  166. XCTAssertEqual(resumedRequest, completedRequest)
  167. XCTAssertEqual(resumedTaskRequest, completedTaskRequest)
  168. XCTAssertEqual(requestResponse?.response?.statusCode, 200)
  169. }
  170. }