Skip to content

Commit

Permalink
Stop double-encoding path in stripPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Worsnop committed Jun 24, 2024
1 parent 920b3a8 commit 4c5d4f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public static Function<ServerRequest, ServerRequest> stripPrefix(int parts) {
}
// TODO: end duplicate code from StripPrefixGatewayFilterFactory

URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath.toString()).build()
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath.toString()).build(true)
.toUri();
return ServerRequest.from(request).uri(prefixedUri).build();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ public void stripPrefixWorks() {
});
}

@Test
public void stripPrefixWorksWithEncoding() {
restClient.get().uri("/long/path/to/anything/anything goes").exchange().expectStatus().isOk()
.expectBody(Map.class).consumeWith(res -> {
Map<String, Object> map = res.getResponseBody();
assertThat(map.get("url")).asString().endsWith("anything%20goes");
});
}

@Test
public void stripPrefixPostWorks() {
restClient.post().uri("/long/path/to/post").bodyValue("hello").header("Host", "www.stripprefixpost.org")
Expand Down Expand Up @@ -815,6 +824,17 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefix() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefixWithEncoding() {
// @formatter:off
return route(GET("/long/path/to/anything/anything goes"), http())
.filter(new HttpbinUriResolver())
.filter(stripPrefix(3))
.filter(addRequestHeader("X-Test", "stripPrefix"))
.withAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "teststripprefix");
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefixPost() {
// @formatter:off
Expand Down

0 comments on commit 4c5d4f2

Please sign in to comment.