Search in sources :

Example 21 with HttpHandler

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

the class WebHttpHandlerBuilderTests method configWithoutFilters.

@Test
void configWithoutFilters() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(NoFilterConfig.class);
    context.refresh();
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    httpHandler.handle(request, response).block(ofMillis(5000));
    assertThat(response.getBodyAsString().block(ofMillis(5000))).isEqualTo("handled");
}
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 22 with HttpHandler

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

the class WebHttpHandlerBuilderTests method orderedWebExceptionHandlerBeans.

// SPR-15074
@Test
void orderedWebExceptionHandlerBeans() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(OrderedExceptionHandlerBeanConfig.class);
    context.refresh();
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    httpHandler.handle(request, response).block(ofMillis(5000));
    assertThat(response.getBodyAsString().block(ofMillis(5000))).isEqualTo("ExceptionHandlerB");
}
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 23 with HttpHandler

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

the class WebHttpHandlerBuilderTests method httpHandlerDecoratorFactoryBeans.

@Test
void httpHandlerDecoratorFactoryBeans() {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(new AnnotationConfigApplicationContext(HttpHandlerDecoratorFactoryBeansConfig.class)).build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    handler.handle(MockServerHttpRequest.get("/").build(), response).block();
    Function<String, Long> headerValue = name -> Long.valueOf(response.getHeaders().getFirst(name));
    assertThat(headerValue.apply("decoratorA")).isLessThan(headerValue.apply("decoratorB"));
    assertThat(headerValue.apply("decoratorC")).isLessThan(headerValue.apply("decoratorB"));
}
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) HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 24 with HttpHandler

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

the class ContextPathIntegrationTests method servletPathMapping.

@Test
void servletPathMapping() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(WebAppConfig.class);
    TomcatHttpServer server = new TomcatHttpServer();
    server.setContextPath("/app");
    server.setServletMapping("/api/*");
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    server.setHandler(httpHandler);
    server.afterPropertiesSet();
    server.start();
    try {
        String url = "http://localhost:" + server.getPort() + "/app/api/test";
        String actual = new RestTemplate().getForObject(url, String.class);
        assertThat(actual).isEqualTo("Tested in /app/api");
    } finally {
        server.stop();
    }
}
Also used : TomcatHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.TomcatHttpServer) HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Example 25 with HttpHandler

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

the class RouterFunctionsTests method toHttpHandlerHandlerThrowsException.

@Test
public void toHttpHandlerHandlerThrowsException() {
    HandlerFunction<ServerResponse> handlerFunction = request -> {
        throw new IllegalStateException();
    };
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) 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