Search in sources :

Example 6 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerErrorTests method startDeferredResultProcessingErrorAndResumeThroughCallback.

@Test
public void startDeferredResultProcessingErrorAndResumeThroughCallback() throws Exception {
    final DeferredResult<Throwable> deferredResult = new DeferredResult<>();
    deferredResult.onError(t -> deferredResult.setResult(t));
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    Exception e = new Exception();
    AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
    this.asyncWebRequest.onError(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(e);
    assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 7 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerErrorTests method startDeferredResultProcessingErrorAndResumeThroughInterceptor.

@Test
public void startDeferredResultProcessingErrorAndResumeThroughInterceptor() throws Exception {
    DeferredResult<Integer> deferredResult = new DeferredResult<>();
    DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptor() {

        @Override
        public <T> boolean handleError(NativeWebRequest request, DeferredResult<T> result, Throwable t) throws Exception {
            result.setErrorResult(t);
            return true;
        }
    };
    this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    Exception e = new Exception();
    AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
    this.asyncWebRequest.onError(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(e);
    assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 8 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingTimeoutAndResumeThroughInterceptor.

@Test
public void startDeferredResultProcessingTimeoutAndResumeThroughInterceptor() throws Exception {
    DeferredResult<Integer> deferredResult = new DeferredResult<>();
    DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptor() {

        @Override
        public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
            result.setErrorResult(23);
            return true;
        }
    };
    this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = null;
    this.asyncWebRequest.onTimeout(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(23);
    assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 9 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingTimeoutAndResumeWithDefaultResult.

@Test
public void startDeferredResultProcessingTimeoutAndResumeWithDefaultResult() throws Exception {
    DeferredResult<Integer> deferredResult = new DeferredResult<>(null, 23);
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = null;
    this.asyncWebRequest.onTimeout(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(23);
    assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 10 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingTimeoutAndResumeThroughCallback.

@Test
public void startDeferredResultProcessingTimeoutAndResumeThroughCallback() throws Exception {
    final DeferredResult<Integer> deferredResult = new DeferredResult<>();
    deferredResult.onTimeout(() -> deferredResult.setResult(23));
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = null;
    this.asyncWebRequest.onTimeout(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(23);
    assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Aggregations

MockAsyncContext (org.springframework.web.testfixture.servlet.MockAsyncContext)23 Test (org.junit.jupiter.api.Test)22 AsyncEvent (jakarta.servlet.AsyncEvent)19 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)4 AsyncListener (jakarta.servlet.AsyncListener)2 AsyncWebRequest (org.springframework.web.context.request.async.AsyncWebRequest)2 StandardServletAsyncWebRequest (org.springframework.web.context.request.async.StandardServletAsyncWebRequest)2 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)2 AsyncContext (jakarta.servlet.AsyncContext)1 URI (java.net.URI)1 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)1 MethodParameter (org.springframework.core.MethodParameter)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1