From b17cba072b8bd33c55be0898d5cb5787bee75820 Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Tue, 10 May 2022 14:39:37 +0100 Subject: [PATCH 1/2] Add a basic test for the client. --- Tests/URLRoutingTests/ClientTests.swift | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Tests/URLRoutingTests/ClientTests.swift diff --git a/Tests/URLRoutingTests/ClientTests.swift b/Tests/URLRoutingTests/ClientTests.swift new file mode 100644 index 0000000000..cba95428b1 --- /dev/null +++ b/Tests/URLRoutingTests/ClientTests.swift @@ -0,0 +1,46 @@ +import Parsing +import URLRouting +import XCTest + +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +class ClientTests: XCTestCase { + enum TestRoute: Equatable { + case one + case child(ChildRoute) + } + + enum ChildRoute: Equatable { + case one + case two + } + + static let router = OneOf { + Route(.case(TestRoute.one)) { + Path { "one" } + } + Route(.case(TestRoute.child)) { + Path { "child" } + OneOf { + Route(.case(ChildRoute.one)) { + Path { "one" } + } + Route(.case(ChildRoute.two)) { + Path { "two" } + } + } + } + } + + func testBasics() async throws { + let client = URLRoutingClient.failing + .override(.one) { try .ok("result") } + + let (value, response) = try await client.request(.one, as: String.self) + + XCTAssertEqual("result", value) + XCTAssertEqual(200, (response as! HTTPURLResponse).statusCode) + } +} From 477e5080c2fab4ac89c15882067b67f6a4e0b791 Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Tue, 10 May 2022 14:44:15 +0100 Subject: [PATCH 2/2] Add support for client scoping. --- Sources/URLRouting/Client/Client.swift | 14 ++++++++++++++ Tests/URLRoutingTests/ClientTests.swift | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Sources/URLRouting/Client/Client.swift b/Sources/URLRouting/Client/Client.swift index 7311545ddf..97c3b959a6 100644 --- a/Sources/URLRouting/Client/Client.swift +++ b/Sources/URLRouting/Client/Client.swift @@ -41,6 +41,20 @@ public struct URLRoutingClient { throw URLRoutingDecodingError(bytes: data, response: response, underlyingError: error) } } + + /// Allows you to create a client that only handles a subset of routes. + /// + /// - Parameters: + /// - toRoute: A closure that converts `ChildRoute` back to `Route`. + /// + /// - Returns: A client that can only request child routes. + public func scoped( + to toRoute: @escaping (ChildRoute) -> Route + ) -> URLRoutingClient { + .init { child in + try await self.request(toRoute(child)) + } + } } public struct URLRoutingDecodingError: Error { diff --git a/Tests/URLRoutingTests/ClientTests.swift b/Tests/URLRoutingTests/ClientTests.swift index cba95428b1..09d52287c2 100644 --- a/Tests/URLRoutingTests/ClientTests.swift +++ b/Tests/URLRoutingTests/ClientTests.swift @@ -43,4 +43,16 @@ class ClientTests: XCTestCase { XCTAssertEqual("result", value) XCTAssertEqual(200, (response as! HTTPURLResponse).statusCode) } + + func testScoped() async throws { + let client = URLRoutingClient.failing + .override(.child(.one)) { try .ok("result") } + + let scopedClient = client.scoped(to: TestRoute.child) + + let (value, response) = try await scopedClient.request(.one, as: String.self) + + XCTAssertEqual("result", value) + XCTAssertEqual(200, (response as! HTTPURLResponse).statusCode) + } }