MockHttpServletRequest exposes "HTTP/1.1" as default protocol
Issue: SPR-15232
(cherry picked from commit ed85337)
This commit is contained in:
@@ -86,10 +86,46 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private static final String HTTPS = "https";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String HOST_HEADER = "Host";
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
|
||||
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
|
||||
new DelegatingServletInputStream(StreamUtils.emptyInput());
|
||||
|
||||
private static final BufferedReader EMPTY_BUFFERED_READER =
|
||||
new BufferedReader(new StringReader(""));
|
||||
|
||||
/**
|
||||
* The default protocol: 'http'.
|
||||
* Date formats as specified in the HTTP RFC
|
||||
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
|
||||
*/
|
||||
public static final String DEFAULT_PROTOCOL = HTTP;
|
||||
private static final String[] DATE_FORMATS = new String[] {
|
||||
"EEE, dd MMM yyyy HH:mm:ss zzz",
|
||||
"EEE, dd-MMM-yy HH:mm:ss zzz",
|
||||
"EEE MMM dd HH:mm:ss yyyy"
|
||||
};
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Public constants
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The default protocol: 'HTTP/1.1'.
|
||||
* @since 4.3.7
|
||||
*/
|
||||
public static final String DEFAULT_PROTOCOL = "HTTP/1.1";
|
||||
|
||||
/**
|
||||
* The default scheme: 'http'.
|
||||
* @since 4.3.7
|
||||
*/
|
||||
public static final String DEFAULT_SCHEME = HTTP;
|
||||
|
||||
/**
|
||||
* The default server address: '127.0.0.1'.
|
||||
@@ -116,30 +152,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
*/
|
||||
public static final String DEFAULT_REMOTE_HOST = "localhost";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String HOST_HEADER = "Host";
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
|
||||
new DelegatingServletInputStream(StreamUtils.emptyInput());
|
||||
|
||||
private static final BufferedReader EMPTY_BUFFERED_READER =
|
||||
new BufferedReader(new StringReader(""));
|
||||
|
||||
/**
|
||||
* Date formats as specified in the HTTP RFC
|
||||
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
|
||||
*/
|
||||
private static final String[] DATE_FORMATS = new String[] {
|
||||
"EEE, dd MMM yyyy HH:mm:ss zzz",
|
||||
"EEE, dd-MMM-yy HH:mm:ss zzz",
|
||||
"EEE MMM dd HH:mm:ss yyyy"
|
||||
};
|
||||
|
||||
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
// ---------------------------------------------------------------------
|
||||
// Lifecycle properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
private final ServletContext servletContext;
|
||||
|
||||
private boolean active = true;
|
||||
|
||||
@@ -160,7 +178,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private String protocol = DEFAULT_PROTOCOL;
|
||||
|
||||
private String scheme = DEFAULT_PROTOCOL;
|
||||
private String scheme = DEFAULT_SCHEME;
|
||||
|
||||
private String serverName = DEFAULT_SERVER_NAME;
|
||||
|
||||
@@ -175,8 +193,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private boolean secure = false;
|
||||
|
||||
private final ServletContext servletContext;
|
||||
|
||||
private int remotePort = DEFAULT_SERVER_PORT;
|
||||
|
||||
private String localName = DEFAULT_SERVER_NAME;
|
||||
@@ -1088,16 +1104,13 @@ 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))) {
|
||||
if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) ||
|
||||
(HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
|
||||
url.append(':').append(this.serverPort);
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(getRequestURI())) {
|
||||
url.append(getRequestURI());
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,7 +27,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -54,11 +53,21 @@ public class MockHttpServletRequestTests {
|
||||
|
||||
private static final String IF_MODIFIED_SINCE = "If-Modified-Since";
|
||||
|
||||
private MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
private final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
|
||||
@Test
|
||||
public void content() throws IOException {
|
||||
public void protocolAndScheme() {
|
||||
assertEquals(MockHttpServletRequest.DEFAULT_PROTOCOL, request.getProtocol());
|
||||
assertEquals(MockHttpServletRequest.DEFAULT_SCHEME, request.getScheme());
|
||||
request.setProtocol("HTTP/2.0");
|
||||
request.setScheme("https");
|
||||
assertEquals("HTTP/2.0", request.getProtocol());
|
||||
assertEquals("https", request.getScheme());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setContentAndGetInputStream() throws IOException {
|
||||
byte[] bytes = "body".getBytes(Charset.defaultCharset());
|
||||
request.setContent(bytes);
|
||||
assertEquals(bytes.length, request.getContentLength());
|
||||
@@ -109,9 +118,7 @@ public class MockHttpServletRequestTests {
|
||||
assertEquals("UTF-8", request.getCharacterEncoding());
|
||||
}
|
||||
|
||||
// SPR-12677
|
||||
|
||||
@Test
|
||||
@Test // SPR-12677
|
||||
public void setContentTypeHeaderWithMoreComplexCharsetSyntax() {
|
||||
String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";
|
||||
request.addHeader("Content-Type", contentType);
|
||||
@@ -157,7 +164,7 @@ public class MockHttpServletRequestTests {
|
||||
public void setMultipleParameters() {
|
||||
request.setParameter("key1", "value1");
|
||||
request.setParameter("key2", "value2");
|
||||
Map<String, Object> params = new HashMap<String, Object>(2);
|
||||
Map<String, Object> params = new HashMap<>(2);
|
||||
params.put("key1", "newValue1");
|
||||
params.put("key3", new String[] { "value3A", "value3B" });
|
||||
request.setParameters(params);
|
||||
@@ -175,7 +182,7 @@ public class MockHttpServletRequestTests {
|
||||
public void addMultipleParameters() {
|
||||
request.setParameter("key1", "value1");
|
||||
request.setParameter("key2", "value2");
|
||||
Map<String, Object> params = new HashMap<String, Object>(2);
|
||||
Map<String, Object> params = new HashMap<>(2);
|
||||
params.put("key1", "newValue1");
|
||||
params.put("key3", new String[] { "value3A", "value3B" });
|
||||
request.addParameters(params);
|
||||
@@ -193,7 +200,7 @@ public class MockHttpServletRequestTests {
|
||||
@Test
|
||||
public void removeAllParameters() {
|
||||
request.setParameter("key1", "value1");
|
||||
Map<String, Object> params = new HashMap<String, Object>(2);
|
||||
Map<String, Object> params = new HashMap<>(2);
|
||||
params.put("key2", "value2");
|
||||
params.put("key3", new String[] { "value3A", "value3B" });
|
||||
request.addParameters(params);
|
||||
@@ -245,7 +252,7 @@ public class MockHttpServletRequestTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setPreferredLocalesWithEmptyList() {
|
||||
request.setPreferredLocales(new ArrayList<Locale>());
|
||||
request.setPreferredLocales(new ArrayList<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -86,10 +86,46 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private static final String HTTPS = "https";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String HOST_HEADER = "Host";
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
|
||||
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
|
||||
new DelegatingServletInputStream(StreamUtils.emptyInput());
|
||||
|
||||
private static final BufferedReader EMPTY_BUFFERED_READER =
|
||||
new BufferedReader(new StringReader(""));
|
||||
|
||||
/**
|
||||
* The default protocol: 'http'.
|
||||
* Date formats as specified in the HTTP RFC
|
||||
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
|
||||
*/
|
||||
public static final String DEFAULT_PROTOCOL = HTTP;
|
||||
private static final String[] DATE_FORMATS = new String[] {
|
||||
"EEE, dd MMM yyyy HH:mm:ss zzz",
|
||||
"EEE, dd-MMM-yy HH:mm:ss zzz",
|
||||
"EEE MMM dd HH:mm:ss yyyy"
|
||||
};
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Public constants
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The default protocol: 'HTTP/1.1'.
|
||||
* @since 4.3.7
|
||||
*/
|
||||
public static final String DEFAULT_PROTOCOL = "HTTP/1.1";
|
||||
|
||||
/**
|
||||
* The default scheme: 'http'.
|
||||
* @since 4.3.7
|
||||
*/
|
||||
public static final String DEFAULT_SCHEME = HTTP;
|
||||
|
||||
/**
|
||||
* The default server address: '127.0.0.1'.
|
||||
@@ -116,30 +152,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
*/
|
||||
public static final String DEFAULT_REMOTE_HOST = "localhost";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String HOST_HEADER = "Host";
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
|
||||
new DelegatingServletInputStream(StreamUtils.emptyInput());
|
||||
|
||||
private static final BufferedReader EMPTY_BUFFERED_READER =
|
||||
new BufferedReader(new StringReader(""));
|
||||
|
||||
/**
|
||||
* Date formats as specified in the HTTP RFC
|
||||
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
|
||||
*/
|
||||
private static final String[] DATE_FORMATS = new String[] {
|
||||
"EEE, dd MMM yyyy HH:mm:ss zzz",
|
||||
"EEE, dd-MMM-yy HH:mm:ss zzz",
|
||||
"EEE MMM dd HH:mm:ss yyyy"
|
||||
};
|
||||
|
||||
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
// ---------------------------------------------------------------------
|
||||
// Lifecycle properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
private final ServletContext servletContext;
|
||||
|
||||
private boolean active = true;
|
||||
|
||||
@@ -160,7 +178,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private String protocol = DEFAULT_PROTOCOL;
|
||||
|
||||
private String scheme = DEFAULT_PROTOCOL;
|
||||
private String scheme = DEFAULT_SCHEME;
|
||||
|
||||
private String serverName = DEFAULT_SERVER_NAME;
|
||||
|
||||
@@ -175,8 +193,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private boolean secure = false;
|
||||
|
||||
private final ServletContext servletContext;
|
||||
|
||||
private int remotePort = DEFAULT_SERVER_PORT;
|
||||
|
||||
private String localName = DEFAULT_SERVER_NAME;
|
||||
@@ -1088,16 +1104,13 @@ 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))) {
|
||||
if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) ||
|
||||
(HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
|
||||
url.append(':').append(this.serverPort);
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(getRequestURI())) {
|
||||
url.append(getRequestURI());
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user