类 MockHttpServletRequest
- java.lang.Object
- org.springframework.mock.web.MockHttpServletRequest
- 所有已实现的接口:
HttpServletRequest
,ServletRequest
public class MockHttpServletRequest extends Object implements HttpServletRequest
Mock implementation of theHttpServletRequest
interface.The default, preferred
Locale
for the server mocked by this request isLocale.ENGLISH
. This value can be changed viaaddPreferredLocale(java.util.Locale)
orsetPreferredLocales(java.util.List<java.util.Locale>)
.As of Spring Framework 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
- 从以下版本开始:
- 1.0.2
- 作者:
- Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher, Chris Beams, Sam Brannen, Brian Clozel
字段概要
字段 修饰符和类型 字段 说明 static String
DEFAULT_PROTOCOL
The default protocol: 'HTTP/1.1'.static String
DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.static String
DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.static String
DEFAULT_SCHEME
The default scheme: 'http'.static String
DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.static String
DEFAULT_SERVER_NAME
The default server name: 'localhost'.static int
DEFAULT_SERVER_PORT
The default server port: '80'.从接口继承的字段 javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
构造器概要
构造器 构造器 说明 MockHttpServletRequest()
Create a newMockHttpServletRequest
with a defaultMockServletContext
.MockHttpServletRequest(String method, String requestURI)
Create a newMockHttpServletRequest
with a defaultMockServletContext
.MockHttpServletRequest(ServletContext servletContext)
Create a newMockHttpServletRequest
with the suppliedServletContext
.MockHttpServletRequest(ServletContext servletContext, String method, String requestURI)
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 void
addHeader(String name, Object value)
Add an HTTP header entry for the given name.void
addParameter(String name, String value)
Add a single value for the specified HTTP parameter.void
addParameter(String name, String... values)
Add an array of values for the specified HTTP parameter.void
addParameters(Map<String,?> params)
Add all provided parameters without replacing any existing values.void
addPart(Part part)
void
addPreferredLocale(Locale locale)
Add a new preferred locale, before any existing locales.void
addUserRole(String role)
boolean
authenticate(HttpServletResponse response)
String
changeSessionId()
The implementation of this (Servlet 3.1+) method callsMockHttpSession.changeSessionId()
if the session is a mock session.protected void
checkActive()
Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.void
clearAttributes()
Clear all of this request's attributes.void
close()
Mark this request as completed, keeping its state.AsyncContext
getAsyncContext()
Object
getAttribute(String name)
Enumeration<String>
getAttributeNames()
String
getAuthType()
String
getCharacterEncoding()
int
getContentLength()
long
getContentLengthLong()
String
getContentType()
String
getContextPath()
Cookie[]
getCookies()
long
getDateHeader(String name)
Return the long timestamp for the date header with the givenname
.DispatcherType
getDispatcherType()
String
getHeader(String name)
Enumeration<String>
getHeaderNames()
Enumeration<String>
getHeaders(String name)
ServletInputStream
getInputStream()
int
getIntHeader(String name)
String
getLocalAddr()
Locale
getLocale()
Return the first preferred locale configured in this mock request.Enumeration<Locale>
getLocales()
Return an enumeration of the preferred locales configured in this mock request.String
getLocalName()
int
getLocalPort()
String
getMethod()
String
getParameter(String name)
Map<String,String[]>
getParameterMap()
Enumeration<String>
getParameterNames()
String[]
getParameterValues(String name)
Part
getPart(String name)
Collection<Part>
getParts()
String
getPathInfo()
String
getPathTranslated()
String
getProtocol()
String
getQueryString()
BufferedReader
getReader()
String
getRealPath(String path)
已过时。String
getRemoteAddr()
String
getRemoteHost()
int
getRemotePort()
String
getRemoteUser()
RequestDispatcher
getRequestDispatcher(String path)
String
getRequestedSessionId()
String
getRequestURI()
StringBuffer
getRequestURL()
String
getScheme()
String
getServerName()
int
getServerPort()
ServletContext
getServletContext()
Return the ServletContext that this request is associated with.String
getServletPath()
HttpSession
getSession()
HttpSession
getSession(boolean create)
Principal
getUserPrincipal()
void
invalidate()
Invalidate this request, clearing its state.boolean
isActive()
Return whether this request is still active (that is, not completed yet).boolean
isAsyncStarted()
boolean
isAsyncSupported()
boolean
isRequestedSessionIdFromCookie()
boolean
isRequestedSessionIdFromUrl()
已过时。boolean
isRequestedSessionIdFromURL()
boolean
isRequestedSessionIdValid()
boolean
isSecure()
boolean
isUserInRole(String role)
void
login(String username, String password)
void
logout()
void
removeAllParameters()
Remove all existing parameters.void
removeAttribute(String name)
void
removeHeader(String name)
Remove already registered entries for the specified HTTP header, if any.void
removeParameter(String name)
Remove already registered values for the specified HTTP parameter, if any.void
setAsyncContext(MockAsyncContext asyncContext)
void
setAsyncStarted(boolean asyncStarted)
void
setAsyncSupported(boolean asyncSupported)
void
setAttribute(String name, Object value)
void
setAuthType(String authType)
void
setCharacterEncoding(String characterEncoding)
void
setContent(byte[] content)
void
setContentType(String contentType)
void
setContextPath(String contextPath)
void
setCookies(Cookie... cookies)
void
setDispatcherType(DispatcherType dispatcherType)
void
setLocalAddr(String localAddr)
void
setLocalName(String localName)
void
setLocalPort(int localPort)
void
setMethod(String method)
void
setParameter(String name, String value)
Set a single value for the specified HTTP parameter.void
setParameter(String name, String... values)
Set an array of values for the specified HTTP parameter.void
setParameters(Map<String,?> params)
Set all provided parameters replacing any existing values for the provided parameter names.void
setPathInfo(String pathInfo)
void
setPreferredLocales(List<Locale> locales)
Set the list of preferred locales, in descending order, effectively replacing any existing locales.void
setProtocol(String protocol)
void
setQueryString(String queryString)
void
setRemoteAddr(String remoteAddr)
void
setRemoteHost(String remoteHost)
void
setRemotePort(int remotePort)
void
setRemoteUser(String remoteUser)
void
setRequestedSessionId(String requestedSessionId)
void
setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
void
setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
void
setRequestedSessionIdValid(boolean requestedSessionIdValid)
void
setRequestURI(String requestURI)
void
setScheme(String scheme)
void
setSecure(boolean secure)
Set the booleansecure
flag indicating whether the mock request was made using a secure channel, such as HTTPS.void
setServerName(String serverName)
void
setServerPort(int serverPort)
void
setServletPath(String servletPath)
void
setSession(HttpSession session)
void
setUserPrincipal(Principal userPrincipal)
AsyncContext
startAsync()
AsyncContext
startAsync(ServletRequest request, ServletResponse response)
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 javax.servlet.http.HttpServletRequest
upgrade
字段详细资料
DEFAULT_PROTOCOL
public static final String DEFAULT_PROTOCOL
The default protocol: 'HTTP/1.1'.- 从以下版本开始:
- 4.3.7
- 另请参阅:
- 常量字段值
DEFAULT_SCHEME
public static final String DEFAULT_SCHEME
The default scheme: 'http'.- 从以下版本开始:
- 4.3.7
- 另请参阅:
- 常量字段值
DEFAULT_SERVER_ADDR
public static final String DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.- 另请参阅:
- 常量字段值
DEFAULT_SERVER_NAME
public static final String DEFAULT_SERVER_NAME
The default server name: 'localhost'.- 另请参阅:
- 常量字段值
DEFAULT_SERVER_PORT
public static final int DEFAULT_SERVER_PORT
The default server port: '80'.- 另请参阅:
- 常量字段值
DEFAULT_REMOTE_ADDR
public static final String DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.- 另请参阅:
- 常量字段值
DEFAULT_REMOTE_HOST
public static final String DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.- 另请参阅:
- 常量字段值
构造器详细资料
MockHttpServletRequest
public MockHttpServletRequest()
Create a newMockHttpServletRequest
with a defaultMockServletContext
.
MockHttpServletRequest
public MockHttpServletRequest(String method, String requestURI)
Create a newMockHttpServletRequest
with a defaultMockServletContext
.- 参数:
method
- the request method (may benull
)requestURI
- the request URI (may benull
)- 另请参阅:
setMethod(java.lang.String)
,setRequestURI(java.lang.String)
,MockHttpServletRequest(ServletContext, String, String)
MockHttpServletRequest
public MockHttpServletRequest(ServletContext servletContext)
Create a newMockHttpServletRequest
with the suppliedServletContext
.- 参数:
servletContext
- the ServletContext that the request runs in (may benull
to use a defaultMockServletContext
)- 另请参阅:
MockHttpServletRequest(ServletContext, String, String)
MockHttpServletRequest
public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI)
Create a newMockHttpServletRequest
with the suppliedServletContext
,method
, andrequestURI
.The preferred locale will be set to
Locale.ENGLISH
.- 参数:
servletContext
- the ServletContext that the request runs in (may benull
to use a defaultMockServletContext
)method
- the request method (may benull
)requestURI
- the request URI (may benull
)- 另请参阅:
setMethod(java.lang.String)
,setRequestURI(java.lang.String)
,setPreferredLocales(java.util.List<java.util.Locale>)
,MockServletContext
方法详细资料
getServletContext
public ServletContext getServletContext()
Return the ServletContext that this request is associated with. (Not available in the standard HttpServletRequest interface for some reason.)- 指定者:
getServletContext
在接口中ServletRequest
isActive
public boolean isActive()
Return whether this request is still active (that is, not completed yet).
close
public void close()
Mark this request as completed, keeping its state.
invalidate
public void invalidate()
Invalidate this request, clearing its state.
checkActive
protected void checkActive() throws IllegalStateException
Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
getAttribute
public Object getAttribute(String name)
- 指定者:
getAttribute
在接口中ServletRequest
getAttributeNames
public Enumeration<String> getAttributeNames()
- 指定者:
getAttributeNames
在接口中ServletRequest
getCharacterEncoding
public String getCharacterEncoding()
- 指定者:
getCharacterEncoding
在接口中ServletRequest
setCharacterEncoding
public void setCharacterEncoding(String characterEncoding)
- 指定者:
setCharacterEncoding
在接口中ServletRequest
setContent
public void setContent(byte[] content)
getContentLength
public int getContentLength()
- 指定者:
getContentLength
在接口中ServletRequest
getContentLengthLong
public long getContentLengthLong()
- 指定者:
getContentLengthLong
在接口中ServletRequest
setContentType
public void setContentType(String contentType)
getContentType
public String getContentType()
- 指定者:
getContentType
在接口中ServletRequest
getInputStream
public ServletInputStream getInputStream()
- 指定者:
getInputStream
在接口中ServletRequest
setParameter
public void setParameter(String name, String value)
Set a single value for the specified HTTP parameter.If there are already one or more values registered for the given parameter name, they will be replaced.
setParameter
public void setParameter(String name, String... values)
Set an array of values for the specified HTTP parameter.If there are already one or more values registered for the given parameter name, they will be replaced.
setParameters
public void setParameters(Map<String,?> params)
Set all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, useaddParameters(java.util.Map)
.
addParameter
public void addParameter(String name, String value)
Add a single value for the specified HTTP parameter.If there are already one or more values registered for the given parameter name, the given value will be added to the end of the list.
addParameter
public void addParameter(String name, String... values)
Add an array of values for the specified HTTP parameter.If there are already one or more values registered for the given parameter name, the given values will be added to the end of the list.
addParameters
public void addParameters(Map<String,?> params)
Add all provided parameters without replacing any existing values. To replace existing values, usesetParameters(java.util.Map)
.
removeParameter
public void removeParameter(String name)
Remove already registered values for the specified HTTP parameter, if any.
removeAllParameters
public void removeAllParameters()
Remove all existing parameters.
getParameter
public String getParameter(String name)
- 指定者:
getParameter
在接口中ServletRequest
getParameterNames
public Enumeration<String> getParameterNames()
- 指定者:
getParameterNames
在接口中ServletRequest
getParameterValues
public String[] getParameterValues(String name)
- 指定者:
getParameterValues
在接口中ServletRequest
getParameterMap
public Map<String,String[]> getParameterMap()
- 指定者:
getParameterMap
在接口中ServletRequest
setProtocol
public void setProtocol(String protocol)
getProtocol
public String getProtocol()
- 指定者:
getProtocol
在接口中ServletRequest
getScheme
public String getScheme()
- 指定者:
getScheme
在接口中ServletRequest
setServerName
public void setServerName(String serverName)
getServerName
public String getServerName()
- 指定者:
getServerName
在接口中ServletRequest
setServerPort
public void setServerPort(int serverPort)
getServerPort
public int getServerPort()
- 指定者:
getServerPort
在接口中ServletRequest
getReader
public BufferedReader getReader() throws UnsupportedEncodingException
- 指定者:
getReader
在接口中ServletRequest
- 抛出:
UnsupportedEncodingException
setRemoteAddr
public void setRemoteAddr(String remoteAddr)
getRemoteAddr
public String getRemoteAddr()
- 指定者:
getRemoteAddr
在接口中ServletRequest
setRemoteHost
public void setRemoteHost(String remoteHost)
getRemoteHost
public String getRemoteHost()
- 指定者:
getRemoteHost
在接口中ServletRequest
setAttribute
public void setAttribute(String name, Object value)
- 指定者:
setAttribute
在接口中ServletRequest
removeAttribute
public void removeAttribute(String name)
- 指定者:
removeAttribute
在接口中ServletRequest
clearAttributes
public void clearAttributes()
Clear all of this request's attributes.
addPreferredLocale
public void addPreferredLocale(Locale locale)
Add a new preferred locale, before any existing locales.
setPreferredLocales
public void setPreferredLocales(List<Locale> locales)
Set the list of preferred locales, in descending order, effectively replacing any existing locales.- 从以下版本开始:
- 3.2
- 另请参阅:
addPreferredLocale(java.util.Locale)
getLocale
public Locale getLocale()
Return the first preferred locale configured in this mock request.If no locales have been explicitly configured, the default, preferred
Locale
for the server mocked by this request isLocale.ENGLISH
.In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the
Accept-Language
header.
getLocales
public Enumeration<Locale> getLocales()
Return an enumeration of the preferred locales configured in this mock request.If no locales have been explicitly configured, the default, preferred
Locale
for the server mocked by this request isLocale.ENGLISH
.In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the
Accept-Language
header.
setSecure
public void setSecure(boolean secure)
Set the booleansecure
flag indicating whether the mock request was made using a secure channel, such as HTTPS.
isSecure
public boolean isSecure()
- 指定者:
isSecure
在接口中ServletRequest
- 另请参阅:
ServletRequest.isSecure()
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(String path)
- 指定者:
getRequestDispatcher
在接口中ServletRequest
getRealPath
@Deprecated public String getRealPath(String path)
已过时。- 指定者:
getRealPath
在接口中ServletRequest
setRemotePort
public void setRemotePort(int remotePort)
getRemotePort
public int getRemotePort()
- 指定者:
getRemotePort
在接口中ServletRequest
setLocalName
public void setLocalName(String localName)
getLocalName
public String getLocalName()
- 指定者:
getLocalName
在接口中ServletRequest
setLocalAddr
public void setLocalAddr(String localAddr)
getLocalAddr
public String getLocalAddr()
- 指定者:
getLocalAddr
在接口中ServletRequest
setLocalPort
public void setLocalPort(int localPort)
getLocalPort
public int getLocalPort()
- 指定者:
getLocalPort
在接口中ServletRequest
startAsync
public AsyncContext startAsync()
- 指定者:
startAsync
在接口中ServletRequest
startAsync
public AsyncContext startAsync(ServletRequest request, ServletResponse response)
- 指定者:
startAsync
在接口中ServletRequest
setAsyncStarted
public void setAsyncStarted(boolean asyncStarted)
isAsyncStarted
public boolean isAsyncStarted()
- 指定者:
isAsyncStarted
在接口中ServletRequest
setAsyncSupported
public void setAsyncSupported(boolean asyncSupported)
isAsyncSupported
public boolean isAsyncSupported()
- 指定者:
isAsyncSupported
在接口中ServletRequest
setAsyncContext
public void setAsyncContext(MockAsyncContext asyncContext)
getAsyncContext
public AsyncContext getAsyncContext()
- 指定者:
getAsyncContext
在接口中ServletRequest
setDispatcherType
public void setDispatcherType(DispatcherType dispatcherType)
getDispatcherType
public DispatcherType getDispatcherType()
- 指定者:
getDispatcherType
在接口中ServletRequest
setAuthType
public void setAuthType(String authType)
getAuthType
public String getAuthType()
- 指定者:
getAuthType
在接口中HttpServletRequest
setCookies
public void setCookies(Cookie... cookies)
getCookies
public Cookie[] getCookies()
- 指定者:
getCookies
在接口中HttpServletRequest
addHeader
public void addHeader(String name, Object value)
Add an HTTP header entry for the given name.While this method can take any
Object
as a parameter, it is recommended to use the following types:- String or any Object to be converted using
toString()
; seegetHeader(java.lang.String)
. - String, Number, or Date for date headers; see
getDateHeader(java.lang.String)
. - String or Number for integer headers; see
getIntHeader(java.lang.String)
. String[]
orCollection<String>
for multiple values; seegetHeaders(java.lang.String)
.
- String or any Object to be converted using
removeHeader
public void removeHeader(String name)
Remove already registered entries for the specified HTTP header, if any.- 从以下版本开始:
- 4.3.20
getDateHeader
public long getDateHeader(String name)
Return the long timestamp for the date header with the givenname
.If the internal value representation is a String, this method will try to parse it as a date using the supported date formats:
- "EEE, dd MMM yyyy HH:mm:ss zzz"
- "EEE, dd-MMM-yy HH:mm:ss zzz"
- "EEE MMM dd HH:mm:ss yyyy"
- 指定者:
getDateHeader
在接口中HttpServletRequest
- 参数:
name
- the header name- 另请参阅:
- Section 7.1.1.1 of RFC 7231
getHeader
public String getHeader(String name)
- 指定者:
getHeader
在接口中HttpServletRequest
getHeaders
public Enumeration<String> getHeaders(String name)
- 指定者:
getHeaders
在接口中HttpServletRequest
getHeaderNames
public Enumeration<String> getHeaderNames()
- 指定者:
getHeaderNames
在接口中HttpServletRequest
getIntHeader
public int getIntHeader(String name)
- 指定者:
getIntHeader
在接口中HttpServletRequest
getMethod
public String getMethod()
- 指定者:
getMethod
在接口中HttpServletRequest
setPathInfo
public void setPathInfo(String pathInfo)
getPathInfo
public String getPathInfo()
- 指定者:
getPathInfo
在接口中HttpServletRequest
getPathTranslated
public String getPathTranslated()
- 指定者:
getPathTranslated
在接口中HttpServletRequest
setContextPath
public void setContextPath(String contextPath)
getContextPath
public String getContextPath()
- 指定者:
getContextPath
在接口中HttpServletRequest
setQueryString
public void setQueryString(String queryString)
getQueryString
public String getQueryString()
- 指定者:
getQueryString
在接口中HttpServletRequest
setRemoteUser
public void setRemoteUser(String remoteUser)
getRemoteUser
public String getRemoteUser()
- 指定者:
getRemoteUser
在接口中HttpServletRequest
addUserRole
public void addUserRole(String role)
isUserInRole
public boolean isUserInRole(String role)
- 指定者:
isUserInRole
在接口中HttpServletRequest
setUserPrincipal
public void setUserPrincipal(Principal userPrincipal)
getUserPrincipal
public Principal getUserPrincipal()
- 指定者:
getUserPrincipal
在接口中HttpServletRequest
setRequestedSessionId
public void setRequestedSessionId(String requestedSessionId)
getRequestedSessionId
public String getRequestedSessionId()
- 指定者:
getRequestedSessionId
在接口中HttpServletRequest
setRequestURI
public void setRequestURI(String requestURI)
getRequestURI
public String getRequestURI()
- 指定者:
getRequestURI
在接口中HttpServletRequest
getRequestURL
public StringBuffer getRequestURL()
- 指定者:
getRequestURL
在接口中HttpServletRequest
setServletPath
public void setServletPath(String servletPath)
getServletPath
public String getServletPath()
- 指定者:
getServletPath
在接口中HttpServletRequest
setSession
public void setSession(HttpSession session)
getSession
public HttpSession getSession(boolean create)
- 指定者:
getSession
在接口中HttpServletRequest
getSession
public HttpSession getSession()
- 指定者:
getSession
在接口中HttpServletRequest
changeSessionId
public String changeSessionId()
The implementation of this (Servlet 3.1+) method callsMockHttpSession.changeSessionId()
if the session is a mock session. Otherwise it simply returns the current session id.- 指定者:
changeSessionId
在接口中HttpServletRequest
- 从以下版本开始:
- 4.0.3
setRequestedSessionIdValid
public void setRequestedSessionIdValid(boolean requestedSessionIdValid)
isRequestedSessionIdValid
public boolean isRequestedSessionIdValid()
setRequestedSessionIdFromCookie
public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
isRequestedSessionIdFromCookie
public boolean isRequestedSessionIdFromCookie()
setRequestedSessionIdFromURL
public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
isRequestedSessionIdFromURL
public boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromUrl
@Deprecated public boolean isRequestedSessionIdFromUrl()
已过时。
authenticate
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
login
public void login(String username, String password) throws ServletException
- 指定者:
login
在接口中HttpServletRequest
- 抛出:
ServletException
logout
public void logout() throws ServletException
- 指定者:
logout
在接口中HttpServletRequest
- 抛出:
ServletException
getPart
public Part getPart(String name) throws IOException, ServletException
- 指定者:
getPart
在接口中HttpServletRequest
- 抛出:
IOException
ServletException
getParts
public Collection<Part> getParts() throws IOException, ServletException