Search in sources :

Example 1 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest 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 2 with MockServerHttpRequest

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

the class HandshakeWebSocketServiceTests method sessionAttributePredicate.

@Test
public void sessionAttributePredicate() {
    MockWebSession session = new MockWebSession();
    session.getAttributes().put("a1", "v1");
    session.getAttributes().put("a2", "v2");
    session.getAttributes().put("a3", "v3");
    session.getAttributes().put("a4", "v4");
    session.getAttributes().put("a5", "v5");
    MockServerHttpRequest request = initHandshakeRequest();
    MockServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build();
    TestRequestUpgradeStrategy upgradeStrategy = new TestRequestUpgradeStrategy();
    HandshakeWebSocketService service = new HandshakeWebSocketService(upgradeStrategy);
    service.setSessionAttributePredicate(name -> Arrays.asList("a1", "a3", "a5").contains(name));
    service.handleRequest(exchange, mock(WebSocketHandler.class)).block();
    HandshakeInfo info = upgradeStrategy.handshakeInfo;
    assertThat(info).isNotNull();
    Map<String, Object> attributes = info.getAttributes();
    assertThat(attributes).hasSize(3).containsEntry("a1", "v1").containsEntry("a3", "v3").containsEntry("a5", "v5");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Test(org.junit.jupiter.api.Test)

Example 3 with MockServerHttpRequest

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

the class RouterFunctionTests method andRoute.

@Test
public void andRoute() {
    RouterFunction<ServerResponse> routerFunction1 = request -> Mono.empty();
    RequestPredicate requestPredicate = request -> true;
    RouterFunction<ServerResponse> result = routerFunction1.andRoute(requestPredicate, this::handlerMethod);
    assertThat(result).isNotNull();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectNextCount(1).expectComplete().verify();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 4 with MockServerHttpRequest

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

the class RouterFunctionTests method andOther.

@Test
public void andOther() {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().bodyValue("42");
    RouterFunction<?> routerFunction1 = request -> Mono.empty();
    RouterFunction<ServerResponse> routerFunction2 = request -> Mono.just(handlerFunction);
    RouterFunction<?> result = routerFunction1.andOther(routerFunction2);
    assertThat(result).isNotNull();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectNextMatches(o -> o.equals(handlerFunction)).expectComplete().verify();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 5 with MockServerHttpRequest

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

the class DefaultEntityResponseBuilderTests method notModifiedLastModified.

@Test
public void notModifiedLastModified() {
    ZonedDateTime now = ZonedDateTime.now();
    ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
    EntityResponse<String> responseMono = EntityResponse.fromObject("bar").lastModified(oneMinuteBeforeNow).build().block();
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)).build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    responseMono.writeTo(exchange, DefaultServerResponseBuilderTests.EMPTY_CONTEXT);
    MockServerHttpResponse response = exchange.getResponse();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
    StepVerifier.create(response.getBody()).expectError(IllegalStateException.class).verify();
}
Also used : ZonedDateTime(java.time.ZonedDateTime) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)182 Test (org.junit.jupiter.api.Test)160 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)101 Mono (reactor.core.publisher.Mono)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)50 Collections (java.util.Collections)48 StepVerifier (reactor.test.StepVerifier)46 HttpHeaders (org.springframework.http.HttpHeaders)39 Flux (reactor.core.publisher.Flux)37 MediaType (org.springframework.http.MediaType)34 ServerWebExchange (org.springframework.web.server.ServerWebExchange)34 MultiValueMap (org.springframework.util.MultiValueMap)33 HttpStatus (org.springframework.http.HttpStatus)31 StandardCharsets (java.nio.charset.StandardCharsets)29 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)29 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)27 ClassPathResource (org.springframework.core.io.ClassPathResource)25 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)24 Optional (java.util.Optional)23