diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 2b5ac8a93e..0be2d6d533 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -88,9 +88,12 @@ public class ServletServerHttpRequest implements ServerHttpRequest { @Override public URI getURI() { try { - return new URI(this.servletRequest.getScheme(), null, this.servletRequest.getServerName(), - this.servletRequest.getServerPort(), this.servletRequest.getRequestURI(), - this.servletRequest.getQueryString(), null); + StringBuffer url = this.servletRequest.getRequestURL(); + String queryStr = this.servletRequest.getQueryString(); + if (StringUtils.hasText(queryStr)) { + url.append('?').append(queryStr); + } + return new URI(url.toString()); } catch (URISyntaxException ex) { throw new IllegalStateException("Could not get HttpServletRequest URI: " + ex.getMessage(), ex); diff --git a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java index 490b0369b0..68ff322cf2 100644 --- a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java @@ -56,13 +56,16 @@ public class ServletServerHttpRequestTests { @Test public void getURI() throws Exception { - URI uri = new URI("http://example.com/path?query"); - mockRequest.setServerName(uri.getHost()); - mockRequest.setServerPort(uri.getPort()); - mockRequest.setRequestURI(uri.getPath()); - mockRequest.setQueryString(uri.getQuery()); - assertEquals("Invalid uri", uri, request.getURI()); - } + URI uri = new URI("https://example.com/%E4%B8%AD%E6%96%87?redirect=https%3A%2F%2Fgithub.com%2Fspring-projects%2Fspring-framework"); + mockRequest.setScheme(uri.getScheme()); + mockRequest.setServerName(uri.getHost()); + mockRequest.setServerPort(uri.getPort()); + // NOTE: should use getRawPath() instead of getPath() is decoded, while HttpServletRequest.setRequestURI() is encoded + mockRequest.setRequestURI(uri.getRawPath()); + // NOTE: should use getRawQuery() instead of getQuery() is decoded, while HttpServletRequest.getQueryString() is encoded + mockRequest.setQueryString(uri.getRawQuery()); + assertEquals("Invalid uri", uri, request.getURI()); + } @Test public void getHeaders() throws Exception { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java index 6387365ca4..6ad4ed25f3 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java @@ -175,7 +175,8 @@ public class HttpEntityMethodProcessorMockTests { assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); RequestEntity requestEntity = (RequestEntity) result; assertEquals("Invalid method", HttpMethod.GET, requestEntity.getMethod()); - assertEquals("Invalid url", new URI("http", null, "www.example.com", 80, "/path", null, null), requestEntity.getUrl()); + // using default port (which is 80), so do not need to append the port (-1 means ignore) + assertEquals("Invalid url", new URI("http", null, "www.example.com", -1, "/path", null, null), requestEntity.getUrl()); assertEquals("Invalid argument", body, requestEntity.getBody()); } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java index cfe121a261..652f0a6165 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java @@ -20,7 +20,6 @@ import java.sql.Date; import org.junit.Before; import org.junit.Test; - import org.springframework.scheduling.TaskScheduler; import org.springframework.web.socket.AbstractHttpRequestTests; import org.springframework.web.socket.WebSocketHandler; @@ -31,6 +30,7 @@ import org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSes import org.springframework.web.socket.sockjs.transport.session.PollingSockJsSession; import org.springframework.web.socket.sockjs.transport.session.StreamingSockJsSession; import org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig; +import org.springframework.web.util.UriUtils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -114,7 +114,8 @@ public class HttpSendingTransportHandlerTests extends AbstractHttpRequestTests setRequest("POST", "/"); if (callbackValue != null) { - this.servletRequest.setQueryString("c=" + callbackValue); + // need to encode the query parameter + this.servletRequest.setQueryString("c=" + UriUtils.encodeQueryParam(callbackValue, "UTF-8")); this.servletRequest.addParameter("c", callbackValue); }