SessionDelegateTests.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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?>?
  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?>?
  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. func testThatAppropriateNotificationsAreCalledWithRequestForDataRequest() {
  78. // Given
  79. let session = Session(startRequestsImmediately: false)
  80. var resumedRequest: Request?
  81. var completedRequest: Request?
  82. var requestResponse: DataResponse<Data?>?
  83. let expect = expectation(description: "request should complete")
  84. // When
  85. let request = session.request("https://httpbin.org/get").response { (response) in
  86. requestResponse = response
  87. expect.fulfill()
  88. }
  89. expectation(forNotification: Request.didResume, object: nil) { (notification) in
  90. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  91. resumedRequest = notification.request
  92. return true
  93. }
  94. expectation(forNotification: Request.didFinish, object: nil) { (notification) in
  95. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  96. completedRequest = notification.request
  97. return true
  98. }
  99. request.resume()
  100. waitForExpectations(timeout: timeout, handler: nil)
  101. // Then
  102. XCTAssertEqual(resumedRequest, completedRequest)
  103. XCTAssertEqual(requestResponse?.response?.statusCode, 200)
  104. }
  105. func testThatDidCompleteNotificationIsCalledWithRequestForDownloadRequests() {
  106. // Given
  107. let session = Session(startRequestsImmediately: false)
  108. var resumedRequest: Request?
  109. var completedRequest: Request?
  110. var requestResponse: DownloadResponse<URL?>?
  111. let expect = expectation(description: "request should complete")
  112. // When
  113. let request = session.download("https://httpbin.org/get").response { (response) in
  114. requestResponse = response
  115. expect.fulfill()
  116. }
  117. expectation(forNotification: Request.didResume, object: nil) { (notification) in
  118. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  119. resumedRequest = notification.request
  120. return true
  121. }
  122. expectation(forNotification: Request.didFinish, object: nil) { (notification) in
  123. guard let receivedRequest = notification.request, receivedRequest == request else { return false }
  124. completedRequest = notification.request
  125. return true
  126. }
  127. request.resume()
  128. waitForExpectations(timeout: timeout, handler: nil)
  129. // Then
  130. XCTAssertEqual(resumedRequest, completedRequest)
  131. XCTAssertEqual(requestResponse?.response?.statusCode, 200)
  132. }
  133. }