Reputation: 4306
In my webflux application, i have this GET
endpoint
v3/callback?state=cGF5bWVudGlkPTRiMmZlMG
I am trying to write an integration test using WebTestClient
@Test
public void happyScenario() {
webTestClient.get().uri("/v3/callback?state=cGF5bWVudGlkPTRiMmZlMG")
.exchange()
.expectStatus()
.isOk();
}
This test case return 404 notFound
, if i removed the query parameter it will be called but the state
parameter it will be missing
I tried to use attribute
webTestClient.get().uri("/v3/callback")
.attribute("state","cGF5bWVudGlkPTRiMmZlMG")
.exchange()
.expectStatus()
.isOk();
but still the state
parameter is missing, How can i include a query parameter with request when using webTestClient
?
Upvotes: 21
Views: 17141
Reputation: 178
Below is the Kotlin Sample for the above
val requestParams = LinkedMultiValueMap<String, String>()
requestParams.add("A", "abc")
requestParams.add("B", "def")
requestParams.add("C", "ghi")
val uri = UriComponentsBuilder.fromPath("/v3/callback").queryParams(requestParams).build().toUri()
webTestClient.get().uri{uri}.exchange().expectStatus().isOk
Upvotes: 1
Reputation: 2395
If your query parameter value contains curly braces, for example like this:
webTestClient.get()
.uri(uriBuilder -> uriBuilder
.path("/v3/callback")
.queryParam("query", "{ some { GraphQL { query } } }")
.build())
.exchange()
.expectStatus()
.isOk();
Then UriBuilder
will attempt to do variable substitution on the contents of the curly braces and you will likely get an exception on the build()
call. In order to avoid it, place your request parameters in a separate requestParams
map and use controlled variable substitution by calling build(requestParams)
when building the URI
:
var requestParams = Map.of(
"query", "{ some { GraphQL { query } } }"
);
webTestClient.get()
.uri(uriBuilder -> uriBuilder
.path("/v3/callback")
.queryParam("query", "{query}")
.build(requestParams))
.exchange()
.expectStatus()
.isOk();
Another tip. If you find .uri(uriBuilder -> ...)
syntax a bit complex, you can prepare the URI
explicitly before the request:
var requestParams = Map.of(
"query", "{ some { GraphQL { query } } }"
);
URI uri = new DefaultUriBuilderFactory("/v3/callback")
.builder()
.queryParam("query", "{query}")
.build(requestParams);
webTestClient.get()
.uri(uri)
.exchange()
.expectStatus()
.isOk();
If the URI
is prepared beforehand - it is easier to check the generated URI
in the debugger or log it.
Upvotes: 4
Reputation: 2228
You can make use of UriBuilder
.
webTestClient.get()
.uri(uriBuilder ->
uriBuilder
.path("/v3/callback")
.queryParam("state", "cGF5bWVudGlkPTRiMmZlMG")
.build())
.exchange()
.expectStatus()
.isOk();
This should work.
Upvotes: 39