-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚨 ✨ Adds ParametersAdaptor. Polishes tests.
- Loading branch information
1 parent
fe559f3
commit 8ed6335
Showing
8 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
Sources/HTTPNetworking/Plugins/Adaptors/ParametersAdaptor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
|
||
/// An ``HTTPRequestAdaptor`` that can be used to append query parameters to a request before it is sent out over the network. | ||
/// | ||
/// > Warning: This adaptor will **not** overwrite existing parameters, instead it will append to existing parameters. | ||
public struct ParametersAdaptor: HTTPRequestAdaptor { | ||
|
||
// MARK: Properties | ||
|
||
let items: [URLQueryItem] | ||
|
||
// MARK: Initializers | ||
|
||
/// Creates a ``ParametersAdaptor`` from the provided query parameters. | ||
/// | ||
/// - Parameter items: The query parameters to append to incoming requests. | ||
public init(items: [URLQueryItem]) { | ||
self.items = items | ||
} | ||
|
||
// MARK: HTTPRequestAdaptor | ||
|
||
public func adapt(_ request: URLRequest, for session: URLSession) async throws -> URLRequest { | ||
var request = request | ||
guard let url = request.url else { | ||
preconditionFailure("URLRequest should contain a valid URL at this point.") | ||
} | ||
|
||
request.url = url.appending(queryItems: items) | ||
return request | ||
} | ||
} | ||
|
||
// MARK: - HTTPRequest + Adaptor | ||
|
||
extension HTTPRequest { | ||
/// Applies a ``ParametersAdaptor`` that appends the provided query parameters to the request. | ||
@discardableResult | ||
public func adapt(queryItems: [URLQueryItem]) -> Self { | ||
adapt(with: ParametersAdaptor(items: queryItems)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Tests/HTTPNetworkingTests/Plugins/Adaptors/ParametersAdaptorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
@testable import HTTPNetworking | ||
import XCTest | ||
|
||
class ParametersAdaptorTests: XCTestCase { | ||
func test_parametersAdaptor_withPrameters_addsParametersToRequest() async throws { | ||
let adaptor = ParametersAdaptor(items: [ | ||
.init(name: "one-name", value: "one-value"), | ||
.init(name: "two-name", value: "two-value"), | ||
]) | ||
|
||
let cleanUrlRequest = URLRequest(url: URL(string: "https://api.com")!) | ||
let adaptedCleanUrlRequest = try await adaptor.adapt(cleanUrlRequest, for: .shared) | ||
XCTAssertEqual( | ||
adaptedCleanUrlRequest.url, | ||
URL(string: "https://api.com?one-name=one-value&two-name=two-value")! | ||
) | ||
|
||
let dirtyUrlRequest = URLRequest(url: URL(string: "https://api.com?")!) | ||
let adaptedDirtyUrlRequest = try await adaptor.adapt(dirtyUrlRequest, for: .shared) | ||
XCTAssertEqual( | ||
adaptedDirtyUrlRequest.url, | ||
URL(string: "https://api.com?one-name=one-value&two-name=two-value")! | ||
) | ||
|
||
let existingParametersUrlRequest = URLRequest(url: URL(string: "https://api.com?two-name=original-two-value&other-name=other-value")!) | ||
let adaptedExistingParametersUrlRequest = try await adaptor.adapt(existingParametersUrlRequest, for: .shared) | ||
XCTAssertEqual( | ||
adaptedExistingParametersUrlRequest.url, | ||
URL(string: "https://api.com?two-name=original-two-value&other-name=other-value&one-name=one-value&two-name=two-value")! | ||
) | ||
} | ||
|
||
func test_request_adaptorConvenience_isAddedToRequestAdaptors() async throws { | ||
let url = URL(string: "https://api.com")! | ||
let client = HTTPClient() | ||
let request = client.request(for: .get, to: url, expecting: String.self) | ||
|
||
let items: [URLQueryItem] = [ | ||
.init(name: "one-name", value: "one-value"), | ||
.init(name: "two-name", value: "two-value"), | ||
] | ||
request.adapt(queryItems: items) | ||
|
||
guard let adaptor = request.adaptors.first as? ParametersAdaptor else { | ||
XCTFail("Expected request to container PrametersAdaptor.") | ||
return | ||
} | ||
|
||
XCTAssertEqual(adaptor.items, items) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters