MockHttpServletRequest exposes "HTTP/1.1" as default protocol
Issue: SPR-15232
This commit is contained in:
parent
65d8d698cd
commit
ed85337901
|
|
@ -87,10 +87,46 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private static final String HTTPS = "https";
|
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'.
|
* The default server address: '127.0.0.1'.
|
||||||
|
|
@ -117,30 +153,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
*/
|
*/
|
||||||
public static final String DEFAULT_REMOTE_HOST = "localhost";
|
public static final String DEFAULT_REMOTE_HOST = "localhost";
|
||||||
|
|
||||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
|
||||||
|
|
||||||
private static final String HOST_HEADER = "Host";
|
// ---------------------------------------------------------------------
|
||||||
|
// Lifecycle properties
|
||||||
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");
|
|
||||||
|
|
||||||
|
private final ServletContext servletContext;
|
||||||
|
|
||||||
private boolean active = true;
|
private boolean active = true;
|
||||||
|
|
||||||
|
|
@ -161,7 +179,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private String protocol = DEFAULT_PROTOCOL;
|
private String protocol = DEFAULT_PROTOCOL;
|
||||||
|
|
||||||
private String scheme = DEFAULT_PROTOCOL;
|
private String scheme = DEFAULT_SCHEME;
|
||||||
|
|
||||||
private String serverName = DEFAULT_SERVER_NAME;
|
private String serverName = DEFAULT_SERVER_NAME;
|
||||||
|
|
||||||
|
|
@ -176,8 +194,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private boolean secure = false;
|
private boolean secure = false;
|
||||||
|
|
||||||
private final ServletContext servletContext;
|
|
||||||
|
|
||||||
private int remotePort = DEFAULT_SERVER_PORT;
|
private int remotePort = DEFAULT_SERVER_PORT;
|
||||||
|
|
||||||
private String localName = DEFAULT_SERVER_NAME;
|
private String localName = DEFAULT_SERVER_NAME;
|
||||||
|
|
@ -1128,16 +1144,13 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
@Override
|
@Override
|
||||||
public StringBuffer getRequestURL() {
|
public StringBuffer getRequestURL() {
|
||||||
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
|
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
|
||||||
|
if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) ||
|
||||||
if (this.serverPort > 0
|
(HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
|
||||||
&& ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) || (HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
|
|
||||||
url.append(':').append(this.serverPort);
|
url.append(':').append(this.serverPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils.hasText(getRequestURI())) {
|
if (StringUtils.hasText(getRequestURI())) {
|
||||||
url.append(getRequestURI());
|
url.append(getRequestURI());
|
||||||
}
|
}
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
|
|
@ -62,6 +61,16 @@ public class MockHttpServletRequestTests {
|
||||||
public final ExpectedException exception = ExpectedException.none();
|
public final ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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
|
@Test
|
||||||
public void setContentAndGetInputStream() throws IOException {
|
public void setContentAndGetInputStream() throws IOException {
|
||||||
byte[] bytes = "body".getBytes(Charset.defaultCharset());
|
byte[] bytes = "body".getBytes(Charset.defaultCharset());
|
||||||
|
|
|
||||||
|
|
@ -87,10 +87,46 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private static final String HTTPS = "https";
|
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'.
|
* The default server address: '127.0.0.1'.
|
||||||
|
|
@ -117,30 +153,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
*/
|
*/
|
||||||
public static final String DEFAULT_REMOTE_HOST = "localhost";
|
public static final String DEFAULT_REMOTE_HOST = "localhost";
|
||||||
|
|
||||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
|
||||||
|
|
||||||
private static final String HOST_HEADER = "Host";
|
// ---------------------------------------------------------------------
|
||||||
|
// Lifecycle properties
|
||||||
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");
|
|
||||||
|
|
||||||
|
private final ServletContext servletContext;
|
||||||
|
|
||||||
private boolean active = true;
|
private boolean active = true;
|
||||||
|
|
||||||
|
|
@ -161,7 +179,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private String protocol = DEFAULT_PROTOCOL;
|
private String protocol = DEFAULT_PROTOCOL;
|
||||||
|
|
||||||
private String scheme = DEFAULT_PROTOCOL;
|
private String scheme = DEFAULT_SCHEME;
|
||||||
|
|
||||||
private String serverName = DEFAULT_SERVER_NAME;
|
private String serverName = DEFAULT_SERVER_NAME;
|
||||||
|
|
||||||
|
|
@ -176,8 +194,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private boolean secure = false;
|
private boolean secure = false;
|
||||||
|
|
||||||
private final ServletContext servletContext;
|
|
||||||
|
|
||||||
private int remotePort = DEFAULT_SERVER_PORT;
|
private int remotePort = DEFAULT_SERVER_PORT;
|
||||||
|
|
||||||
private String localName = DEFAULT_SERVER_NAME;
|
private String localName = DEFAULT_SERVER_NAME;
|
||||||
|
|
@ -1128,16 +1144,13 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
@Override
|
@Override
|
||||||
public StringBuffer getRequestURL() {
|
public StringBuffer getRequestURL() {
|
||||||
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
|
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
|
||||||
|
if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) ||
|
||||||
if (this.serverPort > 0
|
(HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
|
||||||
&& ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) || (HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
|
|
||||||
url.append(':').append(this.serverPort);
|
url.append(':').append(this.serverPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils.hasText(getRequestURI())) {
|
if (StringUtils.hasText(getRequestURI())) {
|
||||||
url.append(getRequestURI());
|
url.append(getRequestURI());
|
||||||
}
|
}
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue