Search in sources :

Example 21 with MockServerHttpResponse

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

the class ContextPathCompositeHandlerTests method notFoundWithCommitAction.

// SPR-17144
@Test
public void notFoundWithCommitAction() {
    AtomicBoolean commitInvoked = new AtomicBoolean();
    ServerHttpRequest request = MockServerHttpRequest.get("/unknown/path").build();
    ServerHttpResponse response = new MockServerHttpResponse();
    response.beforeCommit(() -> {
        commitInvoked.set(true);
        return Mono.empty();
    });
    Map<String, HttpHandler> map = new HashMap<>();
    TestHttpHandler handler = new TestHttpHandler();
    map.put("/path", handler);
    new ContextPathCompositeHandler(map).handle(request, response).block(Duration.ofSeconds(5));
    assertNotInvoked(handler);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertThat(commitInvoked.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 22 with MockServerHttpResponse

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

the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.

// gh-26232
@Test
void monoResponseShouldNotLeakIfCancelled() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory);
    response.setWriteHandler(flux -> {
        throw AbortedException.beforeSend();
    });
    HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder());
    Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), ResolvableType.forClass(Mono.class), ResolvableType.forClass(Map.class), null, request, response, Collections.emptyMap());
    StepVerifier.create(result).expectError(AbortedException.class).verify();
    bufferFactory.checkForLeaks();
}
Also used : LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) AbortedException(reactor.netty.channel.AbortedException) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Map(java.util.Map) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 23 with MockServerHttpResponse

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

the class BodyInsertersTests method ofServerSentEventFlux.

@Test
public void ofServerSentEventFlux() {
    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.web.testfixture.http.server.reactive.MockServerHttpResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 24 with MockServerHttpResponse

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

the class BodyInsertersTests method ofResource.

@Test
public void ofResource() throws IOException {
    Resource resource = new ClassPathResource("response.txt", getClass());
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = BodyInserters.fromResource(resource).insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    byte[] expectedBytes = Files.readAllBytes(resource.getFile().toPath());
    StepVerifier.create(response.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo(expectedBytes);
    }).expectComplete().verify();
}
Also used : Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 25 with MockServerHttpResponse

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

the class BodyInsertersTests method ofProducerWithMono.

@Test
public void ofProducerWithMono() {
    Mono<User> body = Mono.just(new User("foo", "bar"));
    BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, User.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\",\"password\":\"bar\"}").expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)64 Test (org.junit.jupiter.api.Test)55 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)28 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)24 HttpHandler (org.springframework.http.server.reactive.HttpHandler)14 HttpHeaders (org.springframework.http.HttpHeaders)13 Flux (reactor.core.publisher.Flux)13 Mono (reactor.core.publisher.Mono)13 Collections (java.util.Collections)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 DataBuffer (org.springframework.core.io.buffer.DataBuffer)11 MultiValueMap (org.springframework.util.MultiValueMap)11 StandardCharsets (java.nio.charset.StandardCharsets)10 HttpStatus (org.springframework.http.HttpStatus)10 ResponseCookie (org.springframework.http.ResponseCookie)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)10 WebFilter (org.springframework.web.server.WebFilter)10 StepVerifier (reactor.test.StepVerifier)10