From 3477ec0a35c5d19d55431f756cc9c6056ee788da Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 11 Jan 2022 14:54:14 +0000 Subject: [PATCH] Decode servletPath in HtmlUnitRequestBuilder Closes gh-27837 --- .../web/servlet/htmlunit/HtmlUnitRequestBuilder.java | 2 ++ .../servlet/htmlunit/HtmlUnitRequestBuilderTests.java | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java index e25422230e..863a0ddf72 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java @@ -57,6 +57,7 @@ import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; +import org.springframework.web.util.UriUtils; /** * Internal class used to transform a {@link WebRequest} into a @@ -262,6 +263,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { String path = uriComponents.getPath(); String requestPath = (path != null ? path : ""); String servletPath = requestPath.substring(request.getContextPath().length()); + servletPath = UriUtils.decode(servletPath, StandardCharsets.UTF_8); request.setServletPath(servletPath); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java index a1d4d99bee..25e5d23a25 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java @@ -782,6 +782,16 @@ public class HtmlUnitRequestBuilderTests { assertThat(actualRequest.getServletPath()).isEqualTo("/this/here"); } + @Test // gh-27837 + public void buildRequestServletPathWithEncodedUrl() throws Exception { + webRequest.setUrl(new URL("http://localhost/test/Fr%C3%BChling%20Sommer%20Herbst%20Winter")); + + MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); + + assertThat(actualRequest.getRequestURI()).isEqualTo("/test/Fr%C3%BChling%20Sommer%20Herbst%20Winter"); + assertThat(actualRequest.getServletPath()).isEqualTo("/Frühling Sommer Herbst Winter"); + } + @Test public void buildRequestSession() throws Exception { MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);