Search in sources :

Example 11 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.

the class BodyInsertersTests method ofResource.

@Test
public void ofResource() throws Exception {
    Resource body = new ClassPathResource("response.txt", getClass());
    BodyInserter<Resource, ReactiveHttpOutputMessage> inserter = BodyInserters.fromResource(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    byte[] expectedBytes = Files.readAllBytes(body.getFile().toPath());
    StepVerifier.create(response.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        assertArrayEquals(expectedBytes, resultBytes);
    }).expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 12 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.

the class DefaultServerResponseBuilderTests method buildVoidPublisher.

@Test
public void buildVoidPublisher() throws Exception {
    Mono<Void> mono = Mono.empty();
    Mono<ServerResponse> result = ServerResponse.ok().build(mono);
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    when(exchange.getResponse()).thenReturn(response);
    HandlerStrategies strategies = mock(HandlerStrategies.class);
    result.then(res -> res.writeTo(exchange, strategies)).block();
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) HttpHeaders(org.springframework.http.HttpHeaders) ZonedDateTime(java.time.ZonedDateTime) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) CacheControl(org.springframework.http.CacheControl) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) URI(java.net.URI) Collections(java.util.Collections) EnumSet(java.util.EnumSet) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 13 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.

the class BodyInsertersTests method ofServerSentEventFlux.

@Test
public void ofServerSentEventFlux() throws Exception {
    ServerSentEvent<String> event = ServerSentEvent.builder("foo").build();
    Flux<ServerSentEvent<String>> body = Flux.just(event);
    BodyInserter<Flux<ServerSentEvent<String>>, ServerHttpResponse> inserter = BodyInserters.fromServerSentEvents(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
}
Also used : ServerSentEvent(org.springframework.http.codec.ServerSentEvent) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 14 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.

the class BodyInsertersTests method ofString.

@Test
public void ofString() throws Exception {
    String body = "foo";
    BodyInserter<String, ReactiveHttpOutputMessage> inserter = BodyInserters.fromObject(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    ByteBuffer byteBuffer = ByteBuffer.wrap(body.getBytes(UTF_8));
    DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
    StepVerifier.create(response.getBody()).expectNext(buffer).expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ByteBuffer(java.nio.ByteBuffer) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 15 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.

the class ResourceHandlerFunctionTests method get.

@Test
public void get() throws IOException {
    MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
    MockServerHttpResponse mockResponse = exchange.getResponse();
    ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults());
    Mono<ServerResponse> responseMono = this.handlerFunction.handle(request);
    Mono<Void> result = responseMono.then(response -> {
        assertEquals(HttpStatus.OK, response.statusCode());
        return response.writeTo(exchange, HandlerStrategies.withDefaults());
    });
    StepVerifier.create(result).expectComplete().verify();
    byte[] expectedBytes = Files.readAllBytes(this.resource.getFile().toPath());
    StepVerifier.create(mockResponse.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        assertArrayEquals(expectedBytes, resultBytes);
    }).expectComplete().verify();
    assertEquals(MediaType.TEXT_PLAIN, mockResponse.getHeaders().getContentType());
    assertEquals(this.resource.contentLength(), mockResponse.getHeaders().getContentLength());
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Aggregations

MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)42 Test (org.junit.Test)37 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)14 HttpHandler (org.springframework.http.server.reactive.HttpHandler)9 HttpHeaders (org.springframework.http.HttpHeaders)8 HttpStatus (org.springframework.http.HttpStatus)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)8 Mono (reactor.core.publisher.Mono)8 StepVerifier (reactor.test.StepVerifier)8 MediaType (org.springframework.http.MediaType)7 Assert (org.junit.Assert)6 Mockito (org.mockito.Mockito)6 ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)6 ResponseStatusException (org.springframework.web.server.ResponseStatusException)6 HashMap (java.util.HashMap)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)4 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)4 Flux (reactor.core.publisher.Flux)4 Before (org.junit.Before)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3