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 Details

    • MockServerHttpResponse

      public MockServerHttpResponse()
    • MockServerHttpResponse

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

    • setWriteHandler

      public void setWriteHandler(Function<Flux<org.springframework.core.io.buffer.DataBuffer>, 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 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 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 Mono<Void> setComplete()
      Specified by:
      setComplete in interface org.springframework.http.ReactiveHttpOutputMessage
      Overrides:
      setComplete in class org.springframework.http.server.reactive.AbstractServerHttpResponse
    • getBody

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

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