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 ofAbstractServerHttpRequestfor 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 interfaceMockServerHttpRequest.BaseBuilder<B extends MockServerHttpRequest.BaseBuilder<B>>Request builder exposing properties not related to the body.static interfaceMockServerHttpRequest.BodyBuilderA 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.InetSocketAddressgetLocalAddress()Return the local address the request was accepted on, if available.HttpMethodgetMethod()Return the HTTP method of the request.StringgetMethodValue()Return the HTTP method of the request as a String value.<T> TgetNativeRequest()Return the underlying server response.InetSocketAddressgetRemoteAddress()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 anHttpCookiemap.protected SslInfoinitSslInfo()Obtain SSL session information from the underlying "native" request.static MockServerHttpRequest.BodyBuildermethod(String method, String urlTemplate, Object... vars)Create a builder with a raw HTTP method value that is outside the range ofHttpMethodenum values.static MockServerHttpRequest.BodyBuildermethod(HttpMethod method, String urlTemplate, Object... vars)Alternative tomethod(HttpMethod, URI)that accepts a URI template.static MockServerHttpRequest.BodyBuildermethod(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.BodyBuilderpatch(String urlTemplate, Object... uriVars)HTTP PATCH variant.static MockServerHttpRequest.BodyBuilderpost(String urlTemplate, Object... uriVars)HTTP POST variant.static MockServerHttpRequest.BodyBuilderput(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:HttpRequestReturn the HTTP method of the request.- Returns:
- the HTTP method as an HttpMethod enum value, or
nullif 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:HttpRequestReturn 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:ServerHttpRequestReturn the local address the request was accepted on, if available.
getRemoteAddress
@Nullable public InetSocketAddress getRemoteAddress()
Description copied from interface:ServerHttpRequestReturn the remote address where this request is connected to, if available.
initSslInfo
@Nullable protected SslInfo initSslInfo()
Description copied from class:AbstractServerHttpRequestObtain SSL session information from the underlying "native" request.- Specified by:
initSslInfoin classAbstractServerHttpRequest- Returns:
- the session information, or
nullif none available
getBody
public reactor.core.publisher.Flux<DataBuffer> getBody()
Description copied from interface:ReactiveHttpInputMessageReturn the body of the message as aPublisher.- Returns:
- the body content publisher
initCookies
protected MultiValueMap<String,HttpCookie> initCookies()
Description copied from class:AbstractServerHttpRequestObtain the cookies from the underlying "native" request and adapt those to anHttpCookiemap. 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:
initCookiesin classAbstractServerHttpRequest
getNativeRequest
public <T> T getNativeRequest()
Description copied from class:AbstractServerHttpRequestReturn the underlying server response.Note: This is exposed mainly for internal framework use such as WebSocket upgrades in the spring-webflux module.
- Specified by:
getNativeRequestin 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 viaqueryParambuilder 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.queryParambuilder 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 viaqueryParambuilder 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 ofHttpMethodenum values.- Parameters:
method- the HTTP method valueurlTemplate- the URL templatevars- variables to expand into the template- Returns:
- the created builder
- Since:
- 5.2.7