| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- //
- // RequestModifierTests.swift
- //
- // Copyright (c) 2020 Alamofire Software Foundation (http://alamofire.org/)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- import Alamofire
- import XCTest
- final class RequestModifierTests: BaseTestCase {
- // MARK: - DataRequest
- func testThatDataRequestsCanHaveCustomTimeoutValueSet() {
- // Given
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified")
- var response: AFDataResponse<Data?>?
- // When
- AF.request(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
- .response { response = $0; completed.fulfill() }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- }
- func testThatDataRequestsCallRequestModifiersOnRetry() {
- // Given
- let inspector = InspectorInterceptor(RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0))
- let session = Session(interceptor: inspector)
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified twice")
- modified.expectedFulfillmentCount = 2
- var response: AFDataResponse<Data?>?
- // When
- session.request(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
- .response { response = $0; completed.fulfill() }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- XCTAssertEqual(inspector.retryCalledCount, 2)
- }
- // MARK: - UploadRequest
- func testThatUploadRequestsCanHaveCustomTimeoutValueSet() {
- // Given
- let endpoint = Endpoint.delay(1).modifying(\.method, to: .post)
- let data = Data("data".utf8)
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified")
- var response: AFDataResponse<Data?>?
- // When
- AF.upload(data, to: endpoint) { $0.timeoutInterval = 0.01; modified.fulfill() }
- .response { response = $0; completed.fulfill() }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- }
- func testThatUploadRequestsCallRequestModifiersOnRetry() {
- // Given
- let endpoint = Endpoint.delay(1).modifying(\.method, to: .post)
- let data = Data("data".utf8)
- let policy = RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0, retryableHTTPMethods: [.post])
- let inspector = InspectorInterceptor(policy)
- let session = Session(interceptor: inspector)
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified twice")
- modified.expectedFulfillmentCount = 2
- var response: AFDataResponse<Data?>?
- // When
- session.upload(data, to: endpoint) { $0.timeoutInterval = 0.01; modified.fulfill() }
- .response { response = $0; completed.fulfill() }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- XCTAssertEqual(inspector.retryCalledCount, 2)
- }
- // MARK: - DownloadRequest
- func testThatDownloadRequestsCanHaveCustomTimeoutValueSet() {
- // Given
- let url = Endpoint.delay(1).url
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified")
- var response: AFDownloadResponse<URL?>?
- // When
- AF.download(url, requestModifier: { $0.timeoutInterval = 0.01; modified.fulfill() })
- .response { response = $0; completed.fulfill() }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- }
- func testThatDownloadRequestsCallRequestModifiersOnRetry() {
- // Given
- let inspector = InspectorInterceptor(RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0))
- let session = Session(interceptor: inspector)
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified twice")
- modified.expectedFulfillmentCount = 2
- var response: AFDownloadResponse<URL?>?
- // When
- session.download(.delay(1), requestModifier: { $0.timeoutInterval = 0.01; modified.fulfill() })
- .response { response = $0; completed.fulfill() }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- XCTAssertEqual(inspector.retryCalledCount, 2)
- }
- // MARK: - DataStreamRequest
- func testThatDataStreamRequestsCanHaveCustomTimeoutValueSet() {
- // Given
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified")
- var response: DataStreamRequest.Completion?
- // When
- AF.streamRequest(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
- .responseStream { stream in
- guard case let .complete(completion) = stream.event else { return }
- response = completion
- completed.fulfill()
- }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- }
- func testThatDataStreamRequestsCallRequestModifiersOnRetry() {
- // Given
- let inspector = InspectorInterceptor(RetryPolicy(retryLimit: 1, exponentialBackoffScale: 0))
- let session = Session(interceptor: inspector)
- let completed = expectation(description: "request completed")
- let modified = expectation(description: "request should be modified twice")
- modified.expectedFulfillmentCount = 2
- var response: DataStreamRequest.Completion?
- // When
- session.streamRequest(.delay(1)) { $0.timeoutInterval = 0.01; modified.fulfill() }
- .responseStream { stream in
- guard case let .complete(completion) = stream.event else { return }
- response = completion
- completed.fulfill()
- }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual((response?.error?.underlyingError as? URLError)?.code, .timedOut)
- XCTAssertEqual(inspector.retryCalledCount, 2)
- }
- }
|