RequestModifierTests.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // RequestModifierTests.swift
  3. //
  4. // Copyright (c) 2020 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. import Alamofire
  25. import XCTest
  26. final class RequestModifierTests: BaseTestCase {
  27. // MARK: - DataRequest
  28. @MainActor
  29. func testThatDataRequestsCanHaveCustomTimeoutValueSet() {
  30. // Given
  31. let completed = expectation(description: "request completed")
  32. let modified = expectation(description: "request should be modified")
  33. var response: AFDataResponse<Data?>?
  34. // When
  35. AF.request(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
  36. .response { response = $0; completed.fulfill() }
  37. waitForExpectations(timeout: timeout)
  38. // Then
  39. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  40. }
  41. @MainActor
  42. func testThatDataRequestsCallRequestModifiersOnRetry() {
  43. // Given
  44. let inspector = InspectorInterceptor(RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0))
  45. let session = Session(interceptor: inspector)
  46. let completed = expectation(description: "request completed")
  47. let modified = expectation(description: "request should be modified twice")
  48. modified.expectedFulfillmentCount = 2
  49. var response: AFDataResponse<Data?>?
  50. // When
  51. session.request(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
  52. .response { response = $0; completed.fulfill() }
  53. waitForExpectations(timeout: timeout)
  54. // Then
  55. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  56. XCTAssertEqual(inspector.retryCalledCount, 2)
  57. }
  58. // MARK: - UploadRequest
  59. @MainActor
  60. func testThatUploadRequestsCanHaveCustomTimeoutValueSet() {
  61. // Given
  62. let endpoint = Endpoint.delay(1).modifying(\.method, to: .post)
  63. let data = Data("data".utf8)
  64. let completed = expectation(description: "request completed")
  65. let modified = expectation(description: "request should be modified")
  66. var response: AFDataResponse<Data?>?
  67. // When
  68. AF.upload(data, to: endpoint) { $0.timeoutInterval = 0.01; modified.fulfill() }
  69. .response { response = $0; completed.fulfill() }
  70. waitForExpectations(timeout: timeout)
  71. // Then
  72. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  73. }
  74. @MainActor
  75. func testThatUploadRequestsCallRequestModifiersOnRetry() {
  76. // Given
  77. let endpoint = Endpoint.delay(1).modifying(\.method, to: .post)
  78. let data = Data("data".utf8)
  79. let policy = RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0, retryableHTTPMethods: [.post])
  80. let inspector = InspectorInterceptor(policy)
  81. let session = Session(interceptor: inspector)
  82. let completed = expectation(description: "request completed")
  83. let modified = expectation(description: "request should be modified twice")
  84. modified.expectedFulfillmentCount = 2
  85. var response: AFDataResponse<Data?>?
  86. // When
  87. session.upload(data, to: endpoint) { $0.timeoutInterval = 0.01; modified.fulfill() }
  88. .response { response = $0; completed.fulfill() }
  89. waitForExpectations(timeout: timeout)
  90. // Then
  91. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  92. XCTAssertEqual(inspector.retryCalledCount, 2)
  93. }
  94. // MARK: - DownloadRequest
  95. @MainActor
  96. func testThatDownloadRequestsCanHaveCustomTimeoutValueSet() {
  97. // Given
  98. let url = Endpoint.delay(1).url
  99. let completed = expectation(description: "request completed")
  100. let modified = expectation(description: "request should be modified")
  101. var response: AFDownloadResponse<URL?>?
  102. // When
  103. AF.download(url, requestModifier: { $0.timeoutInterval = 0.01; modified.fulfill() })
  104. .response { response = $0; completed.fulfill() }
  105. waitForExpectations(timeout: timeout)
  106. // Then
  107. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  108. }
  109. @MainActor
  110. func testThatDownloadRequestsCallRequestModifiersOnRetry() {
  111. // Given
  112. let inspector = InspectorInterceptor(RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0))
  113. let session = Session(interceptor: inspector)
  114. let completed = expectation(description: "request completed")
  115. let modified = expectation(description: "request should be modified twice")
  116. modified.expectedFulfillmentCount = 2
  117. var response: AFDownloadResponse<URL?>?
  118. // When
  119. session.download(.delay(1), requestModifier: { $0.timeoutInterval = 0.01; modified.fulfill() })
  120. .response { response = $0; completed.fulfill() }
  121. waitForExpectations(timeout: timeout)
  122. // Then
  123. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  124. XCTAssertEqual(inspector.retryCalledCount, 2)
  125. }
  126. // MARK: - DataStreamRequest
  127. @MainActor
  128. func testThatDataStreamRequestsCanHaveCustomTimeoutValueSet() {
  129. // Given
  130. let completed = expectation(description: "request completed")
  131. let modified = expectation(description: "request should be modified")
  132. var response: DataStreamRequest.Completion?
  133. // When
  134. AF.streamRequest(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
  135. .responseStream { stream in
  136. guard case let .complete(completion) = stream.event else { return }
  137. response = completion
  138. completed.fulfill()
  139. }
  140. waitForExpectations(timeout: timeout)
  141. // Then
  142. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  143. }
  144. @MainActor
  145. func testThatDataStreamRequestsCallRequestModifiersOnRetry() {
  146. // Given
  147. let inspector = InspectorInterceptor(RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0))
  148. let session = Session(interceptor: inspector)
  149. let completed = expectation(description: "request completed")
  150. let modified = expectation(description: "request should be modified twice")
  151. modified.expectedFulfillmentCount = 2
  152. var response: DataStreamRequest.Completion?
  153. // When
  154. session.streamRequest(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
  155. .responseStream { stream in
  156. guard case let .complete(completion) = stream.event else { return }
  157. response = completion
  158. completed.fulfill()
  159. }
  160. waitForExpectations(timeout: timeout)
  161. // Then
  162. XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
  163. XCTAssertEqual(inspector.retryCalledCount, 2)
  164. }
  165. }