Search in sources :

Example 6 with MockAsyncContext

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

the class StandardServletAsyncWebRequestTests method startAsyncMultipleTimes.

@Test
public void startAsyncMultipleTimes() throws Exception {
    this.asyncRequest.startAsync();
    this.asyncRequest.startAsync();
    this.asyncRequest.startAsync();
    // idempotent
    this.asyncRequest.startAsync();
    MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
    assertNotNull(context);
    assertEquals(1, context.getListeners().size());
}
Also used : MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) Test(org.junit.Test)

Example 7 with MockAsyncContext

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

the class StandardServletAsyncWebRequestTests method startAsyncAfterCompleted.

@Test
public void startAsyncAfterCompleted() throws Exception {
    this.asyncRequest.onComplete(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
    try {
        this.asyncRequest.startAsync();
        fail("expected exception");
    } catch (IllegalStateException ex) {
        assertEquals("Async processing has already completed", ex.getMessage());
    }
}
Also used : MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 8 with MockAsyncContext

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

the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitter.

@Test
public void responseBodyEmitter() throws Exception {
    MethodParameter returnType = returnType("handle");
    ResponseBodyEmitter emitter = new ResponseBodyEmitter();
    handleReturnValue(emitter, returnType);
    assertTrue(this.request.isAsyncStarted());
    assertEquals("", this.response.getContentAsString());
    SimpleBean bean = new SimpleBean();
    bean.setId(1L);
    bean.setName("Joe");
    emitter.send(bean);
    emitter.send("\n");
    bean.setId(2L);
    bean.setName("John");
    emitter.send(bean);
    emitter.send("\n");
    bean.setId(3L);
    bean.setName("Jason");
    emitter.send(bean);
    assertEquals("{\"id\":1,\"name\":\"Joe\"}\n" + "{\"id\":2,\"name\":\"John\"}\n" + "{\"id\":3,\"name\":\"Jason\"}", this.response.getContentAsString());
    MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
    assertNull(asyncContext.getDispatchedPath());
    emitter.complete();
    assertNotNull(asyncContext.getDispatchedPath());
}
Also used : MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Aggregations

MockAsyncContext (org.springframework.mock.web.test.MockAsyncContext)8 Test (org.junit.Test)7 AsyncEvent (javax.servlet.AsyncEvent)4 IOException (java.io.IOException)1 AsyncContext (javax.servlet.AsyncContext)1 AsyncListener (javax.servlet.AsyncListener)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MethodParameter (org.springframework.core.MethodParameter)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1 AsyncWebRequest (org.springframework.web.context.request.async.AsyncWebRequest)1 StandardServletAsyncWebRequest (org.springframework.web.context.request.async.StandardServletAsyncWebRequest)1 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)1