Skip to content

Commit

Permalink
Some Request.Configuration properties not being applied (#47)
Browse files Browse the repository at this point in the history
* #46: Set properties on URLRequest from Request.Configuration

* #46: URLRequest.NetworkServiceType.avStreaming does not exist in FoundationNetworking platforms
  • Loading branch information
tdeleon authored Jan 25, 2024
1 parent c8619b1 commit 4b0c498
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/Relax/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,20 @@ 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
request.networkServiceType = configuration.networkServiceType
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
}

Expand Down
67 changes: 67 additions & 0 deletions Tests/RelaxTests/Request/Properties/ConfigurationTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 4b0c498

Please sign in to comment.