Search in sources :

Example 21 with MockAsyncContext

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

the class WebAsyncManagerErrorTests method startCallableProcessingErrorAndResumeThroughCallback.

@Test
public void startCallableProcessingErrorAndResumeThroughCallback() throws Exception {
    StubCallable callable = new StubCallable();
    WebAsyncTask<Object> webAsyncTask = new WebAsyncTask<>(callable);
    webAsyncTask.onError(() -> 7);
    this.asyncManager.startCallableProcessing(webAsyncTask);
    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(7);
    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 22 with MockAsyncContext

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

the class WebAsyncManagerErrorTests method startDeferredResultProcessingAfterException.

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

        @Override
        public <T> boolean handleError(NativeWebRequest request, DeferredResult<T> result, Throwable t) throws Exception {
            throw exception;
        }
    };
    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 23 with MockAsyncContext

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

the class ServerHttpRequestTests method createRequest.

private ServerHttpRequest createRequest(String uriString, String contextPath) throws Exception {
    URI uri = URI.create(uriString);
    MockHttpServletRequest request = new TestHttpServletRequest(uri);
    request.setContextPath(contextPath);
    AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
    return new ServletServerHttpRequest(request, asyncContext, "", DefaultDataBufferFactory.sharedInstance, 1024);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncContext(jakarta.servlet.AsyncContext) URI(java.net.URI) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

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