Search in sources :

Example 11 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.

the class WebHttpHandlerBuilderTests method httpHandlerDecorator.

@Test
void httpHandlerDecorator() {
    BiFunction<ServerHttpRequest, String, ServerHttpRequest> mutator = (req, value) -> req.mutate().headers(headers -> headers.add("My-Header", value)).build();
    AtomicBoolean success = new AtomicBoolean();
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> {
        HttpHeaders headers = exchange.getRequest().getHeaders();
        assertThat(headers.get("My-Header")).containsExactlyInAnyOrder("1", "2", "3");
        success.set(true);
        return Mono.empty();
    }).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "1"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "2"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "3"), res)).build();
    httpHandler.handle(MockServerHttpRequest.get("/").build(), new MockServerHttpResponse()).block();
    assertThat(success.get()).isTrue();
}
Also used : HttpHandlerDecoratorFactory(org.springframework.http.server.reactive.HttpHandlerDecoratorFactory) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebHandler(org.springframework.web.server.WebHandler) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilter(org.springframework.web.server.WebFilter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Order(org.springframework.core.annotation.Order) HttpHeaders(org.springframework.http.HttpHeaders) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Duration.ofMillis(java.time.Duration.ofMillis) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpHeaders(org.springframework.http.HttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 12 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.

the class WebHttpHandlerBuilderTests method orderedWebFilterBeans.

// SPR-15074
@Test
void orderedWebFilterBeans() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(OrderedWebFilterBeanConfig.class);
    context.refresh();
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    boolean condition = httpHandler instanceof HttpWebHandlerAdapter;
    assertThat(condition).isTrue();
    assertThat(((HttpWebHandlerAdapter) httpHandler).getApplicationContext()).isSameAs(context);
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    httpHandler.handle(request, response).block(ofMillis(5000));
    assertThat(response.getBodyAsString().block(ofMillis(5000))).isEqualTo("FilterB::FilterA");
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 13 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.

the class HttpHandlerConnector method doConnect.

private Mono<ClientHttpResponse> doConnect(HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
    // unsafe(): we're intercepting, already serialized Publisher signals
    Sinks.Empty<Void> requestWriteSink = Sinks.unsafe().empty();
    Sinks.Empty<Void> handlerSink = Sinks.unsafe().empty();
    ClientHttpResponse[] savedResponse = new ClientHttpResponse[1];
    MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
    MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();
    mockClientRequest.setWriteHandler(requestBody -> {
        log("Invoking HttpHandler for ", httpMethod, uri);
        ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
        ServerHttpResponse responseToUse = prepareResponse(mockServerResponse, mockServerRequest);
        this.handler.handle(mockServerRequest, responseToUse).subscribe(aVoid -> {
        }, // Ignore result: signals cannot compete
        handlerSink::tryEmitError, handlerSink::tryEmitEmpty);
        return Mono.empty();
    });
    mockServerResponse.setWriteHandler(responseBody -> Mono.fromRunnable(() -> {
        log("Creating client response for ", httpMethod, uri);
        savedResponse[0] = adaptResponse(mockServerResponse, responseBody);
    }));
    log("Writing client request for ", httpMethod, uri);
    requestCallback.apply(mockClientRequest).subscribe(aVoid -> {
    }, // Ignore result: signals cannot compete
    requestWriteSink::tryEmitError, requestWriteSink::tryEmitEmpty);
    return Mono.when(requestWriteSink.asMono(), handlerSink.asMono()).onErrorMap(ex -> {
        ClientHttpResponse response = savedResponse[0];
        return response != null ? new FailureAfterResponseCompletedException(response, ex) : ex;
    }).then(Mono.fromCallable(() -> savedResponse[0] != null ? savedResponse[0] : adaptResponse(mockServerResponse, Flux.empty())));
}
Also used : Sinks(reactor.core.publisher.Sinks) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Function(java.util.function.Function) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) HttpCookie(org.springframework.http.HttpCookie) Schedulers(reactor.core.scheduler.Schedulers) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) HttpHeadResponseDecorator(org.springframework.http.server.reactive.HttpHeadResponseDecorator) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) Assert(org.springframework.util.Assert) Sinks(reactor.core.publisher.Sinks) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse)

Example 14 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.

the class ServerWebExchangeContextFilterTests method extractServerWebExchangeFromContext.

@Test
void extractServerWebExchangeFromContext() {
    MyService service = new MyService();
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> service.service().then()).filter(new ServerWebExchangeContextFilter()).build();
    httpHandler.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse()).block(Duration.ofSeconds(5));
    assertThat(service.getExchange()).isNotNull();
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 15 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.

the class ContextPathIntegrationTests method multipleWebFluxApps.

@Test
void multipleWebFluxApps() throws Exception {
    AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(WebAppConfig.class);
    AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(WebAppConfig.class);
    HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build();
    HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build();
    ReactorHttpServer server = new ReactorHttpServer();
    server.registerHttpHandler("/webApp1", webApp1Handler);
    server.registerHttpHandler("/webApp2", webApp2Handler);
    server.afterPropertiesSet();
    server.start();
    try {
        RestTemplate restTemplate = new RestTemplate();
        String actual;
        String url = "http://localhost:" + server.getPort() + "/webApp1/test";
        actual = restTemplate.getForObject(url, String.class);
        assertThat(actual).isEqualTo("Tested in /webApp1");
        url = "http://localhost:" + server.getPort() + "/webApp2/test";
        actual = restTemplate.getForObject(url, String.class);
        assertThat(actual).isEqualTo("Tested in /webApp2");
    } finally {
        server.stop();
    }
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ReactorHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

HttpHandler (org.springframework.http.server.reactive.HttpHandler)29 Test (org.junit.jupiter.api.Test)17 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)14 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)13 Mono (reactor.core.publisher.Mono)13 WebFilter (org.springframework.web.server.WebFilter)12 Flux (reactor.core.publisher.Flux)12 DataBuffer (org.springframework.core.io.buffer.DataBuffer)11 HttpHeaders (org.springframework.http.HttpHeaders)11 StandardCharsets (java.nio.charset.StandardCharsets)10 Collections (java.util.Collections)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)10 MultiValueMap (org.springframework.util.MultiValueMap)9 Optional (java.util.Optional)8 BDDMockito.given (org.mockito.BDDMockito.given)8 Mockito.mock (org.mockito.Mockito.mock)8 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)8 HttpStatus (org.springframework.http.HttpStatus)8