类 MockHttpServletRequestBuilder
- 所有已实现的接口:
cn.taketoday.beans.Mergeable,ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>,RequestBuilder,SmartRequestBuilder
MockHttpServletRequest required as input to
perform requests in MockMvc.
Application tests will typically access this builder through the static
factory methods in MockMvcRequestBuilders.
This class is not open for extension. To apply custom initialization to
the created MockHttpServletRequest, please use the
with(RequestPostProcessor) extension point.
- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Arjen Poutsma, Sam Brannen, Kamill Sokol
-
字段概要
字段修饰符和类型字段说明private Stringprivate byte[]private Stringprivate Stringprivate final List<jakarta.servlet.http.Cookie>private final Stringprivate Stringprivate final List<RequestPostProcessor>private Principalprivate Stringprivate Booleanprivate Stringprivate MockHttpSessionprivate final URI -
构造器概要
构造器构造器说明MockHttpServletRequestBuilder(cn.taketoday.http.HttpMethod httpMethod, String url, Object... vars) Package private constructor.MockHttpServletRequestBuilder(cn.taketoday.http.HttpMethod httpMethod, URI url) Alternative toMockHttpServletRequestBuilder(HttpMethod, String, Object...)with a pre-built URI.MockHttpServletRequestBuilder(String httpMethod, URI url) Alternative constructor for custom HTTP methods. -
方法概要
修饰符和类型方法说明accept(cn.taketoday.http.MediaType... mediaTypes) Set the 'Accept' header to the given media type(s).Set theAcceptheader using raw String values, possibly not even well-formed (for testing purposes).private voidaddRequestParams(MockHttpServletRequest request, cn.taketoday.util.MultiValueMap<String, String> map) private static voidprivate static <T> voidaddToMultiValueMap(cn.taketoday.util.MultiValueMap<String, T> map, String name, T[] values) final MockHttpServletRequestbuildRequest(jakarta.servlet.ServletContext servletContext) Build aMockHttpServletRequest.characterEncoding(String encoding) Set the character encoding of the request.characterEncoding(Charset encoding) Set the character encoding of the request.private booleancontainsCookie(jakarta.servlet.http.Cookie cookie) content(byte[] content) Set the request body.Set the request body as a UTF-8 String.contentType(cn.taketoday.http.MediaType contentType) Set the 'Content-Type' header of the request.contentType(String contentType) Set the 'Content-Type' header of the request as a raw String value, possibly not even well-formed (for testing purposes).contextPath(String contextPath) Specify the portion of the requestURI that represents the context path.cookie(jakarta.servlet.http.Cookie... cookies) Add the given cookies to the request.protected MockHttpServletRequestcreateServletRequest(jakarta.servlet.ServletContext servletContext) Create a newMockHttpServletRequestbased on the suppliedServletContext.Set an "input" flash attribute.flashAttrs(Map<String, Object> flashAttributes) Set flash attributes.Add a header to the request.headers(cn.taketoday.http.HttpHeaders httpHeaders) Add all headers to the request.private static URIbooleanSet the locale of the request, overriding any previous locales.Add the specified locales as preferred request locales.Merges the properties of the "parent" RequestBuilder accepting values only if not already set in "this" instance.Add a request parameter toMockHttpServletRequest.getParameterMap().Variant ofparam(String, String...)with aMultiValueMap.parseFormData(cn.taketoday.http.MediaType mediaType) Specify the portion of the requestURI that represents the pathInfo.postProcessRequest(MockHttpServletRequest request) Apply request post-processing.Set the principal of the request.queryParam(String name, String... values) Append to the query string and also add to therequest parametersmap.queryParams(cn.taketoday.util.MultiValueMap<String, String> params) Append to the query string and also add to therequest parametersmap.remoteAddress(String remoteAddress) Set the remote address of the request.requestAttr(String name, Object value) Set a request attribute.secure(boolean secure) Set the secure property of theServletRequestindicating use of a secure channel, such as HTTPS.servletPath(String servletPath) Specify the portion of the requestURI that represents the path to which the Servlet is mapped.session(MockHttpSession session) Set the HTTP session to use, possibly re-used across requests.sessionAttr(String name, Object value) Set a session attribute.sessionAttrs(Map<String, Object> sessionAttributes) Set session attributes.private voidupdatePathRequestProperties(MockHttpServletRequest request, String requestUri) Update the contextPath, servletPath, and pathInfo of the request.with(RequestPostProcessor postProcessor) An extension point for further initialization ofMockHttpServletRequestin ways not built directly into theMockHttpServletRequestBuilder.
-
字段详细资料
-
method
-
url
-
contextPath
-
servletPath
-
pathInfo
-
secure
-
principal
-
remoteAddress
-
session
-
characterEncoding
-
@Nullable private byte[] content
-
contentType
-
headers
-
parameters
-
queryParams
-
cookies
-
locales
-
requestAttributes
-
sessionAttributes
-
flashAttributes
-
postProcessors
-
-
构造器详细资料
-
MockHttpServletRequestBuilder
Package private constructor. To get an instance, use static factory methods inMockMvcRequestBuilders.Although this class cannot be extended, additional ways to initialize the
MockHttpServletRequestcan be plugged in viawith(RequestPostProcessor).- 参数:
httpMethod- the HTTP method (GET, POST, etc)url- a URL template; the resulting URL will be encodedvars- zero or more URI variables
-
MockHttpServletRequestBuilder
MockHttpServletRequestBuilder(cn.taketoday.http.HttpMethod httpMethod, URI url) Alternative toMockHttpServletRequestBuilder(HttpMethod, String, Object...)with a pre-built URI.- 参数:
httpMethod- the HTTP method (GET, POST, etc)url- the URL
-
MockHttpServletRequestBuilder
Alternative constructor for custom HTTP methods.- 参数:
httpMethod- the HTTP method (GET, POST, etc)url- the URL
-
-
方法详细资料
-
initUri
-
contextPath
Specify the portion of the requestURI that represents the context path. The context path, if specified, must match to the start of the request URI.In most cases, tests can be written by omitting the context path from the requestURI. This is because most applications don't actually depend on the name under which they're deployed. If specified here, the context path must start with a "/" and must not end with a "/".
- 另请参阅:
-
HttpServletRequest.getContextPath()
-
servletPath
Specify the portion of the requestURI that represents the path to which the Servlet is mapped. This is typically a portion of the requestURI after the context path.In most cases, tests can be written by omitting the servlet path from the requestURI. This is because most applications don't actually depend on the prefix to which a servlet is mapped. For example if a Servlet is mapped to
"/main/*", tests can be written with the requestURI"/accounts/1"as opposed to"/main/accounts/1". If specified here, the servletPath must start with a "/" and must not end with a "/".- 另请参阅:
-
HttpServletRequest.getServletPath()
-
pathInfo
Specify the portion of the requestURI that represents the pathInfo.If left unspecified (recommended), the pathInfo will be automatically derived by removing the contextPath and the servletPath from the requestURI and using any remaining part. If specified here, the pathInfo must start with a "/".
If specified, the pathInfo will be used as-is.
- 另请参阅:
-
HttpServletRequest.getPathInfo()
-
secure
Set the secure property of theServletRequestindicating use of a secure channel, such as HTTPS.- 参数:
secure- whether the request is using a secure channel
-
characterEncoding
Set the character encoding of the request.- 参数:
encoding- the character encoding- 另请参阅:
-
characterEncoding
Set the character encoding of the request.- 参数:
encoding- the character encoding
-
content
Set the request body.If content is provided and
contentType(MediaType)is set toapplication/x-www-form-urlencoded, the content will be parsed and used to populate therequest parametersmap.- 参数:
content- the body content
-
content
Set the request body as a UTF-8 String.If content is provided and
contentType(MediaType)is set toapplication/x-www-form-urlencoded, the content will be parsed and used to populate therequest parametersmap.- 参数:
content- the body content
-
contentType
Set the 'Content-Type' header of the request.If content is provided and
contentTypeis set toapplication/x-www-form-urlencoded, the content will be parsed and used to populate therequest parametersmap.- 参数:
contentType- the content type
-
contentType
Set the 'Content-Type' header of the request as a raw String value, possibly not even well-formed (for testing purposes).- 参数:
contentType- the content type
-
accept
Set the 'Accept' header to the given media type(s).- 参数:
mediaTypes- one or more media types
-
accept
Set theAcceptheader using raw String values, possibly not even well-formed (for testing purposes).- 参数:
mediaTypes- one or more media types; internally joined as comma-separated String
-
header
Add a header to the request. Values are always added.- 参数:
name- the header namevalues- one or more header values
-
headers
Add all headers to the request. Values are always added.- 参数:
httpHeaders- the headers and values to add
-
param
Add a request parameter toMockHttpServletRequest.getParameterMap().In the Servlet API, a request parameter may be parsed from the query string and/or from the body of an
application/x-www-form-urlencodedrequest. This method simply adds to the request parameter map. You may also use add Servlet request parameters by specifying the query or form data through one of the following:- Supply a URL with a query to
MockMvcRequestBuilders. - Add query params via
queryParam(java.lang.String, java.lang.String...)orqueryParams. - Provide
contentwithcontentTypeapplication/x-www-form-urlencoded.
- 参数:
name- the parameter namevalues- one or more values
- Supply a URL with a query to
-
params
Variant ofparam(String, String...)with aMultiValueMap.- 参数:
params- the parameters to add- 从以下版本开始:
- 4.0.4
-
queryParam
Append to the query string and also add to therequest parametersmap. The parameter name and value are encoded when they are added to the query string.- 参数:
name- the parameter namevalues- one or more values- 从以下版本开始:
- 4.0
-
queryParams
public MockHttpServletRequestBuilder queryParams(cn.taketoday.util.MultiValueMap<String, String> params) Append to the query string and also add to therequest parametersmap. The parameter name and value are encoded when they are added to the query string.- 参数:
params- the parameters to add- 从以下版本开始:
- 4.0
-
cookie
Add the given cookies to the request. Cookies are always added.- 参数:
cookies- the cookies to add
-
locale
Add the specified locales as preferred request locales.- 参数:
locales- the locales to add- 另请参阅:
-
locale
Set the locale of the request, overriding any previous locales.- 参数:
locale- the locale, ornullto reset it- 另请参阅:
-
requestAttr
Set a request attribute.- 参数:
name- the attribute namevalue- the attribute value
-
sessionAttr
Set a session attribute.- 参数:
name- the session attribute namevalue- the session attribute value
-
sessionAttrs
Set session attributes.- 参数:
sessionAttributes- the session attributes
-
flashAttr
Set an "input" flash attribute.- 参数:
name- the flash attribute namevalue- the flash attribute value
-
flashAttrs
Set flash attributes.- 参数:
flashAttributes- the flash attributes
-
session
Set the HTTP session to use, possibly re-used across requests.Individual attributes provided via
sessionAttr(String, Object)override the content of the session provided here.- 参数:
session- the HTTP session
-
principal
Set the principal of the request.- 参数:
principal- the principal
-
remoteAddress
Set the remote address of the request.- 参数:
remoteAddress- the remote address (IP)
-
with
An extension point for further initialization ofMockHttpServletRequestin ways not built directly into theMockHttpServletRequestBuilder. Implementation of this interface can have builder-style methods themselves and be made accessible through static factory methods.- 指定者:
with在接口中ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>- 参数:
postProcessor- a post-processor to add
-
isMergeEnabled
public boolean isMergeEnabled()- 指定者:
isMergeEnabled在接口中cn.taketoday.beans.Mergeable- 返回:
- always returns
true.
-
merge
Merges the properties of the "parent" RequestBuilder accepting values only if not already set in "this" instance.- 指定者:
merge在接口中cn.taketoday.beans.Mergeable- 参数:
parent- the parentRequestBuilderto inherit properties from- 返回:
- the result of the merge
-
containsCookie
private boolean containsCookie(jakarta.servlet.http.Cookie cookie) -
buildRequest
Build aMockHttpServletRequest.- 指定者:
buildRequest在接口中RequestBuilder- 参数:
servletContext- theServletContextto use to create the request- 返回:
- the request
-
createServletRequest
protected MockHttpServletRequest createServletRequest(jakarta.servlet.ServletContext servletContext) Create a newMockHttpServletRequestbased on the suppliedServletContext.Can be overridden in subclasses.
-
updatePathRequestProperties
Update the contextPath, servletPath, and pathInfo of the request. -
addRequestParams
private void addRequestParams(MockHttpServletRequest request, cn.taketoday.util.MultiValueMap<String, String> map) -
parseFormData
-
postProcessRequest
从接口复制的说明:SmartRequestBuilderApply request post-processing. Typically, that means invoking one or morecn.taketoday.test.web.servlet.request.RequestPostProcessors.- 指定者:
postProcessRequest在接口中SmartRequestBuilder- 参数:
request- the request to initialize- 返回:
- the request to use, either the one passed in or a wrapped one
-
addToMap
-
addToMultiValueMap
-