SessionDelegateTests.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. func testThatRequestWillPerformHTTPRedirectionByDefault() {
  36. // Given
  37. let redirectURLString = "https://www.apple.com/"
  38. let urlString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  39. let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
  40. var response: DataResponse<Data?, AFError>?
  41. // When
  42. manager.request(urlString)
  43. .response { resp in
  44. response = resp
  45. expectation.fulfill()
  46. }
  47. waitForExpectations(timeout: timeout, handler: nil)
  48. // Then
  49. XCTAssertNotNil(response?.request)
  50. XCTAssertNotNil(response?.response)
  51. XCTAssertNotNil(response?.data)
  52. XCTAssertNil(response?.error)
  53. XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
  54. XCTAssertEqual(response?.response?.statusCode, 200)
  55. }
  56. func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
  57. // Given
  58. let redirectURLString = "https://httpbin.org/get"
  59. let urlString = "https://httpbin.org/redirect/5"
  60. let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
  61. var response: DataResponse<Data?, AFError>?
  62. // When
  63. manager.request(urlString)
  64. .response { resp in
  65. response = resp
  66. expectation.fulfill()
  67. }
  68. waitForExpectations(timeout: timeout, handler: nil)
  69. // Then
  70. XCTAssertNotNil(response?.request)
  71. XCTAssertNotNil(response?.response)
  72. XCTAssertNotNil(response?.data)
  73. XCTAssertNil(response?.error)
  74. XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
  75. XCTAssertEqual(response?.response?.statusCode, 200)
  76. }
  77. // MARK: - Tests - Notification
  78. func testThatAppropriateNotificationsAreCalledWithRequestForDataRequest() {
  79. // Given
  80. let session = Session(startRequestsImmediately: false)
  81. var resumedRequest: Request?
  82. var resumedTaskRequest: Request?
  83. var completedTaskRequest: Request?
  84. var completedRequest: Request?
  85. var requestResponse: DataResponse<Data?, AFError>?
  86. let expect = expectation(description: "request should complete")
  87. // When
  88. let request = session.request("https://httpbin.org/get").response { response in
  89. requestResponse = response
  90. expect.fulfill()
  91. }
  92. expectation(forNotification: Request.didResumeNotification, object: nil) { notification in
  93. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  94. resumedRequest = notification.request
  95. return true
  96. }
  97. expectation(forNotification: Request.didResumeTaskNotification, object: nil) { notification in
  98. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  99. resumedTaskRequest = notification.request
  100. return true
  101. }
  102. expectation(forNotification: Request.didCompleteTaskNotification, object: nil) { notification in
  103. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  104. completedTaskRequest = notification.request
  105. return true
  106. }
  107. expectation(forNotification: Request.didFinishNotification, object: nil) { notification in
  108. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  109. completedRequest = notification.request
  110. return true
  111. }
  112. request.resume()
  113. waitForExpectations(timeout: timeout, handler: nil)
  114. // Then
  115. XCTAssertNotNil(resumedRequest)
  116. XCTAssertNotNil(resumedTaskRequest)
  117. XCTAssertNotNil(completedTaskRequest)
  118. XCTAssertNotNil(completedRequest)
  119. XCTAssertEqual(resumedRequest, completedRequest)
  120. XCTAssertEqual(resumedTaskRequest, completedTaskRequest)
  121. XCTAssertEqual(requestResponse?.response?.statusCode, 200)
  122. }
  123. func testThatDidCompleteNotificationIsCalledWithRequestForDownloadRequests() {
  124. // Given
  125. let session = Session(startRequestsImmediately: false)
  126. var resumedRequest: Request?
  127. var resumedTaskRequest: Request?
  128. var completedTaskRequest: Request?
  129. var completedRequest: Request?
  130. var requestResponse: DownloadResponse<URL?, AFError>?
  131. let expect = expectation(description: "request should complete")
  132. // When
  133. let request = session.download("https://httpbin.org/get").response { response in
  134. requestResponse = response
  135. expect.fulfill()
  136. }
  137. expectation(forNotification: Request.didResumeNotification, object: nil) { notification in
  138. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  139. resumedRequest = notification.request
  140. return true
  141. }
  142. expectation(forNotification: Request.didResumeTaskNotification, object: nil) { notification in
  143. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  144. resumedTaskRequest = notification.request
  145. return true
  146. }
  147. expectation(forNotification: Request.didCompleteTaskNotification, object: nil) { notification in
  148. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  149. completedTaskRequest = notification.request
  150. return true
  151. }
  152. expectation(forNotification: Request.didFinishNotification, object: nil) { notification in
  153. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  154. completedRequest = notification.request
  155. return true
  156. }
  157. request.resume()
  158. waitForExpectations(timeout: timeout, handler: nil)
  159. // Then
  160. XCTAssertNotNil(resumedRequest)
  161. XCTAssertNotNil(resumedTaskRequest)
  162. XCTAssertNotNil(completedTaskRequest)
  163. XCTAssertNotNil(completedRequest)
  164. XCTAssertEqual(resumedRequest, completedRequest)
  165. XCTAssertEqual(resumedTaskRequest, completedTaskRequest)
  166. XCTAssertEqual(requestResponse?.response?.statusCode, 200)
  167. }
  168. }