Skip to content

Commit

Permalink
Allow additional path extensions on configured hosts (#253)
Browse files Browse the repository at this point in the history
This adds support for specifying hosts with additional path extensions,
such as `https://connectrpc.com/a/b/c/`. Previously, `/a/b/c` would be
dropped.

Resolves #252.

Also related to #161.
  • Loading branch information
rebello95 authored Mar 28, 2024
1 parent 5d0e21c commit 06b8ad4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension ProtocolClient: ProtocolClientInterface {
var headers = headers
headers[HeaderConstants.contentType] = ["application/\(config.codec.name())"]
let request = HTTPRequest<Input>(
url: URL(string: path, relativeTo: URL(string: config.host))!,
url: config.createURL(forPath: path),
headers: headers,
message: request,
method: .post,
Expand Down Expand Up @@ -330,7 +330,7 @@ extension ProtocolClient: ProtocolClientInterface {
var headers = headers
headers[HeaderConstants.contentType] = ["application/connect+\(codec.name())"]
let request = HTTPRequest<Void>(
url: URL(string: path, relativeTo: URL(string: self.config.host))!,
url: config.createURL(forPath: path),
headers: headers,
message: (),
method: .post,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,8 @@ extension ProtocolClientConfig {
func createStreamInterceptorChain() -> InterceptorChain<any StreamInterceptor> {
return .init(self.interceptors.compactMap { $0.createStream(with: self) })
}

func createURL(forPath path: String) -> URL {
return URL(string: self.host)!.appendingPathComponent(path)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ final class ProtocolClientConfigTests: XCTestCase {
XCTAssertFalse(compression.shouldCompress(data))
}

func testCreatingURLsWithVariousHosts() {
let rpcPath = Connectrpc_Conformance_V1_ConformanceServiceClient.Metadata.Methods.unary.path

XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/a")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/a/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/a/")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/a/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/a/b/c")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/a/b/c/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/a/b/c/")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/a/b/c/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "connectrpc.com/a/b/c")
.createURL(forPath: rpcPath).absoluteString,
"connectrpc.com/a/b/c/connectrpc.conformance.v1.ConformanceService/Unary"
)
}

func testAddsConnectInterceptorLastWhenUsingConnectProtocol() {
let config = ProtocolClientConfig(
host: "https://connectrpc.com",
Expand Down

0 comments on commit 06b8ad4

Please sign in to comment.