From ee5d6c8f839e2346f03afbf5049c42067bf04526 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 11 Sep 2013 23:08:05 +0200 Subject: [PATCH] Exclude null requestURI in MHSR.getRequestURL() This commit undoes the changes made in ec5d81e78e and ensures that the getRequestURL() method in MockHttpServletRequest does not include the String "null" for a null requestURI by first checking if the requestURI contains text before including it in the composed URL. Issue: SPR-10643 --- .../mock/web/MockHttpServletRequest.java | 14 +++++++++----- .../mock/web/MockHttpServletRequestTests.java | 7 +++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 8659f78806..492e951ca4 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -53,6 +53,7 @@ import javax.servlet.http.Part; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; +import org.springframework.util.StringUtils; /** * Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface. @@ -249,8 +250,8 @@ public class MockHttpServletRequest implements HttpServletRequest { */ public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); - this.method = (method == null ? "" : method); - this.requestURI = (requestURI == null ? "" : requestURI); + this.method = method; + this.requestURI = requestURI; this.locales.add(Locale.ENGLISH); } @@ -859,7 +860,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } public void setMethod(String method) { - this.method = (method == null ? "" : method); + this.method = method; } @Override @@ -937,7 +938,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } public void setRequestURI(String requestURI) { - this.requestURI = (requestURI == null ? "" : requestURI); + this.requestURI = requestURI; } @Override @@ -954,7 +955,10 @@ public class MockHttpServletRequest implements HttpServletRequest { url.append(':').append(this.serverPort); } - url.append(getRequestURI()); + if (StringUtils.hasText(getRequestURI())) { + url.append(getRequestURI()); + } + return url; } diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index 5eb2c42a62..35900fa6be 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java @@ -211,6 +211,13 @@ public class MockHttpServletRequestTests { assertEquals("http://localhost", requestURL.toString()); } + @Test + public void getRequestURLWithNullRequestUri() { + request.setRequestURI(null); + StringBuffer requestURL = request.getRequestURL(); + assertEquals("http://localhost", requestURL.toString()); + } + @Test public void getRequestURLWithDefaultsAndHttps() { request.setScheme("https");