RequestModifierTests.swift 7.7 KB

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