public class MockHttpServletRequest extends Object implements javax.servlet.http.HttpServletRequest
Constructor and Description |
---|
MockHttpServletRequest(Application application,
javax.servlet.http.HttpSession session,
javax.servlet.ServletContext context)
Create the request using the supplied session object.
|
Modifier and Type | Method and Description |
---|---|
void |
addCookie(javax.servlet.http.Cookie cookie)
Add a new cookie.
|
void |
addCookies(Iterable<javax.servlet.http.Cookie> cookies) |
void |
addDateHeader(String name,
long date) |
void |
addFile(String fieldName,
File file,
String contentType)
Add an uploaded file to the request.
|
void |
addHeader(String name,
String value)
Add a header to the request.
|
void |
addParameter(String name,
String value) |
boolean |
authenticate(javax.servlet.http.HttpServletResponse response) |
javax.servlet.AsyncContext |
getAsyncContext() |
Object |
getAttribute(String name)
Get an attribute.
|
Enumeration<String> |
getAttributeNames()
Get the names of all of the values.
|
String |
getAuthType()
Get the auth type.
|
String |
getCharacterEncoding()
Get the current character encoding.
|
Charset |
getCharset()
Get the current character set.
|
int |
getContentLength()
Return the length of the content.
|
String |
getContentType()
If useMultiPartContentType set as true return the correct content-type.
|
String |
getContextPath()
Get the context path.
|
javax.servlet.http.Cookie |
getCookie(String name) |
javax.servlet.http.Cookie[] |
getCookies()
Get all of the cookies for this request.
|
long |
getDateHeader(String name)
Get the given header as a date.
|
javax.servlet.DispatcherType |
getDispatcherType() |
String |
getFilterPrefix() |
String |
getHeader(String name)
Get the given header value.
|
Enumeration<String> |
getHeaderNames()
Get the names of all of the headers.
|
Enumeration<String> |
getHeaders(String name)
Get enumeration of all header values with the given name.
|
javax.servlet.ServletInputStream |
getInputStream()
Returns an input stream if there has been added some uploaded files.
|
int |
getIntHeader(String name)
Get the given header as an int.
|
String |
getLocalAddr() |
Locale |
getLocale()
Get the locale of the request.
|
Enumeration<Locale> |
getLocales()
Return all the accepted locales.
|
String |
getLocalName() |
int |
getLocalPort() |
String |
getMethod()
Get the method.
|
String |
getParameter(String name)
Get the request parameter with the given name.
|
Map<String,String[]> |
getParameterMap()
Get the map of all of the parameters.
|
Enumeration<String> |
getParameterNames()
Get the names of all of the parameters.
|
String[] |
getParameterValues(String name)
Get the values for the given parameter.
|
javax.servlet.http.Part |
getPart(String name) |
Collection<javax.servlet.http.Part> |
getParts() |
String |
getPathInfo()
Get the path info.
|
String |
getPathTranslated()
Always returns null.
|
MockRequestParameters |
getPostParameters() |
String |
getProtocol()
Get the protocol.
|
String |
getQueryString()
Get the query string part of the request.
|
BufferedReader |
getReader()
This feature is not implemented at this time as we are not supporting binary servlet input.
|
String |
getRealPath(String name)
Deprecated.
Use ServletContext.getRealPath(String) instead.
|
String |
getRemoteAddr() |
String |
getRemoteHost()
Get the remote host.
|
int |
getRemotePort() |
String |
getRemoteUser()
Get the name of the remote user from the REMOTE_USER header.
|
javax.servlet.RequestDispatcher |
getRequestDispatcher(String name)
Return a dummy dispatcher that just records that dispatch has occurred without actually doing
anything.
|
String |
getRequestedSessionId()
Get the requested session id.
|
String |
getRequestURI()
Returns context path and servlet path concatenated, typically
/applicationClassName/applicationClassName
|
StringBuffer |
getRequestURL()
Try to build a rough URL.
|
String |
getScheme()
Get the scheme.
|
String |
getServerName()
Get the server name.
|
int |
getServerPort() |
javax.servlet.ServletContext |
getServletContext() |
String |
getServletPath()
The servlet path may either be the application name or /.
|
javax.servlet.http.HttpSession |
getSession()
Get the sessions.
|
javax.servlet.http.HttpSession |
getSession(boolean b)
Get the session.
|
Url |
getUrl() |
Principal |
getUserPrincipal()
Get the user principal.
|
boolean |
hasUploadedFiles() |
void |
initialize()
Reset the request back to a default state.
|
boolean |
isAsyncStarted() |
boolean |
isAsyncSupported() |
boolean |
isRequestedSessionIdFromCookie()
Check whether session id is from a cookie.
|
boolean |
isRequestedSessionIdFromUrl()
Check whether session id is from a url rewrite.
|
boolean |
isRequestedSessionIdFromURL()
Check whether session id is from a url rewrite.
|
boolean |
isRequestedSessionIdValid()
Check whether the session id is valid.
|
boolean |
isSecure() |
boolean |
isUserInRole(String name)
NOT IMPLEMENTED.
|
void |
login(String username,
String password) |
void |
logout() |
void |
removeAttribute(String name)
Remove the given attribute.
|
void |
setAttribute(String name,
Object o)
Set the given attribute.
|
void |
setAuthType(String authType)
Set the auth type.
|
void |
setCharacterEncoding(String encoding)
Set the character encoding.
|
void |
setCookies(javax.servlet.http.Cookie[] theCookies)
Set the cookies.
|
void |
setHeader(String name,
String value)
Sets a header to the request.
|
void |
setLocale(Locale locale) |
void |
setMethod(String method)
Set the method.
|
void |
setParameter(String name,
String value)
Set a parameter.
|
void |
setParameters(Map<String,String[]> parameters)
Sets a map of parameters.
|
MockHttpServletRequest |
setPart(String name,
javax.servlet.http.Part part) |
void |
setPath(String path)
Set the path that this request is supposed to be serving.
|
void |
setRemoteAddr(String addr) |
void |
setScheme(String scheme)
Sets the scheme of this request
set the
secure flag accordingly (true for 'https',
false otherwise) |
void |
setSecure(boolean secure) |
void |
setServerName(String serverName)
Set the server name.
|
void |
setServerPort(int port)
Sets the server port for this request
|
void |
setURL(String url)
Set the complete url for this request.
|
void |
setUrl(Url url) |
void |
setUseMultiPartContentType(boolean useMultiPartContentType)
true will force Request generate multiPart ContentType and ContentLength
|
javax.servlet.AsyncContext |
startAsync() |
javax.servlet.AsyncContext |
startAsync(javax.servlet.ServletRequest servletRequest,
javax.servlet.ServletResponse servletResponse) |
public MockHttpServletRequest(Application application, javax.servlet.http.HttpSession session, javax.servlet.ServletContext context)
MockHttpSession
application
- The application that this request is forsession
- The session objectcontext
- The current servlet contextpublic void addCookie(javax.servlet.http.Cookie cookie)
cookie
- The cookiepublic void addCookies(Iterable<javax.servlet.http.Cookie> cookies)
cookies
- public void addFile(String fieldName, File file, String contentType)
fieldName
- The fieldname of the upload field.file
- The file to upload.contentType
- The content type of the file. Must be a correct mimetype.public void addHeader(String name, String value)
name
- The name of the header to addvalue
- The valuepublic void setHeader(String name, String value)
name
- The name of the header to addvalue
- The valueaddHeader(String, String)
public void addDateHeader(String name, long date)
name
- date
- public Object getAttribute(String name)
getAttribute
in interface javax.servlet.ServletRequest
name
- The attribute namepublic Enumeration<String> getAttributeNames()
getAttributeNames
in interface javax.servlet.ServletRequest
public String getAuthType()
getAuthType
in interface javax.servlet.http.HttpServletRequest
public String getCharacterEncoding()
getCharacterEncoding
in interface javax.servlet.ServletRequest
public Charset getCharset()
public void setUseMultiPartContentType(boolean useMultiPartContentType)
useMultiPartContentType
- public int getContentLength()
getContentLength
in interface javax.servlet.ServletRequest
public String getContentType()
getContentType
in interface javax.servlet.ServletRequest
public String getContextPath()
getContextPath
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.Cookie getCookie(String name)
name
- public javax.servlet.http.Cookie[] getCookies()
getCookies
in interface javax.servlet.http.HttpServletRequest
public long getDateHeader(String name) throws IllegalArgumentException
getDateHeader
in interface javax.servlet.http.HttpServletRequest
name
- The header nameIllegalArgumentException
- If the header cannot be convertedpublic String getHeader(String name)
getHeader
in interface javax.servlet.http.HttpServletRequest
name
- The header namepublic Enumeration<String> getHeaderNames()
getHeaderNames
in interface javax.servlet.http.HttpServletRequest
public Enumeration<String> getHeaders(String name)
getHeaders
in interface javax.servlet.http.HttpServletRequest
name
- The namepublic javax.servlet.ServletInputStream getInputStream() throws IOException
addFile(String, File, String)
to add some uploaded files.getInputStream
in interface javax.servlet.ServletRequest
IOException
- If an I/O related problem occurspublic int getIntHeader(String name)
getIntHeader
in interface javax.servlet.http.HttpServletRequest
name
- The header nameNumberFormatException
- If the header is not formatted correctlypublic Locale getLocale()
getLocale
in interface javax.servlet.ServletRequest
public void setLocale(Locale locale)
public Enumeration<Locale> getLocales()
getLocales
in interface javax.servlet.ServletRequest
public String getMethod()
getMethod
in interface javax.servlet.http.HttpServletRequest
public String getParameter(String name)
getParameter
in interface javax.servlet.ServletRequest
name
- The parameter namepublic Map<String,String[]> getParameterMap()
getParameterMap
in interface javax.servlet.ServletRequest
public Enumeration<String> getParameterNames()
getParameterNames
in interface javax.servlet.ServletRequest
public String[] getParameterValues(String name)
getParameterValues
in interface javax.servlet.ServletRequest
name
- The name of the parameterpublic String getPathInfo()
getPathInfo
in interface javax.servlet.http.HttpServletRequest
public String getPathTranslated()
getPathTranslated
in interface javax.servlet.http.HttpServletRequest
public String getProtocol()
getProtocol
in interface javax.servlet.ServletRequest
public String getQueryString()
getQueryString
in interface javax.servlet.http.HttpServletRequest
public BufferedReader getReader() throws IOException
getReader
in interface javax.servlet.ServletRequest
IOException
- If an I/O related problem occurs@Deprecated public String getRealPath(String name)
getRealPath
in interface javax.servlet.ServletRequest
name
- The namepublic String getRemoteAddr()
getRemoteAddr
in interface javax.servlet.ServletRequest
public void setRemoteAddr(String addr)
addr
- Format: "aaa.bbb.ccc.ddd"public String getRemoteHost()
getRemoteHost
in interface javax.servlet.ServletRequest
public String getRemoteUser()
getRemoteUser
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.RequestDispatcher getRequestDispatcher(String name)
getRequestDispatcher
in interface javax.servlet.ServletRequest
name
- The name to dispatch topublic String getRequestedSessionId()
getRequestedSessionId
in interface javax.servlet.http.HttpServletRequest
public String getRequestURI()
getRequestURI
in interface javax.servlet.http.HttpServletRequest
HttpServletRequest.getRequestURI()
public StringBuffer getRequestURL()
getRequestURL
in interface javax.servlet.http.HttpServletRequest
HttpServletRequest.getRequestURL()
public String getScheme()
getScheme
in interface javax.servlet.ServletRequest
public void setScheme(String scheme)
secure
flag accordingly (true
for 'https',
false
otherwise)scheme
- protocol scheme (e.g. https, http, ftp)isSecure()
public String getServerName()
getServerName
in interface javax.servlet.ServletRequest
public void setServerName(String serverName)
serverName
- content of 'Host' headerpublic int getServerPort()
getServerPort
in interface javax.servlet.ServletRequest
public void setServerPort(int port)
port
- public String getServletPath()
getServletPath
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.HttpSession getSession()
getSession
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.HttpSession getSession(boolean b)
getSession
in interface javax.servlet.http.HttpServletRequest
b
- Ignored, there is always a sessionpublic Principal getUserPrincipal()
getUserPrincipal
in interface javax.servlet.http.HttpServletRequest
public boolean hasUploadedFiles()
addFile(String, File, String)
public void initialize()
public boolean isRequestedSessionIdFromCookie()
isRequestedSessionIdFromCookie
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromUrl()
isRequestedSessionIdFromUrl
in interface javax.servlet.http.HttpServletRequest
public boolean authenticate(javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException
authenticate
in interface javax.servlet.http.HttpServletRequest
IOException
javax.servlet.ServletException
public void login(String username, String password) throws javax.servlet.ServletException
login
in interface javax.servlet.http.HttpServletRequest
javax.servlet.ServletException
public void logout() throws javax.servlet.ServletException
logout
in interface javax.servlet.http.HttpServletRequest
javax.servlet.ServletException
public Collection<javax.servlet.http.Part> getParts() throws IOException, javax.servlet.ServletException
getParts
in interface javax.servlet.http.HttpServletRequest
IOException
javax.servlet.ServletException
public javax.servlet.http.Part getPart(String name) throws IOException, javax.servlet.ServletException
getPart
in interface javax.servlet.http.HttpServletRequest
IOException
javax.servlet.ServletException
public MockHttpServletRequest setPart(String name, javax.servlet.http.Part part)
public boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromURL
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdValid()
isRequestedSessionIdValid
in interface javax.servlet.http.HttpServletRequest
public boolean isSecure()
isSecure
in interface javax.servlet.ServletRequest
true
if this request's scheme is 'https', false
- otherwisepublic void setSecure(boolean secure)
secure
- public boolean isUserInRole(String name)
isUserInRole
in interface javax.servlet.http.HttpServletRequest
name
- The role namepublic void removeAttribute(String name)
removeAttribute
in interface javax.servlet.ServletRequest
name
- The name of the attributepublic void setAttribute(String name, Object o)
setAttribute
in interface javax.servlet.ServletRequest
name
- The attribute nameo
- The value to setpublic void setAuthType(String authType)
authType
- The auth typepublic void setCharacterEncoding(String encoding) throws UnsupportedEncodingException
setCharacterEncoding
in interface javax.servlet.ServletRequest
encoding
- The character encodingUnsupportedEncodingException
- If encoding not supportedpublic void setCookies(javax.servlet.http.Cookie[] theCookies)
theCookies
- The cookiespublic void setMethod(String method)
method
- The methodpublic void setParameter(String name, String value)
name
- The namevalue
- The valuepublic void setParameters(Map<String,String[]> parameters)
parameters
- the parameters to setpublic void setPath(String path)
path
- public void setURL(String url)
url
- public String getLocalAddr()
getLocalAddr
in interface javax.servlet.ServletRequest
public String getLocalName()
getLocalName
in interface javax.servlet.ServletRequest
public int getLocalPort()
getLocalPort
in interface javax.servlet.ServletRequest
public int getRemotePort()
getRemotePort
in interface javax.servlet.ServletRequest
public void setUrl(Url url)
url
- public Url getUrl()
public MockRequestParameters getPostParameters()
public String getFilterPrefix()
public javax.servlet.ServletContext getServletContext()
getServletContext
in interface javax.servlet.ServletRequest
public javax.servlet.AsyncContext startAsync() throws IllegalStateException
startAsync
in interface javax.servlet.ServletRequest
IllegalStateException
public javax.servlet.AsyncContext startAsync(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws IllegalStateException
startAsync
in interface javax.servlet.ServletRequest
IllegalStateException
public boolean isAsyncStarted()
isAsyncStarted
in interface javax.servlet.ServletRequest
public boolean isAsyncSupported()
isAsyncSupported
in interface javax.servlet.ServletRequest
public javax.servlet.AsyncContext getAsyncContext()
getAsyncContext
in interface javax.servlet.ServletRequest
public javax.servlet.DispatcherType getDispatcherType()
getDispatcherType
in interface javax.servlet.ServletRequest
Copyright © 2006–2020 Apache Software Foundation. All rights reserved.