Package org.springframework.mock.http.server.reactive

# Class MockServerHttpResponse

java.lang.Object
org.springframework.http.server.reactive.AbstractServerHttpResponse
org.springframework.mock.http.server.reactive.MockServerHttpResponse

All Implemented Interfaces:
  `org.springframework.http.HttpMessage`, `org.springframework.http.ReactiveHttpOutputMessage`, `org.springframework.http.server.reactive.ServerHttpResponse`

public class MockServerHttpResponse extends org.springframework.http.server.reactive.AbstractServerHttpResponse
Mock extension of `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`.

Since:
  5.0
Author:
  Rossen Stoyanchev

## Constructor Summary

Constructors
Constructor
Description
`MockServerHttpResponse()`
`MockServerHttpResponse(org.springframework.core.io.buffer.DataBufferFactory dataBufferFactory)`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`applyCookies()`
`protected void`
`applyHeaders()`
`protected void`
`applyStatusCode()`
`reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer>`
`getBody()`
Return the response body or an error stream if the body was not set.
`reactor.core.publisher.Mono<String>`
`getBodyAsString()`
Aggregate response data and convert to a String using the "Content-Type" charset or "UTF-8" by default.
`<T> T`
`getNativeResponse()`
`reactor.core.publisher.Mono<Void>`
`setComplete()`
`void`
`setWriteHandler(Function<reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer>,reactor.core.publisher.Mono<Void>> writeHandler)`
Configure a custom handler to consume the response body.
`protected reactor.core.publisher.Mono<Void>`
`writeAndFlushWithInternal(Publisher<? extends Publisher<? extends org.springframework.core.io.buffer.DataBuffer>> body)`
`protected reactor.core.publisher.Mono<Void>`
`writeWithInternal(Publisher<? extends org.springframework.core.io.buffer.DataBuffer> body)`

### Methods inherited from class org.springframework.http.server.reactive.AbstractServerHttpResponse

`addCookie, beforeCommit, bufferFactory, doCommit, doCommit, getCookies, getHeaders, getStatusCode, isCommitted, setRawStatusCode, setStatusCode, touchDataBuffer, writeAndFlushWith, writeWith`

### 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.ServerHttpResponse

`getRawStatusCode`

## Constructor Details

### MockServerHttpResponse

public MockServerHttpResponse()

### MockServerHttpResponse

public MockServerHttpResponse(org.springframework.core.io.buffer.DataBufferFactory dataBufferFactory)

## Method Details

### setWriteHandler

public void setWriteHandler(Function<reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer>,reactor.core.publisher.Mono<Void>> writeHandler)
Configure a custom handler to consume the response body.

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.

Parameters:
  `writeHandler` - the write handler to use returning `Mono<Void>` when the body has been "written" (i.e. consumed).

### getNativeResponse

public <T> T getNativeResponse()

Specified by:
  `getNativeResponse` in class `org.springframework.http.server.reactive.AbstractServerHttpResponse`

### applyStatusCode

protected void applyStatusCode()

Specified by:
  `applyStatusCode` in class `org.springframework.http.server.reactive.AbstractServerHttpResponse`

### applyHeaders

protected void applyHeaders()

Specified by:
  `applyHeaders` in class `org.springframework.http.server.reactive.AbstractServerHttpResponse`

### applyCookies

protected void applyCookies()

Specified by:
  `applyCookies` in class `org.springframework.http.server.reactive.AbstractServerHttpResponse`

### writeWithInternal

protected reactor.core.publisher.Mono<Void> writeWithInternal(Publisher<? extends org.springframework.core.io.buffer.DataBuffer> body)

Specified by:
  `writeWithInternal` in class `org.springframework.http.server.reactive.AbstractServerHttpResponse`

### writeAndFlushWithInternal

protected reactor.core.publisher.Mono<Void> writeAndFlushWithInternal(Publisher<? extends Publisher<? extends org.springframework.core.io.buffer.DataBuffer>> body)

Specified by:
  `writeAndFlushWithInternal` in class `org.springframework.http.server.reactive.AbstractServerHttpResponse`

### setComplete

public reactor.core.publisher.Mono<Void> setComplete()

Specified by:
  `setComplete` in interface `org.springframework.http.ReactiveHttpOutputMessage`
Overrides:
  `setComplete` in class `org.springframework.http.server.reactive.AbstractServerHttpResponse`

### getBody

public reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer> getBody()
Return the response body or an error stream if the body was not set.

### getBodyAsString

public reactor.core.publisher.Mono<String> getBodyAsString()
Aggregate response data and convert to a String using the "Content-Type" charset or "UTF-8" by default.