Skip to content

Commit

Permalink
In this approach, we rebuild the url with path and parse it so that
Browse files Browse the repository at this point in the history
query parameters are extracted.
Advantage of this approach is that tests can pass the full url with
queryparameters (e.g /get?param=value). Disadvantage is that this
changes how we were constructing the url and there is a chance for
breaking old behavior.

GitOrigin-RevId: c5a36bd86576979ac1840f36e6ace3a1c130ed25
  • Loading branch information
lakpic authored and svc-squareup-copybara committed Aug 12, 2024
1 parent 8cffeac commit 88efd9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions misk-testing/src/main/kotlin/misk/web/WebTestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions misk-testing/src/test/kotlin/misk/web/WebTestClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class WebTestClientTest {
install(WebServerTestingModule())
install(MiskTestingServiceModule())
install(WebActionModule.create<GetAction>())
install(WebActionModule.create<GetActionWithQueryParams>())
install(WebActionModule.create<PostAction>())
}
}
Expand All @@ -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)
Expand All @@ -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<Packet>()
).isEqualTo(Packet("get with param value: test"))
}

@Test
fun `performs a POST`() {
assertThat(
Expand Down

0 comments on commit 88efd9e

Please sign in to comment.