diff --git a/Sources/Relax/Request/Request.swift b/Sources/Relax/Request/Request.swift index f6728b5..d0f3e85 100644 --- a/Sources/Relax/Request/Request.swift +++ b/Sources/Relax/Request/Request.swift @@ -127,6 +127,7 @@ public struct Request: Hashable { _properties.headers.value.forEach { request.addValue($0.value, forHTTPHeaderField: $0.key) } request.httpBody = _properties.body.value + // configuration properties request.allowsCellularAccess = configuration.allowsCellularAccess request.cachePolicy = configuration.cachePolicy request.httpShouldUsePipelining = configuration.httpShouldUsePipelining @@ -134,6 +135,12 @@ public struct Request: Hashable { request.timeoutInterval = configuration.timeoutInterval request.httpShouldHandleCookies = configuration.httpShouldHandleCookies + // properties not available in FoundationNetworking (non-Apple) + #if !canImport(FoundationNetworking) + request.allowsConstrainedNetworkAccess = configuration.allowsConstrainedNetworkAccess + request.allowsExpensiveNetworkAccess = configuration.allowsExpensiveNetworkAccess + #endif + return request } diff --git a/Tests/RelaxTests/Request/Properties/ConfigurationTests.swift b/Tests/RelaxTests/Request/Properties/ConfigurationTests.swift new file mode 100644 index 0000000..d295596 --- /dev/null +++ b/Tests/RelaxTests/Request/Properties/ConfigurationTests.swift @@ -0,0 +1,67 @@ +// +// ConfigurationTests.swift +// +// +// Created by Thomas De Leon on 1/25/24. +// + +import Foundation +import XCTest +@testable import Relax + +final class ConfigurationTests: XCTestCase { + let testURL = URL(string: "https://example.com/")! + private func check(_ request: Request, against expected: Request.Configuration) { + XCTAssertEqual(request.configuration, expected) + + let urlRequest = request.urlRequest + + XCTAssertEqual(urlRequest.allowsCellularAccess, expected.allowsCellularAccess) + XCTAssertEqual(urlRequest.cachePolicy, expected.cachePolicy) + XCTAssertEqual(urlRequest.httpShouldUsePipelining, expected.httpShouldUsePipelining) + XCTAssertEqual(urlRequest.networkServiceType, expected.networkServiceType) + XCTAssertEqual(urlRequest.timeoutInterval, expected.timeoutInterval) + XCTAssertEqual(urlRequest.httpShouldHandleCookies, expected.httpShouldHandleCookies) + + #if !canImport(FoundationNetworking) + XCTAssertEqual(urlRequest.allowsConstrainedNetworkAccess, expected.allowsConstrainedNetworkAccess) + XCTAssertEqual(urlRequest.allowsExpensiveNetworkAccess, expected.allowsExpensiveNetworkAccess) + #endif + } + + func testDefaultConfiguration() throws { + let request = Request(.get, url: testURL) + check(request, against: .default) + } + + func testConfiguration() throws { + #if canImport(FoundationNetworking) + let configuration = Request.Configuration( + allowsCellularAccess: false, + cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, + httpShouldUsePipelining: true, + networkServiceType: .video, + timeoutInterval: 1, + httpShouldHandleCookies: false, + parseHTTPStatusErrors: true, + appendTraillingSlashToPath: true + ) + #else + let configuration = Request.Configuration( + allowsCellularAccess: false, + cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, + httpShouldUsePipelining: true, + networkServiceType: .video, + timeoutInterval: 1, + httpShouldHandleCookies: false, + allowsConstrainedNetworkAccess: false, + allowsExpensiveNetworkAccess: false, + parseHTTPStatusErrors: true, + appendTraillingSlashToPath: true + ) + #endif + + let request = Request(.get, url: testURL, configuration: configuration) + check(request, against: configuration) + } +}