Fix RemoveRequestParameterGatewayFilterFactory to deal with query params which require encoding (#1613)
The RemoveRequestParameterGatewayFilterFactory class builds up a new URI once it removes the request parameter outlined in the filter config, however currently expects the remaining request parameters to all be encoded and the are not. When the request query params are determined, they are decoded in AbstractServerHttpRequest and are not subsequently encoded again before the filter builds up the URI. Currently when the remaining request parameters have a character which requires encoding, you get an "Invalid character ' ' for QUERY_PARAM" error. Co-authored-by: Alan Moffat <alan.moffat@ros.gov.uk>
This commit is contained in:
@@ -61,7 +61,7 @@ public class RemoveRequestParameterGatewayFilterFactory
|
||||
|
||||
URI newUri = UriComponentsBuilder.fromUri(request.getURI())
|
||||
.replaceQueryParams(unmodifiableMultiValueMap(queryParams))
|
||||
.build(true).toUri();
|
||||
.build().toUri();
|
||||
|
||||
ServerHttpRequest updatedRequest = exchange.getRequest().mutate()
|
||||
.uri(newUri).build();
|
||||
|
||||
@@ -47,11 +47,12 @@ public class RemoveRequestParameterGatewayFilterFactoryIntegrationTests
|
||||
|
||||
@Test
|
||||
public void removeResponseHeaderFilterWorks() {
|
||||
testClient.get().uri("/get?foo=bar&baz=bam")
|
||||
testClient.get().uri("/get?foo=bar&baz=bam%20bar")
|
||||
.header("Host", "www.removerequestparamjava.org").exchange()
|
||||
.expectStatus().isOk().expectBody(Map.class).consumeWith(result -> {
|
||||
Map<String, Object> params = getMap(result.getResponseBody(), "args");
|
||||
assertThat(params).doesNotContainKey("foo");
|
||||
assertThat(params).containsEntry("baz", "bam%20bar");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -103,4 +103,27 @@ public class RemoveRequestParameterGatewayFilterFactoryTests {
|
||||
singletonList("xyz"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeRequestParameterFilterShouldHandleRemainingParamsWhichRequiringEncoding() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost")
|
||||
.queryParam("foo", "bar").queryParam("aaa", "abc xyz")
|
||||
.queryParam("bbb", "[xyz").queryParam("ccc", ",xyz").build();
|
||||
exchange = MockServerWebExchange.from(request);
|
||||
NameConfig config = new NameConfig();
|
||||
config.setName("foo");
|
||||
GatewayFilter filter = new RemoveRequestParameterGatewayFilterFactory()
|
||||
.apply(config);
|
||||
|
||||
filter.filter(exchange, filterChain);
|
||||
|
||||
ServerHttpRequest actualRequest = captor.getValue().getRequest();
|
||||
assertThat(actualRequest.getQueryParams()).doesNotContainKey("foo");
|
||||
assertThat(actualRequest.getQueryParams()).containsEntry("aaa",
|
||||
singletonList("abc xyz"));
|
||||
assertThat(actualRequest.getQueryParams()).containsEntry("bbb",
|
||||
singletonList("[xyz"));
|
||||
assertThat(actualRequest.getQueryParams()).containsEntry("ccc",
|
||||
singletonList(",xyz"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user