diff --git a/misk-testing/src/main/kotlin/misk/web/WebTestClient.kt b/misk-testing/src/main/kotlin/misk/web/WebTestClient.kt index a93cb677d94..a4b711d5291 100644 --- a/misk-testing/src/main/kotlin/misk/web/WebTestClient.kt +++ b/misk-testing/src/main/kotlin/misk/web/WebTestClient.kt @@ -9,6 +9,7 @@ import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response import jakarta.inject.Inject +import okhttp3.HttpUrl.Companion.toHttpUrl import kotlin.reflect.KClass /** @@ -55,11 +56,13 @@ class WebTestClient @Inject constructor( * Performs a call to the started service. Allows the caller to customize the action before it's * sent through. */ - fun call(path: String, action: Request.Builder.() -> Unit): WebTestResponse = - Request.Builder() - .url(jettyService.httpServerUrl.newBuilder().encodedPath(path).build()) + fun call(path: String, action: Request.Builder.() -> Unit): WebTestResponse { + val fullUrl = jettyService.httpServerUrl.toUrl().toString() + path.trimStart('/') + return Request.Builder() + .url(fullUrl.toHttpUrl()) .apply(action) .let { performRequest(it) } + } private fun performRequest(request: Request.Builder): WebTestResponse { request.header("Accept", MediaTypes.ALL) diff --git a/misk-testing/src/test/kotlin/misk/web/WebTestClientTest.kt b/misk-testing/src/test/kotlin/misk/web/WebTestClientTest.kt index e23e137f41e..fd8c84b08c9 100644 --- a/misk-testing/src/test/kotlin/misk/web/WebTestClientTest.kt +++ b/misk-testing/src/test/kotlin/misk/web/WebTestClientTest.kt @@ -22,6 +22,7 @@ class WebTestClientTest { install(WebServerTestingModule()) install(MiskTestingServiceModule()) install(WebActionModule.create()) + install(WebActionModule.create()) install(WebActionModule.create()) } } @@ -34,6 +35,12 @@ class WebTestClientTest { fun get() = Packet("get") } + class GetActionWithQueryParams @Inject constructor() : WebAction { + @Get("/get/param") + @ResponseContentType(MediaTypes.APPLICATION_JSON) + fun get(@QueryParam paramKey:String) = Packet("get with param value: $paramKey") + } + class PostAction @Inject constructor() : WebAction { @Post("/post") @RequestContentType(MediaTypes.APPLICATION_JSON) @@ -50,6 +57,15 @@ class WebTestClientTest { ).isEqualTo(Packet("get")) } + @Test + fun `performs a GET with query param`() { + assertThat( + webTestClient + .get("/get/param?paramKey=test") + .parseJson() + ).isEqualTo(Packet("get with param value: test")) + } + @Test fun `performs a POST`() { assertThat(