Class MockServerHttpRequest
- java.lang.Object
- org.springframework.http.server.reactive.AbstractServerHttpRequest
- org.springframework.mock.http.server.reactive.MockServerHttpRequest
- All Implemented Interfaces:
HttpMessage
,HttpRequest
,ReactiveHttpInputMessage
,ServerHttpRequest
public final class MockServerHttpRequest extends AbstractServerHttpRequest
Mock extension ofAbstractServerHttpRequest
for use in tests without an actual server. Use the static methods to obtain a builder.- Since:
- 5.0
- Author:
- Rossen Stoyanchev
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
MockServerHttpRequest.BaseBuilder<B extends MockServerHttpRequest.BaseBuilder<B>>
Request builder exposing properties not related to the body.static interface
MockServerHttpRequest.BodyBuilder
A builder that adds a body to the request.Nested classes/interfaces inherited from interface org.springframework.http.server.reactive.ServerHttpRequest
ServerHttpRequest.Builder
Field Summary
Fields inherited from class org.springframework.http.server.reactive.AbstractServerHttpRequest
logger
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MockServerHttpRequest.BaseBuilder<?>
delete(String urlTemplate, Object... uriVars)
HTTP DELETE variant.static MockServerHttpRequest.BaseBuilder<?>
get(String urlTemplate, Object... uriVars)
Create an HTTP GET builder with the given URI template.reactor.core.publisher.Flux<DataBuffer>
getBody()
Return the body of the message as aPublisher
.InetSocketAddress
getLocalAddress()
Return the local address the request was accepted on, if available.HttpMethod
getMethod()
Return the HTTP method of the request.String
getMethodValue()
Return the HTTP method of the request as a String value.<T> T
getNativeRequest()
Return the underlying server response.InetSocketAddress
getRemoteAddress()
Return the remote address where this request is connected to, if available.static MockServerHttpRequest.BaseBuilder<?>
head(String urlTemplate, Object... uriVars)
HTTP HEAD variant.protected MultiValueMap<String,HttpCookie>
initCookies()
Obtain the cookies from the underlying "native" request and adapt those to anHttpCookie
map.protected SslInfo
initSslInfo()
Obtain SSL session information from the underlying "native" request.static MockServerHttpRequest.BodyBuilder
method(String method, String urlTemplate, Object... vars)
Create a builder with a raw HTTP method value that is outside the range ofHttpMethod
enum values.static MockServerHttpRequest.BodyBuilder
method(HttpMethod method, String urlTemplate, Object... vars)
Alternative tomethod(HttpMethod, URI)
that accepts a URI template.static MockServerHttpRequest.BodyBuilder
method(HttpMethod method, URI url)
Create a builder with the given HTTP method and aURI
.static MockServerHttpRequest.BaseBuilder<?>
options(String urlTemplate, Object... uriVars)
HTTP OPTIONS variant.static MockServerHttpRequest.BodyBuilder
patch(String urlTemplate, Object... uriVars)
HTTP PATCH variant.static MockServerHttpRequest.BodyBuilder
post(String urlTemplate, Object... uriVars)
HTTP POST variant.static MockServerHttpRequest.BodyBuilder
put(String urlTemplate, Object... uriVars)
HTTP PUT variant.Methods inherited from class org.springframework.http.server.reactive.AbstractServerHttpRequest
getCookies, getHeaders, getId, getPath, getQueryParams, getSslInfo, getURI, initId, initQueryParams
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.http.server.reactive.ServerHttpRequest
mutate
Method Detail
getMethod
public HttpMethod getMethod()
Description copied from interface:HttpRequest
Return the HTTP method of the request.- Returns:
- the HTTP method as an HttpMethod enum value, or
null
if not resolvable (e.g. in case of a non-standard HTTP method) - See Also:
HttpRequest.getMethodValue()
,HttpMethod.resolve(String)
getMethodValue
public String getMethodValue()
Description copied from interface:HttpRequest
Return the HTTP method of the request as a String value.- Returns:
- the HTTP method as a plain String
- See Also:
HttpRequest.getMethod()
getLocalAddress
@Nullable public InetSocketAddress getLocalAddress()
Description copied from interface:ServerHttpRequest
Return the local address the request was accepted on, if available.
getRemoteAddress
@Nullable public InetSocketAddress getRemoteAddress()
Description copied from interface:ServerHttpRequest
Return the remote address where this request is connected to, if available.
initSslInfo
@Nullable protected SslInfo initSslInfo()
Description copied from class:AbstractServerHttpRequest
Obtain SSL session information from the underlying "native" request.- Specified by:
initSslInfo
in classAbstractServerHttpRequest
- Returns:
- the session information, or
null
if none available
getBody
public reactor.core.publisher.Flux<DataBuffer> getBody()
Description copied from interface:ReactiveHttpInputMessage
Return the body of the message as aPublisher
.- Returns:
- the body content publisher
initCookies
protected MultiValueMap<String,HttpCookie> initCookies()
Description copied from class:AbstractServerHttpRequest
Obtain the cookies from the underlying "native" request and adapt those to anHttpCookie
map. The return value is turned into an immutable map and cached.Note that this method is invoked lazily on access to
AbstractServerHttpRequest.getCookies()
. Sub-classes should synchronize cookie initialization if the underlying "native" request does not provide thread-safe access to cookie data.- Specified by:
initCookies
in classAbstractServerHttpRequest
getNativeRequest
public <T> T getNativeRequest()
Description copied from class:AbstractServerHttpRequest
Return the underlying server response.Note: This is exposed mainly for internal framework use such as WebSocket upgrades in the spring-webflux module.
- Specified by:
getNativeRequest
in classAbstractServerHttpRequest
get
public static MockServerHttpRequest.BaseBuilder<?> get(String urlTemplate, Object... uriVars)
Create an HTTP GET builder with the given URI template. The given URI may contain query parameters, or those may be added later viaqueryParam
builder methods.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Returns:
- the created builder
head
public static MockServerHttpRequest.BaseBuilder<?> head(String urlTemplate, Object... uriVars)
HTTP HEAD variant. Seeget(String, Object...)
for general info.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Returns:
- the created builder
post
public static MockServerHttpRequest.BodyBuilder post(String urlTemplate, Object... uriVars)
HTTP POST variant. Seeget(String, Object...)
for general info.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Returns:
- the created builder
put
public static MockServerHttpRequest.BodyBuilder put(String urlTemplate, Object... uriVars)
HTTP PUT variant. Seeget(String, Object...)
for general info.queryParam
builder methods.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Returns:
- the created builder
patch
public static MockServerHttpRequest.BodyBuilder patch(String urlTemplate, Object... uriVars)
HTTP PATCH variant. Seeget(String, Object...)
for general info.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Returns:
- the created builder
delete
public static MockServerHttpRequest.BaseBuilder<?> delete(String urlTemplate, Object... uriVars)
HTTP DELETE variant. Seeget(String, Object...)
for general info.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Returns:
- the created builder
options
public static MockServerHttpRequest.BaseBuilder<?> options(String urlTemplate, Object... uriVars)
HTTP OPTIONS variant. Seeget(String, Object...)
for general info.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Returns:
- the created builder
method
public static MockServerHttpRequest.BodyBuilder method(HttpMethod method, URI url)
Create a builder with the given HTTP method and aURI
.- Parameters:
method
- the HTTP method (GET, POST, etc)url
- the URL- Returns:
- the created builder
method
public static MockServerHttpRequest.BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars)
Alternative tomethod(HttpMethod, URI)
that accepts a URI template. The given URI may contain query parameters, or those may be added later viaqueryParam
builder methods.- Parameters:
method
- the HTTP method (GET, POST, etc)urlTemplate
- the URL templatevars
- variables to expand into the template- Returns:
- the created builder
method
public static MockServerHttpRequest.BodyBuilder method(String method, String urlTemplate, Object... vars)
Create a builder with a raw HTTP method value that is outside the range ofHttpMethod
enum values.- Parameters:
method
- the HTTP method valueurlTemplate
- the URL templatevars
- variables to expand into the template- Returns:
- the created builder
- Since:
- 5.2.7