Search in sources :

Example 1 with MockAsyncContext

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

the class TestDispatcherServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    registerAsyncResultInterceptors(request);
    super.service(request, response);
    if (request.getAsyncContext() != null) {
        MockAsyncContext asyncContext;
        if (request.getAsyncContext() instanceof MockAsyncContext) {
            asyncContext = (MockAsyncContext) request.getAsyncContext();
        } else {
            MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
            Assert.notNull(mockRequest, "Expected MockHttpServletRequest");
            asyncContext = (MockAsyncContext) mockRequest.getAsyncContext();
            String requestClassName = request.getClass().getName();
            Assert.notNull(asyncContext, () -> "Outer request wrapper " + requestClassName + " has an AsyncContext," + "but it is not a MockAsyncContext, while the nested " + mockRequest.getClass().getName() + " does not have an AsyncContext at all.");
        }
        CountDownLatch dispatchLatch = new CountDownLatch(1);
        asyncContext.addDispatchHandler(dispatchLatch::countDown);
        getMvcResult(request).setAsyncDispatchLatch(dispatchLatch);
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockAsyncContext(org.springframework.mock.web.MockAsyncContext) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with MockAsyncContext

use of org.springframework.mock.web.MockAsyncContext in project cxf by apache.

the class SseEventSinkContextProviderTest method setUp.

@Before
public void setUp() {
    provider = new SseEventSinkContextProvider();
    final Exchange exchange = mock(Exchange.class);
    final Endpoint endpoint = mock(Endpoint.class);
    final ContinuationProvider continuationProvider = mock(ContinuationProvider.class);
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockAsyncContext ctx = new MockAsyncContext(request, response) {

        @Override
        public void start(Runnable runnable) {
        /* do nothing */
        }
    };
    request.setAsyncContext(ctx);
    message = new MessageImpl();
    message.setExchange(exchange);
    message.put(ContinuationProvider.class.getName(), continuationProvider);
    message.put(AbstractHTTPDestination.HTTP_REQUEST, request);
    when(exchange.getEndpoint()).thenReturn(endpoint);
}
Also used : Exchange(org.apache.cxf.message.Exchange) ContinuationProvider(org.apache.cxf.continuations.ContinuationProvider) Endpoint(org.apache.cxf.endpoint.Endpoint) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockAsyncContext(org.springframework.mock.web.MockAsyncContext) MessageImpl(org.apache.cxf.message.MessageImpl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 3 with MockAsyncContext

use of org.springframework.mock.web.MockAsyncContext in project cxf by apache.

the class SseBroadcasterImplTest method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() {
    broadcaster = new SseBroadcasterImpl();
    response = new MockHttpServletResponse();
    writer = mock(MessageBodyWriter.class);
    ctx = new MockAsyncContext(new MockHttpServletRequest(), response);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockAsyncContext(org.springframework.mock.web.MockAsyncContext) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

MockAsyncContext (org.springframework.mock.web.MockAsyncContext)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 Before (org.junit.Before)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)1 ContinuationProvider (org.apache.cxf.continuations.ContinuationProvider)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 Exchange (org.apache.cxf.message.Exchange)1 MessageImpl (org.apache.cxf.message.MessageImpl)1