Class MockHttpServletRequest
java.lang.Object
org.apache.wicket.protocol.http.mock.MockHttpServletRequest
- All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest
,jakarta.servlet.ServletRequest
public class MockHttpServletRequest
extends Object
implements jakarta.servlet.http.HttpServletRequest
Mock servlet request. Implements all of the methods from the standard HttpServletRequest class
plus helper methods to aid setting up a request.
- Author:
- Chris Turner
-
Field Summary
Fields inherited from interface jakarta.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
-
Constructor Summary
ConstructorDescriptionMockHttpServletRequest
(Application application, jakarta.servlet.http.HttpSession session, jakarta.servlet.ServletContext context) MockHttpServletRequest
(Application application, jakarta.servlet.http.HttpSession session, jakarta.servlet.ServletContext context, Locale locale) Create the request using the supplied session object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCookie
(jakarta.servlet.http.Cookie cookie) Add a new cookie.void
addCookies
(Iterable<jakarta.servlet.http.Cookie> cookies) void
addDateHeader
(String name, long date) void
Add an uploaded file to the request.void
Add a header to the request.void
addParameter
(String name, String value) boolean
authenticate
(jakarta.servlet.http.HttpServletResponse response) jakarta.servlet.AsyncContext
getAttribute
(String name) Get an attribute.Get the names of all of the values.Get the auth type.Get the current character encoding.Get the current character set.int
Return the length of the content.long
If useMultiPartContentType set as true return the correct content-type.Get the context path.jakarta.servlet.http.Cookie
jakarta.servlet.http.Cookie[]
Get all of the cookies for this request.long
getDateHeader
(String name) Get the given header as a date.jakarta.servlet.DispatcherType
Get the given header value.Get the names of all of the headers.getHeaders
(String name) Get enumeration of all header values with the given name.jakarta.servlet.ServletInputStream
Returns an input stream if there has been added some uploaded files.int
getIntHeader
(String name) Get the given header as an int.Get the locale of the request.Return all the accepted locales.int
Get the method.getParameter
(String name) Get the request parameter with the given name.Get the map of all of the parameters.Get the names of all of the parameters.String[]
getParameterValues
(String name) Get the values for the given parameter.jakarta.servlet.http.Part
Collection<jakarta.servlet.http.Part>
getParts()
Get the path info.Always returns null.Get the protocol.Get the query string part of the request.This feature is not implemented at this time as we are not supporting binary servlet input.Get the remote host.int
Get the name of the remote user from the REMOTE_USER header.jakarta.servlet.RequestDispatcher
getRequestDispatcher
(String name) Return a dummy dispatcher that just records that dispatch has occurred without actually doing anything.Get the requested session id.Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassNameTry to build a rough URL.Get the scheme.Get the server name.int
jakarta.servlet.ServletConnection
jakarta.servlet.ServletContext
The servlet path may either be the application name or /.jakarta.servlet.http.HttpSession
Get the sessions.jakarta.servlet.http.HttpSession
getSession
(boolean createNew) Get the session.getUrl()
Get the user principal.boolean
void
initialize
(Locale locale) Reset the request back to a default state.boolean
boolean
boolean
Check whether session id is from a cookie.boolean
Check whether session id is from a url rewrite.boolean
Check whether the session id is valid.boolean
isSecure()
boolean
isUserInRole
(String name) NOT IMPLEMENTED.void
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
(jakarta.servlet.http.Cookie[] theCookies) Set the cookies.void
Sets a header to the request.void
void
Set the method.void
setParameter
(String name, String value) Set a parameter.void
setParameters
(Map<String, String[]> parameters) Sets a map of parameters.void
Set the path that this request is supposed to be serving.void
setRemoteAddr
(String addr) void
Sets the scheme of this requestvoid
setSecure
(boolean secure) void
setServerName
(String serverName) Set the server name.void
setServerPort
(int port) Sets the server port for this requestvoid
void
Set the complete url for this request.void
setUseMultiPartContentType
(boolean useMultiPartContentType) true will force Request generate multiPart ContentType and ContentLengthjakarta.servlet.AsyncContext
jakarta.servlet.AsyncContext
startAsync
(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse) <T extends jakarta.servlet.http.HttpUpgradeHandler>
TMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface jakarta.servlet.http.HttpServletRequest
getHttpServletMapping, getTrailerFields, isTrailerFieldsReady, newPushBuilder
-
Constructor Details
-
MockHttpServletRequest
public MockHttpServletRequest(Application application, jakarta.servlet.http.HttpSession session, jakarta.servlet.ServletContext context, Locale locale) Create the request using the supplied session object. Note that in order for temporary sessions to work, the supplied session must be an instance ofMockHttpSession
- Parameters:
application
- The application that this request is forsession
- The session objectcontext
- The current servlet contextlocale
- The current locale
-
MockHttpServletRequest
public MockHttpServletRequest(Application application, jakarta.servlet.http.HttpSession session, jakarta.servlet.ServletContext context)
-
-
Method Details
-
addCookie
Add a new cookie.- Parameters:
cookie
- The cookie
-
addCookies
- Parameters:
cookies
-
-
addFile
Add an uploaded file to the request. Use this to simulate a file that has been uploaded to a field.- Parameters:
fieldName
- The fieldname of the upload field.file
- The file to upload.contentType
- The content type of the file. Must be a correct mimetype.
-
addHeader
Add a header to the request.- Parameters:
name
- The name of the header to addvalue
- The value
-
setHeader
Sets a header to the request. Overrides any previous value of this header.- Parameters:
name
- The name of the header to addvalue
- The value- See Also:
-
addDateHeader
- Parameters:
name
-date
-
-
getAttribute
Get an attribute.- Specified by:
getAttribute
in interfacejakarta.servlet.ServletRequest
- Parameters:
name
- The attribute name- Returns:
- The value, or null
-
getAttributeNames
Get the names of all of the values.- Specified by:
getAttributeNames
in interfacejakarta.servlet.ServletRequest
- Returns:
- The names
-
getAuthType
Get the auth type.- Specified by:
getAuthType
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The auth type
-
getCharacterEncoding
Get the current character encoding.- Specified by:
getCharacterEncoding
in interfacejakarta.servlet.ServletRequest
- Returns:
- The character encoding
-
getCharset
Get the current character set.- Returns:
- The character set
-
setUseMultiPartContentType
true will force Request generate multiPart ContentType and ContentLength- Parameters:
useMultiPartContentType
-
-
getContentLength
Return the length of the content. This is always -1 except if useMultiPartContentType set as true. Then the length will be the length of the generated request.- Specified by:
getContentLength
in interfacejakarta.servlet.ServletRequest
- Returns:
- -1 if useMultiPartContentType is false. Else the length of the generated request.
-
getContentLengthLong
- Specified by:
getContentLengthLong
in interfacejakarta.servlet.ServletRequest
-
getContentType
If useMultiPartContentType set as true return the correct content-type.- Specified by:
getContentType
in interfacejakarta.servlet.ServletRequest
- Returns:
- The correct multipart content-type if useMultiPartContentType is true. Else null.
-
getContextPath
Get the context path. For this mock implementation the name of the application is always returned.- Specified by:
getContextPath
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The context path
-
getCookie
- Parameters:
name
-- Returns:
- Cookie
-
getCookies
Get all of the cookies for this request.- Specified by:
getCookies
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The cookies
-
getDateHeader
Get the given header as a date.- Specified by:
getDateHeader
in interfacejakarta.servlet.http.HttpServletRequest
- Parameters:
name
- The header name- Returns:
- The date, or -1 if header not found
- Throws:
IllegalArgumentException
- If the header cannot be converted
-
getHeader
Get the given header value.- Specified by:
getHeader
in interfacejakarta.servlet.http.HttpServletRequest
- Parameters:
name
- The header name- Returns:
- The header value or null
-
getHeaderNames
Get the names of all of the headers.- Specified by:
getHeaderNames
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The header names
-
getHeaders
Get enumeration of all header values with the given name.- Specified by:
getHeaders
in interfacejakarta.servlet.http.HttpServletRequest
- Parameters:
name
- The name- Returns:
- The header values
-
getInputStream
Returns an input stream if there has been added some uploaded files. UseaddFile(String, File, String)
to add some uploaded files.- Specified by:
getInputStream
in interfacejakarta.servlet.ServletRequest
- Returns:
- The input stream
- Throws:
IOException
- If an I/O related problem occurs
-
getIntHeader
Get the given header as an int.- Specified by:
getIntHeader
in interfacejakarta.servlet.http.HttpServletRequest
- Parameters:
name
- The header name- Returns:
- The header value or -1 if header not found
- Throws:
NumberFormatException
- If the header is not formatted correctly
-
getLocale
Get the locale of the request. Attempts to decode the Accept-Language header and if not found returns the default locale of the JVM.- Specified by:
getLocale
in interfacejakarta.servlet.ServletRequest
- Returns:
- The locale
-
setLocale
-
getLocales
Return all the accepted locales. This implementation always returns just one.- Specified by:
getLocales
in interfacejakarta.servlet.ServletRequest
- Returns:
- The locales
-
getMethod
Get the method.- Specified by:
getMethod
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The method
-
getParameter
Get the request parameter with the given name.- Specified by:
getParameter
in interfacejakarta.servlet.ServletRequest
- Parameters:
name
- The parameter name- Returns:
- The parameter value, or null
-
getParameterMap
Get the map of all of the parameters.- Specified by:
getParameterMap
in interfacejakarta.servlet.ServletRequest
- Returns:
- The parameters
-
getParameterNames
Get the names of all of the parameters.- Specified by:
getParameterNames
in interfacejakarta.servlet.ServletRequest
- Returns:
- The parameter names
-
getParameterValues
Get the values for the given parameter.- Specified by:
getParameterValues
in interfacejakarta.servlet.ServletRequest
- Parameters:
name
- The name of the parameter- Returns:
- The return values
-
getPathInfo
Get the path info.- Specified by:
getPathInfo
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The path info
-
getPathTranslated
Always returns null.- Specified by:
getPathTranslated
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- null
-
getProtocol
Get the protocol.- Specified by:
getProtocol
in interfacejakarta.servlet.ServletRequest
- Returns:
- Always HTTP/1.1
-
getQueryString
Get the query string part of the request.- Specified by:
getQueryString
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The query string
-
getReader
This feature is not implemented at this time as we are not supporting binary servlet input. This functionality may be added in the future.- Specified by:
getReader
in interfacejakarta.servlet.ServletRequest
- Returns:
- The reader
- Throws:
IOException
- If an I/O related problem occurs
-
getRemoteAddr
- Specified by:
getRemoteAddr
in interfacejakarta.servlet.ServletRequest
- Returns:
- the remote address of the client
-
setRemoteAddr
- Parameters:
addr
- Format: "aaa.bbb.ccc.ddd"
-
getRemoteHost
Get the remote host.- Specified by:
getRemoteHost
in interfacejakarta.servlet.ServletRequest
- Returns:
- Return 'localhost' by default
-
getRemoteUser
Get the name of the remote user from the REMOTE_USER header.- Specified by:
getRemoteUser
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The name of the remote user
-
getRequestDispatcher
Return a dummy dispatcher that just records that dispatch has occurred without actually doing anything.- Specified by:
getRequestDispatcher
in interfacejakarta.servlet.ServletRequest
- Parameters:
name
- The name to dispatch to- Returns:
- The dispatcher
-
getRequestedSessionId
Get the requested session id. Always returns the id of the current session.- Specified by:
getRequestedSessionId
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The session id
-
getRequestURI
Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassName- Specified by:
getRequestURI
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The path value
- See Also:
-
HttpServletRequest.getRequestURI()
-
getRequestURL
Try to build a rough URL.- Specified by:
getRequestURL
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The url
- See Also:
-
HttpServletRequest.getRequestURL()
-
getScheme
Get the scheme.- Specified by:
getScheme
in interfacejakarta.servlet.ServletRequest
- Returns:
- the scheme of this request
-
setScheme
Sets the scheme of this request set thesecure
flag accordingly (true
for 'https',false
otherwise)- Parameters:
scheme
- protocol scheme (e.g. https, http, ftp)- See Also:
-
getServerName
Get the server name.- Specified by:
getServerName
in interfacejakarta.servlet.ServletRequest
- Returns:
- by default returns 'localhost'
-
setServerName
Set the server name.- Parameters:
serverName
- content of 'Host' header
-
getServerPort
- Specified by:
getServerPort
in interfacejakarta.servlet.ServletRequest
- Returns:
- the server port of this request
-
setServerPort
Sets the server port for this request- Parameters:
port
-
-
getServletPath
The servlet path may either be the application name or /. For test purposes we always return the servlet name.- Specified by:
getServletPath
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The servlet path
-
getSession
Get the sessions.- Specified by:
getSession
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- The session
-
changeSessionId
- Specified by:
changeSessionId
in interfacejakarta.servlet.http.HttpServletRequest
-
getSession
Get the session.- Specified by:
getSession
in interfacejakarta.servlet.http.HttpServletRequest
- Parameters:
createNew
- Ignored, there is always a session- Returns:
- The session
-
getUserPrincipal
Get the user principal.- Specified by:
getUserPrincipal
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- A user principal
-
hasUploadedFiles
- Returns:
- True if there has been added files to this request using
addFile(String, File, String)
-
initialize
Reset the request back to a default state.- Parameters:
locale
-
-
isRequestedSessionIdFromCookie
Check whether session id is from a cookie. Always returns true.- Specified by:
isRequestedSessionIdFromCookie
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- Always true
-
authenticate
public boolean authenticate(jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException - Specified by:
authenticate
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-
login
- Specified by:
login
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
jakarta.servlet.ServletException
-
logout
- Specified by:
logout
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
jakarta.servlet.ServletException
-
getParts
public Collection<jakarta.servlet.http.Part> getParts() throws IOException, jakarta.servlet.ServletException- Specified by:
getParts
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-
getPart
public jakarta.servlet.http.Part getPart(String name) throws IOException, jakarta.servlet.ServletException - Specified by:
getPart
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-
upgrade
public <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> aClass) throws IOException, jakarta.servlet.ServletException - Specified by:
upgrade
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-
setPart
-
isRequestedSessionIdFromURL
Check whether session id is from a url rewrite. Always returns false.- Specified by:
isRequestedSessionIdFromURL
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- Always false
-
isRequestedSessionIdValid
Check whether the session id is valid.- Specified by:
isRequestedSessionIdValid
in interfacejakarta.servlet.http.HttpServletRequest
- Returns:
- Always true
-
isSecure
- Specified by:
isSecure
in interfacejakarta.servlet.ServletRequest
- Returns:
true
if this request's scheme is 'https',false
- otherwise
-
setSecure
- Parameters:
secure
-
-
isUserInRole
NOT IMPLEMENTED.- Specified by:
isUserInRole
in interfacejakarta.servlet.http.HttpServletRequest
- Parameters:
name
- The role name- Returns:
- Always false
-
removeAttribute
Remove the given attribute.- Specified by:
removeAttribute
in interfacejakarta.servlet.ServletRequest
- Parameters:
name
- The name of the attribute
-
setAttribute
Set the given attribute.- Specified by:
setAttribute
in interfacejakarta.servlet.ServletRequest
- Parameters:
name
- The attribute nameo
- The value to set
-
setAuthType
Set the auth type.- Parameters:
authType
- The auth type
-
setCharacterEncoding
Set the character encoding.- Specified by:
setCharacterEncoding
in interfacejakarta.servlet.ServletRequest
- Parameters:
encoding
- The character encoding- Throws:
UnsupportedEncodingException
- If encoding not supported
-
setCookies
Set the cookies.- Parameters:
theCookies
- The cookies
-
setMethod
Set the method.- Parameters:
method
- The method
-
setParameter
Set a parameter.- Parameters:
name
- The namevalue
- The value
-
addParameter
- Parameters:
name
-value
-
-
setParameters
Sets a map of parameters.- Parameters:
parameters
- the parameters to set
-
setPath
Set the path that this request is supposed to be serving. The path is relative to the web application root and should start with a / character- Parameters:
path
-
-
setURL
Set the complete url for this request. The url will be analyzed.- Parameters:
url
-
-
getLocalAddr
- Specified by:
getLocalAddr
in interfacejakarta.servlet.ServletRequest
- Returns:
- local address
-
getLocalName
- Specified by:
getLocalName
in interfacejakarta.servlet.ServletRequest
- Returns:
- local host name
-
getLocalPort
- Specified by:
getLocalPort
in interfacejakarta.servlet.ServletRequest
- Returns:
- local port
-
getRemotePort
- Specified by:
getRemotePort
in interfacejakarta.servlet.ServletRequest
- Returns:
- remote port
-
setUrl
- Parameters:
url
-
-
getUrl
- Returns:
- request url
-
getPostParameters
- Returns:
- post parameters
-
getFilterPrefix
- Returns:
- filter prefix
-
getServletContext
- Specified by:
getServletContext
in interfacejakarta.servlet.ServletRequest
- Returns:
- ServletContext
-
startAsync
- Specified by:
startAsync
in interfacejakarta.servlet.ServletRequest
- Throws:
IllegalStateException
-
startAsync
public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse) throws IllegalStateException - Specified by:
startAsync
in interfacejakarta.servlet.ServletRequest
- Throws:
IllegalStateException
-
isAsyncStarted
- Specified by:
isAsyncStarted
in interfacejakarta.servlet.ServletRequest
-
isAsyncSupported
- Specified by:
isAsyncSupported
in interfacejakarta.servlet.ServletRequest
-
getAsyncContext
- Specified by:
getAsyncContext
in interfacejakarta.servlet.ServletRequest
-
getDispatcherType
- Specified by:
getDispatcherType
in interfacejakarta.servlet.ServletRequest
-
getRequestId
- Specified by:
getRequestId
in interfacejakarta.servlet.ServletRequest
-
getProtocolRequestId
- Specified by:
getProtocolRequestId
in interfacejakarta.servlet.ServletRequest
-
getServletConnection
- Specified by:
getServletConnection
in interfacejakarta.servlet.ServletRequest
-