Skip to content

Commit

Permalink
Allow additional path extensions on configured hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
rebello95 committed Mar 28, 2024
1 parent 1eb6a95 commit e89fd85
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/api")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/api/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/api/")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/api/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/api/ext")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/api/ext/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "https://connectrpc.com/api/ext/")
.createURL(forPath: rpcPath).absoluteString,
"https://connectrpc.com/api/ext/connectrpc.conformance.v1.ConformanceService/Unary"
)
XCTAssertEqual(
ProtocolClientConfig(host: "connectrpc.com/api/ext")
.createURL(forPath: rpcPath).absoluteString,
"connectrpc.com/api/ext/connectrpc.conformance.v1.ConformanceService/Unary"
)
}

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

0 comments on commit e89fd85

Please sign in to comment.