public class MockServerHttpResponse extends AbstractServerHttpResponse
AbstractServerHttpResponse
for use in tests without
an actual server.
By default response content is consumed in full upon writing and cached
for subsequent access, however it is also possible to set a custom
writeHandler
.
Modifier and Type | Field and Description |
---|---|
private <any> |
body |
private java.util.function.Function<<any>,<any>> |
writeHandler |
logger
Constructor and Description |
---|
MockServerHttpResponse() |
Modifier and Type | Method and Description |
---|---|
protected void |
applyCookies()
Add cookies from
AbstractServerHttpResponse.getHeaders() to the underlying response. |
protected void |
applyHeaders()
Apply header changes from
AbstractServerHttpResponse.getHeaders() to the underlying response. |
protected void |
applyStatusCode()
Write the status code to the underlying response.
|
private static java.lang.String |
bufferToString(DataBuffer buffer,
java.nio.charset.Charset charset) |
<any> |
getBody()
Return the response body or an error stream if the body was not set.
|
<any> |
getBodyAsString()
Aggregate response data and convert to a String using the "Content-Type"
charset or "UTF-8" by default.
|
<T> T |
getNativeResponse()
Return the underlying server response.
|
<any> |
setComplete()
Indicate that message handling is complete, allowing for any cleanup or
end-of-processing tasks to be performed such as applying header changes
made via
HttpMessage.getHeaders() to the underlying HTTP message (if not
applied already). |
void |
setWriteHandler(java.util.function.Function<<any>,<any>> writeHandler)
Configure a custom handler to consume the response body.
|
protected <any> |
writeAndFlushWithInternal(<any> body)
Write to the underlying the response, and flush after each
Publisher<DataBuffer> . |
protected <any> |
writeWithInternal(<any> body)
Write to the underlying the response.
|
addCookie, beforeCommit, bufferFactory, doCommit, doCommit, getCookies, getHeaders, getStatusCode, getStatusCodeValue, isCommitted, setStatusCode, setStatusCodeValue, writeAndFlushWith, writeWith
private <any> body
private java.util.function.Function<<any>,<any>> writeHandler
public void setWriteHandler(java.util.function.Function<<any>,<any>> writeHandler)
By default, response body content is consumed in full and cached for subsequent access in tests. Use this option to take control over how the response body is consumed.
writeHandler
- the write handler to use returning Mono<Void>
when the body has been "written" (i.e. consumed).public <T> T getNativeResponse()
AbstractServerHttpResponse
Note: This is exposed mainly for internal framework use such as WebSocket upgrades in the spring-webflux module.
getNativeResponse
in class AbstractServerHttpResponse
protected void applyStatusCode()
AbstractServerHttpResponse
applyStatusCode
in class AbstractServerHttpResponse
protected void applyHeaders()
AbstractServerHttpResponse
AbstractServerHttpResponse.getHeaders()
to the underlying response.
This method is called once only.applyHeaders
in class AbstractServerHttpResponse
protected void applyCookies()
AbstractServerHttpResponse
AbstractServerHttpResponse.getHeaders()
to the underlying response.
This method is called once only.applyCookies
in class AbstractServerHttpResponse
protected <any> writeWithInternal(<any> body)
AbstractServerHttpResponse
writeWithInternal
in class AbstractServerHttpResponse
body
- the publisher to write withprotected <any> writeAndFlushWithInternal(<any> body)
AbstractServerHttpResponse
Publisher<DataBuffer>
.writeAndFlushWithInternal
in class AbstractServerHttpResponse
body
- the publisher to write and flush withpublic <any> setComplete()
ReactiveHttpOutputMessage
HttpMessage.getHeaders()
to the underlying HTTP message (if not
applied already).
This method should be automatically invoked at the end of message processing so typically applications should not have to invoke it. If invoked multiple times it should have no side effects.
setComplete
in interface ReactiveHttpOutputMessage
setComplete
in class AbstractServerHttpResponse
Mono
that indicates completion or errorpublic <any> getBody()
public <any> getBodyAsString()
private static java.lang.String bufferToString(DataBuffer buffer, java.nio.charset.Charset charset)