Search in sources :

Example 1 with ResponseEntity

use of cn.taketoday.http.ResponseEntity in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive.

@Test
void givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive() throws Exception {
    AbstractReactiveWebServerFactory factory = getFactory();
    factory.setShutdown(Shutdown.GRACEFUL);
    BlockingHandler blockingHandler = new BlockingHandler();
    this.webServer = factory.getWebServer(blockingHandler);
    this.webServer.start();
    Mono<ResponseEntity<Void>> request = getWebClient(this.webServer.getPort()).build().get().retrieve().toBodilessEntity();
    AtomicReference<ResponseEntity<Void>> responseReference = new AtomicReference<>();
    CountDownLatch responseLatch = new CountDownLatch(1);
    request.subscribe((response) -> {
        responseReference.set(response);
        responseLatch.countDown();
    });
    blockingHandler.awaitQueue();
    AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
    this.webServer.shutDownGracefully(result::set);
    assertThat(responseReference.get()).isNull();
    try {
        this.webServer.stop();
    } catch (Exception ex) {
    // Continue
    }
    System.out.println("Stopped");
    Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> GracefulShutdownResult.REQUESTS_ACTIVE == result.get());
    blockingHandler.completeOne();
}
Also used : ResponseEntity(cn.taketoday.http.ResponseEntity) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) WebClientRequestException(cn.taketoday.web.reactive.function.client.WebClientRequestException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 2 with ResponseEntity

use of cn.taketoday.http.ResponseEntity in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method whenARequestIsActiveThenStopWillComplete.

@Test
void whenARequestIsActiveThenStopWillComplete() throws InterruptedException, BrokenBarrierException {
    AbstractReactiveWebServerFactory factory = getFactory();
    BlockingHandler blockingHandler = new BlockingHandler();
    this.webServer = factory.getWebServer(blockingHandler);
    this.webServer.start();
    Mono<ResponseEntity<Void>> request = getWebClient(this.webServer.getPort()).build().get().retrieve().toBodilessEntity();
    AtomicReference<ResponseEntity<Void>> responseReference = new AtomicReference<>();
    CountDownLatch responseLatch = new CountDownLatch(1);
    request.subscribe((response) -> {
        responseReference.set(response);
        responseLatch.countDown();
    });
    blockingHandler.awaitQueue();
    try {
        this.webServer.stop();
    } catch (Exception ex) {
    // Continue
    }
    blockingHandler.completeOne();
}
Also used : ResponseEntity(cn.taketoday.http.ResponseEntity) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) WebClientRequestException(cn.taketoday.web.reactive.function.client.WebClientRequestException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 3 with ResponseEntity

use of cn.taketoday.http.ResponseEntity in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method whenARequestRemainsInFlightThenShutDownGracefullyDoesNotInvokeCallbackUntilTheRequestCompletes.

@Test
void whenARequestRemainsInFlightThenShutDownGracefullyDoesNotInvokeCallbackUntilTheRequestCompletes() throws Exception {
    AbstractReactiveWebServerFactory factory = getFactory();
    factory.setShutdown(Shutdown.GRACEFUL);
    BlockingHandler blockingHandler = new BlockingHandler();
    this.webServer = factory.getWebServer(blockingHandler);
    this.webServer.start();
    Mono<ResponseEntity<Void>> request = getWebClient(this.webServer.getPort()).build().get().retrieve().toBodilessEntity();
    AtomicReference<ResponseEntity<Void>> responseReference = new AtomicReference<>();
    CountDownLatch responseLatch = new CountDownLatch(1);
    request.subscribe((response) -> {
        responseReference.set(response);
        responseLatch.countDown();
    });
    blockingHandler.awaitQueue();
    AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
    this.webServer.shutDownGracefully(result::set);
    assertThat(responseReference.get()).isNull();
    blockingHandler.completeOne();
    assertThat(responseLatch.await(5, TimeUnit.SECONDS)).isTrue();
    Awaitility.await().atMost(Duration.ofSeconds(30)).until(() -> GracefulShutdownResult.IDLE == result.get());
}
Also used : ResponseEntity(cn.taketoday.http.ResponseEntity) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with ResponseEntity

use of cn.taketoday.http.ResponseEntity in project today-framework by TAKETODAY.

the class HttpEntityMethodProcessorTests method handleReturnValueWithETagAndETagFilter.

// SPR-13423
@Test
public void handleReturnValueWithETagAndETagFilter() throws Exception {
    String eTagValue = "\"deadb33f8badf00d\"";
    String content = "body";
    Method method = getClass().getDeclaredMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    FilterChain chain = (req, res) -> {
        ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(eTagValue).body(content);
        try {
            ServletRequestContext requestToUse = new ServletRequestContext(null, (HttpServletRequest) req, (HttpServletResponse) res);
            new HttpEntityMethodProcessor(Collections.singletonList(new StringHttpMessageConverter()), null).handleReturnValue(requestToUse, new HandlerMethod(this, method), returnValue);
            assertThat(this.servletResponse.getContentAsString()).as("Response body was cached? It should be written directly to the raw response").isEqualTo(content);
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    };
    this.servletRequest.setMethod("GET");
    new ShallowEtagHeaderFilter().doFilter(this.servletRequest, this.servletResponse, chain);
    assertThat(this.servletResponse.getStatus()).isEqualTo(200);
    assertThat(this.servletResponse.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue);
    assertThat(this.servletResponse.getContentAsString()).isEqualTo(content);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MethodParameter(cn.taketoday.core.MethodParameter) ArrayList(java.util.ArrayList) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) HttpHeaders(cn.taketoday.http.HttpHeaders) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) FilterChain(jakarta.servlet.FilterChain) ResponseBody(cn.taketoday.web.annotation.ResponseBody) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResponseEntity(cn.taketoday.http.ResponseEntity) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Test(org.junit.jupiter.api.Test) List(java.util.List) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestMapping(cn.taketoday.web.annotation.RequestMapping) ShallowEtagHeaderFilter(cn.taketoday.web.servlet.filter.ShallowEtagHeaderFilter) HttpEntity(cn.taketoday.http.HttpEntity) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) FilterChain(jakarta.servlet.FilterChain) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) ShallowEtagHeaderFilter(cn.taketoday.web.servlet.filter.ShallowEtagHeaderFilter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ResponseEntity(cn.taketoday.http.ResponseEntity) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Example 5 with ResponseEntity

use of cn.taketoday.http.ResponseEntity in project today-framework by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive.

@Test
void givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive() throws Exception {
    AbstractReactiveWebServerFactory factory = getFactory();
    factory.setShutdown(Shutdown.GRACEFUL);
    BlockingHandler blockingHandler = new BlockingHandler();
    this.webServer = factory.getWebServer(blockingHandler);
    this.webServer.start();
    Mono<ResponseEntity<Void>> request = getWebClient(this.webServer.getPort()).build().get().retrieve().toBodilessEntity();
    AtomicReference<ResponseEntity<Void>> responseReference = new AtomicReference<>();
    CountDownLatch responseLatch = new CountDownLatch(1);
    request.subscribe((response) -> {
        responseReference.set(response);
        responseLatch.countDown();
    });
    blockingHandler.awaitQueue();
    AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
    this.webServer.shutDownGracefully(result::set);
    assertThat(responseReference.get()).isNull();
    try {
        this.webServer.stop();
    } catch (Exception ex) {
    // Continue
    }
    System.out.println("Stopped");
    Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> GracefulShutdownResult.REQUESTS_ACTIVE == result.get());
    blockingHandler.completeOne();
}
Also used : ResponseEntity(cn.taketoday.http.ResponseEntity) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) WebClientRequestException(cn.taketoday.web.reactive.function.client.WebClientRequestException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

ResponseEntity (cn.taketoday.http.ResponseEntity)9 Test (org.junit.jupiter.api.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 GracefulShutdownResult (cn.taketoday.framework.web.server.GracefulShutdownResult)6 WebClientRequestException (cn.taketoday.web.reactive.function.client.WebClientRequestException)4 IOException (java.io.IOException)4 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)4 ExecutionException (java.util.concurrent.ExecutionException)4 MethodParameter (cn.taketoday.core.MethodParameter)1 HttpEntity (cn.taketoday.http.HttpEntity)1 HttpHeaders (cn.taketoday.http.HttpHeaders)1 MediaType (cn.taketoday.http.MediaType)1 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)1 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)1 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)1 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)1 RequestMapping (cn.taketoday.web.annotation.RequestMapping)1 ResponseBody (cn.taketoday.web.annotation.ResponseBody)1 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)1