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:
themoffster
2020-03-11 20:26:58 +00:00
committed by GitHub
parent 7b1a4ac9e2
commit a54b9b88e5
3 changed files with 26 additions and 2 deletions

View File

@@ -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();

View File

@@ -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");
});
}

View File

@@ -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"));
}
}