MockHttpServletRequest.getRequestURL aligns with getServerName/Port

Issue: SPR-16138
This commit is contained in:
Juergen Hoeller
2017-11-05 16:11:21 +01:00
parent db050ac38c
commit 0edf4d6509
3 changed files with 60 additions and 33 deletions

View File

@@ -62,6 +62,7 @@ import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
@@ -404,7 +405,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
/**
* Get the content of the request body as a byte array.
* @return the content as a byte array, potentially {@code null}
* @return the content as a byte array (potentially {@code null})
* @since 5.0
* @see #setContent(byte[])
* @see #getContentAsString()
@@ -588,7 +589,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public String getParameter(String name) {
String[] arr = (name != null ? this.parameters.get(name) : null);
Assert.notNull(name, "Parameter name must not be null");
String[] arr = this.parameters.get(name);
return (arr != null && arr.length > 0 ? arr[0] : null);
}
@@ -599,7 +601,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public String[] getParameterValues(String name) {
return (name != null ? this.parameters.get(name) : null);
Assert.notNull(name, "Parameter name must not be null");
return this.parameters.get(name);
}
@Override
@@ -927,10 +930,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public void setCookies(Cookie... cookies) {
this.cookies = cookies;
this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies);
this.headers.remove(HttpHeaders.COOKIE);
if (cookies != null) {
Arrays.stream(cookies)
if (this.cookies != null) {
Arrays.stream(this.cookies)
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
.forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false));
}
@@ -1164,13 +1167,18 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public StringBuffer getRequestURL() {
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) ||
(HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
url.append(':').append(this.serverPort);
String scheme = getScheme();
String server = getServerName();
int port = getServerPort();
String uri = getRequestURI();
StringBuffer url = new StringBuffer(scheme).append("://").append(server);
if (port > 0 && ((HTTP.equalsIgnoreCase(scheme) && port != 80) ||
(HTTPS.equalsIgnoreCase(scheme) && port != 443))) {
url.append(':').append(port);
}
if (StringUtils.hasText(getRequestURI())) {
url.append(getRequestURI());
if (StringUtils.hasText(uri)) {
url.append(uri);
}
return url;
}
@@ -1280,12 +1288,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
@Override
public Part getPart(String name) throws IOException, IllegalStateException, ServletException {
public Part getPart(String name) throws IOException, ServletException {
return this.parts.getFirst(name);
}
@Override
public Collection<Part> getParts() throws IOException, IllegalStateException, ServletException {
public Collection<Part> getParts() throws IOException, ServletException {
List<Part> result = new LinkedList<>();
for (List<Part> list : this.parts.values()) {
result.addAll(list);