Search in sources :

Example 1 with MockAsyncContext

use of cn.taketoday.web.testfixture.servlet.MockAsyncContext in project today-framework by TAKETODAY.

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(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) MockAsyncContext(cn.taketoday.web.testfixture.servlet.MockAsyncContext) MockAsyncContext(cn.taketoday.web.testfixture.servlet.MockAsyncContext) AsyncContext(jakarta.servlet.AsyncContext) URI(java.net.URI) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)

Example 2 with MockAsyncContext

use of cn.taketoday.web.testfixture.servlet.MockAsyncContext in project today-framework by TAKETODAY.

the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitter.

@Test
public void responseBodyEmitter() throws Exception {
    HandlerMethod handlerMethod = on(TestController.class).resolveHandlerMethod(ResponseBodyEmitter.class);
    ResponseBodyEmitter emitter = new ResponseBodyEmitter();
    this.handler.handleReturnValue(webRequest, handlerMethod, emitter);
    assertThat(this.request.isAsyncStarted()).isTrue();
    assertThat(this.response.getContentAsString()).isEqualTo("");
    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);
    assertThat(this.response.getContentAsString()).isEqualTo(("{\"id\":1,\"name\":\"Joe\"}\n" + "{\"id\":2,\"name\":\"John\"}\n" + "{\"id\":3,\"name\":\"Jason\"}"));
    MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
    assertThat(asyncContext.getDispatchedPath()).isNull();
    emitter.complete();
    assertThat(asyncContext.getDispatchedPath()).isNotNull();
}
Also used : MockAsyncContext(cn.taketoday.web.testfixture.servlet.MockAsyncContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockAsyncContext (cn.taketoday.web.testfixture.servlet.MockAsyncContext)2 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)1 AsyncContext (jakarta.servlet.AsyncContext)1 URI (java.net.URI)1 Test (org.junit.jupiter.api.Test)1