Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix URL resolution for base URI with non-empty path #173

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.connectrpc.protocols.GETConfiguration
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.suspendCancellableCoroutine
import java.net.URL
import java.util.concurrent.CountDownLatch
import kotlin.coroutines.resume

Expand Down Expand Up @@ -268,8 +267,5 @@ class ProtocolClient(
)
}

private fun <Input : Any, Output : Any> urlFromMethodSpec(methodSpec: MethodSpec<Input, Output>): URL {
val host = config.baseUri.resolve("/${methodSpec.path}")
return host.toURL()
}
private fun urlFromMethodSpec(methodSpec: MethodSpec<*, *>) = config.baseUri.resolve(methodSpec.path).toURL()
pkwarren marked this conversation as resolved.
Show resolved Hide resolved
}
155 changes: 74 additions & 81 deletions library/src/test/kotlin/com/connectrpc/impl/ProtocolClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,15 @@ class ProtocolClientTest {
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)

val client = ProtocolClient(
httpClient = httpClient,
config = ProtocolClientConfig(
host = "https://connectrpc.com/",
serializationStrategy = serializationStrategy,
),
)
val client = createClient("https://connectrpc.com/")
client.unary(
"input",
emptyMap(),
MethodSpec(
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
createMethodSpec(StreamType.UNARY),
) { _ -> }
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).unary(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/com.connectrpc.SomeService/Service")
}

@Test
Expand All @@ -69,23 +61,15 @@ class ProtocolClientTest {
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)

val client = ProtocolClient(
httpClient = httpClient,
config = ProtocolClientConfig(
host = "https://connectrpc.com",
serializationStrategy = serializationStrategy,
),
)
val client = createClient("https://connectrpc.com")
client.unary(
"input",
emptyMap(),
MethodSpec(
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
createMethodSpec(StreamType.UNARY),
) { _ -> }
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).unary(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/com.connectrpc.SomeService/Service")
}

@Test
Expand All @@ -94,23 +78,15 @@ class ProtocolClientTest {
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)

val client = ProtocolClient(
httpClient = httpClient,
config = ProtocolClientConfig(
host = "https://connectrpc.com/",
serializationStrategy = serializationStrategy,
),
)
val client = createClient("https://connectrpc.com/")
CoroutineScope(Dispatchers.IO).launch {
client.stream(
emptyMap(),
MethodSpec(
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.BIDI,
),
createMethodSpec(StreamType.BIDI),
)
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).stream(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/com.connectrpc.SomeService/Service")
}
}

Expand All @@ -120,23 +96,15 @@ class ProtocolClientTest {
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)

val client = ProtocolClient(
httpClient = httpClient,
config = ProtocolClientConfig(
host = "https://connectrpc.com",
serializationStrategy = serializationStrategy,
),
)
val client = createClient("https://connectrpc.com")
CoroutineScope(Dispatchers.IO).launch {
client.stream(
emptyMap(),
MethodSpec(
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.BIDI,
),
createMethodSpec(StreamType.BIDI),
)
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).stream(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/com.connectrpc.SomeService/Service")
}
}

Expand All @@ -145,25 +113,12 @@ class ProtocolClientTest {
whenever(codec.encodingName()).thenReturn("testing")
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)
val client = ProtocolClient(
httpClient = httpClient,
config = ProtocolClientConfig(
host = "https://connectrpc.com",
serializationStrategy = serializationStrategy,
),
)
val client = createClient("https://connectrpc.com")
client.unary(
"",
emptyMap(),
MethodSpec(
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
createMethodSpec(StreamType.UNARY),
) {}

// Use HTTP client to determine and verify the final URL.
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).unary(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/com.connectrpc.SomeService/Service")
Expand All @@ -174,27 +129,65 @@ class ProtocolClientTest {
whenever(codec.encodingName()).thenReturn("testing")
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)
val client = ProtocolClient(
httpClient = httpClient,
config = ProtocolClientConfig(
host = "https://connectrpc.com/",
serializationStrategy = serializationStrategy,
),
)
val client = createClient("https://connectrpc.com/")
client.unary(
"",
emptyMap(),
MethodSpec(
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
createMethodSpec(StreamType.UNARY),
) {}
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).unary(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/com.connectrpc.SomeService/Service")
}

// Use HTTP client to determine and verify the final URL.
@Test
fun finalUrlRelativeBaseURI() {
whenever(codec.encodingName()).thenReturn("testing")
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)
val client = createClient("https://connectrpc.com/api")
client.unary(
"",
emptyMap(),
createMethodSpec(StreamType.UNARY),
) {}
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).unary(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/com.connectrpc.SomeService/Service")
}

@Test
fun finalUrlAbsoluteBaseURI() {
whenever(codec.encodingName()).thenReturn("testing")
whenever(codec.serialize(any())).thenReturn(Buffer())
whenever(serializationStrategy.codec<String>(any())).thenReturn(codec)
val client = createClient("https://connectrpc.com/api/")
client.unary(
"",
emptyMap(),
createMethodSpec(StreamType.UNARY),
) {}
val captor = argumentCaptor<HTTPRequest>()
verify(httpClient).unary(captor.capture(), any())
assertThat(captor.firstValue.url.toString()).isEqualTo("https://connectrpc.com/api/com.connectrpc.SomeService/Service")
}

private fun createClient(host: String): ProtocolClient {
return ProtocolClient(
httpClient = httpClient,
config = ProtocolClientConfig(
host = host,
serializationStrategy = serializationStrategy,
),
)
}

private fun createMethodSpec(streamType: StreamType): MethodSpec<String, String> {
return MethodSpec(
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType,
)
}
}