Browse Source

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
pull/353/head
Sam Brannen 11 years ago
parent
commit
ee5d6c8f83
  1. 14
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
  2. 7
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

14
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

@ -53,6 +53,7 @@ import javax.servlet.http.Part; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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;
}

7
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

@ -211,6 +211,13 @@ public class MockHttpServletRequestTests { @@ -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");

Loading…
Cancel
Save