extends
Object
implements
HttpServletRequest
Since:
1.0.2
Author:
Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher, Chris Beams, Sam Brannen, Brian Clozel
Create a new
MockHttpServletRequest
with the supplied
ServletContext
.
Create a new
MockHttpServletRequest
with the supplied
ServletContext
,
method
, and
requestURI
.
Add a new preferred locale, before any existing locales.
boolean
protected void
Check whether this request is still active (that is, not completed yet),
throwing an IllegalStateException if not active anymore.
Clear all of this request's attributes.
Mark this request as completed, keeping its state.
byte[]
Get the content of the request body as a byte array.
Get the content of the request body as a
String
, using the configured
character encoding
.
Return the long timestamp for the date header with the given
name
.
Return the first preferred
locale
configured
in this mock request.
Return the ServletContext that this request is associated with.
Invalidate this request, clearing its state.
boolean
Return whether this request is still active (that is, not completed yet).
boolean
boolean
boolean
boolean
boolean
boolean
Return
true
if the
secure
flag has been set
to
true
or if the
scheme
is
https
.
boolean
Remove all existing parameters.
Remove already registered entries for the specified HTTP header, if any.
Remove already registered values for the specified HTTP parameter, if any.
Set the content of the request body as a byte array.
Set a single value for the specified HTTP parameter.
Set an array of values for the specified HTTP parameter.
Set all provided parameters
replacing
any existing
values for the provided parameter names.
Set the list of preferred locales, in descending order, effectively replacing
any existing locales.
Set the boolean
secure
flag indicating whether the mock request
was made using a secure channel, such as HTTPS.
Methods 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
getTrailerFields
,
isTrailerFieldsReady
,
newPushBuilder
DEFAULT_PROTOCOL
public static final
String
DEFAULT_PROTOCOL
The default protocol: 'HTTP/1.1'.
Since:
4.3.7
See Also:
Constant Field Values
DEFAULT_SERVER_ADDR
public static final
String
DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.
See Also:
Constant Field Values
DEFAULT_SERVER_NAME
public static final
String
DEFAULT_SERVER_NAME
The default server name: 'localhost'.
See Also:
Constant Field Values
DEFAULT_SERVER_PORT
public static final
int
DEFAULT_SERVER_PORT
The default server port: '80'.
See Also:
Constant Field Values
DEFAULT_REMOTE_ADDR
public static final
String
DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.
See Also:
Constant Field Values
DEFAULT_REMOTE_HOST
public static final
String
DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.
See Also:
Constant Field Values
MockHttpServletRequest
public
MockHttpServletRequest
()
See Also:
MockHttpServletRequest(ServletContext, String, String)
String
requestURI)
Parameters:
method
- the request method (may be
null
)
requestURI
- the request URI (may be
null
)
See Also:
setMethod(java.lang.String)
setRequestURI(java.lang.String)
MockHttpServletRequest(ServletContext, String, String)
MockHttpServletRequest
Create a new
MockHttpServletRequest
with the supplied
ServletContext
.
Parameters:
servletContext
- the ServletContext that the request runs in
(may be
null
to use a default
MockServletContext
)
See Also:
MockHttpServletRequest(ServletContext, String, String)
MockHttpServletRequest
Create a new
MockHttpServletRequest
with the supplied
ServletContext
,
method
, and
requestURI
.
The preferred locale will be set to
Locale.ENGLISH
.
Parameters:
servletContext
- the ServletContext that the request runs in (may be
null
to use a default
MockServletContext
)
method
- the request method (may be
null
)
requestURI
- the request URI (may be
null
)
See Also:
setMethod(java.lang.String)
setRequestURI(java.lang.String)
setPreferredLocales(java.util.List<java.util.Locale>)
MockServletContext
getServletContext
Return the ServletContext that this request is associated with. (Not
available in the standard HttpServletRequest interface for some reason.)
Specified by:
getServletContext
in interface
ServletRequest
Check whether this request is still active (that is, not completed yet),
throwing an IllegalStateException if not active anymore.
Throws:
IllegalStateException
setCharacterEncoding
Specified by:
setCharacterEncoding
in interface
ServletRequest
public
void
setContent
(
@Nullable
byte[] content)
Set the content of the request body as a byte array.
If the supplied byte array represents text such as XML or JSON, the
character encoding
should typically be
set as well.
See Also:
setCharacterEncoding(String)
getContentAsByteArray()
getContentAsString()
@Nullable
public
byte[]
getContentAsByteArray
()
Get the content of the request body as a byte array.
Returns:
the content as a byte array (potentially
null
)
Since:
See Also:
setContent(byte[])
getContentAsString()
public
String
getContentAsString
()
throws
IllegalStateException
,
UnsupportedEncodingException
Get the content of the request body as a
String
, using the configured
character encoding
.
Returns:
the content as a
String
, potentially
null
Throws:
IllegalStateException
- if the character encoding has not been set
UnsupportedEncodingException
- if the character encoding is not supported
Since:
See Also:
setContent(byte[])
setCharacterEncoding(String)
getContentAsByteArray()
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.
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, use
addParameters(java.util.Map)
.
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.
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)
addPreferredLocale
public
void
addPreferredLocale
(
Locale
locale)
Add a new preferred locale, before any existing locales.
See Also:
setPreferredLocales(java.util.List<java.util.Locale>)
setPreferredLocales
public
void
setPreferredLocales
(
List
<
Locale
> locales)
Set the list of preferred locales, in descending order, effectively replacing
any existing locales.
Since:
See Also:
addPreferredLocale(java.util.Locale)
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 is
Locale.ENGLISH
.
In contrast to the Servlet specification, this mock implementation
does
not
take into consideration any locales
specified via the
Accept-Language
header.
Specified by:
getLocale
in interface
ServletRequest
See Also:
ServletRequest.getLocale()
addPreferredLocale(Locale)
setPreferredLocales(List)
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 is
Locale.ENGLISH
.
In contrast to the Servlet specification, this mock implementation
does
not
take into consideration any locales
specified via the
Accept-Language
header.
Specified by:
getLocales
in interface
ServletRequest
See Also:
ServletRequest.getLocales()
addPreferredLocale(Locale)
setPreferredLocales(List)
setSecure
public
void
setSecure
(boolean secure)
Set the boolean
secure
flag indicating whether the mock request
was made using a secure channel, such as HTTPS.
See Also:
isSecure()
getScheme()
setScheme(String)
public
boolean
isSecure
()
Return
true
if the
secure
flag has been set
to
true
or if the
scheme
is
https
.
Specified by:
isSecure
in interface
ServletRequest
See Also:
ServletRequest.isSecure()
Specified by:
startAsync
in interface
ServletRequest