RequestModifierTests.swift 7.9 KB

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