Search in sources :

Example 11 with WebRequest

use of com.enonic.xp.web.WebRequest in project xp by enonic.

the class ResponseSerializerTest method serializeBodyBytes.

@Test
public void serializeBodyBytes() throws Exception {
    final WebRequest req = new WebRequest();
    req.setMethod(HttpMethod.GET);
    final byte[] body_bytes = "String body".getBytes(StandardCharsets.UTF_8);
    final WebResponse resp = WebResponse.create().status(HttpStatus.ACCEPTED).contentType(MediaType.PLAIN_TEXT_UTF_8).header("header-test", "header-value").cookie(new Cookie("cookie-name", "cookie-value")).body(body_bytes).build();
    final ResponseSerializer serializer = new ResponseSerializer(req, resp);
    final HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    final ServletOutputStream servletOutputStream = mock(ServletOutputStream.class);
    when(httpResponse.getOutputStream()).thenReturn(servletOutputStream);
    serializer.serialize(httpResponse);
    verify(httpResponse).setHeader("header-test", "header-value");
    verify(httpResponse).setStatus(202);
    verify(httpResponse).setContentType("text/plain; charset=utf-8");
    verify(httpResponse).setContentLength(11);
    verify(servletOutputStream).write(body_bytes);
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 12 with WebRequest

use of com.enonic.xp.web.WebRequest in project xp by enonic.

the class ResponseSerializerTest method serializeRequestCommitted.

@Test
public void serializeRequestCommitted() throws Exception {
    final WebRequest req = new WebRequest();
    req.setMethod(HttpMethod.GET);
    final WebResponse resp = WebResponse.create().status(HttpStatus.OK).contentType(MediaType.PLAIN_TEXT_UTF_8).body("String body").build();
    final ResponseSerializer serializer = new ResponseSerializer(req, resp);
    final HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    when(httpResponse.isCommitted()).thenReturn(true);
    serializer.serialize(httpResponse);
    verify(httpResponse).isCommitted();
    verifyNoMoreInteractions(httpResponse);
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 13 with WebRequest

use of com.enonic.xp.web.WebRequest in project xp by enonic.

the class AdminSiteHandlerTest method setup.

@BeforeEach
public final void setup() throws Exception {
    final ExceptionMapper exceptionMapper = Mockito.mock(ExceptionMapper.class);
    final ExceptionRenderer exceptionRenderer = Mockito.mock(ExceptionRenderer.class);
    this.handler = new AdminSiteHandler();
    this.handler.setExceptionMapper(exceptionMapper);
    this.handler.setExceptionRenderer(exceptionRenderer);
    this.handler.activate(mock(AdminConfig.class, invocation -> invocation.getMethod().getDefaultValue()));
    final HttpServletRequest rawRequest = Mockito.mock(HttpServletRequest.class);
    this.request = new WebRequest();
    this.request.setRawRequest(rawRequest);
    this.response = WebResponse.create().build();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) WebException(com.enonic.xp.web.WebException) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) WebResponse(com.enonic.xp.web.WebResponse) ExceptionMapper(com.enonic.xp.web.exception.ExceptionMapper) WebRequest(com.enonic.xp.web.WebRequest) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) HttpServletRequest(javax.servlet.http.HttpServletRequest) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExceptionRenderer(com.enonic.xp.web.exception.ExceptionRenderer) Mockito.mock(org.mockito.Mockito.mock) HttpServletRequest(javax.servlet.http.HttpServletRequest) ExceptionMapper(com.enonic.xp.web.exception.ExceptionMapper) WebRequest(com.enonic.xp.web.WebRequest) ExceptionRenderer(com.enonic.xp.web.exception.ExceptionRenderer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with WebRequest

use of com.enonic.xp.web.WebRequest in project xp by enonic.

the class WebDispatcherServlet method service.

@Override
protected void service(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    final WebRequest webRequest = newWebRequest(req);
    final WebSocketContext webSocketContext = this.webSocketContextFactory.newContext(req, res);
    webRequest.setWebSocketContext(webSocketContext);
    final WebResponse webResponse = doHandle(webRequest);
    final WebSocketConfig config = webResponse.getWebSocket();
    if ((webSocketContext != null) && (config != null)) {
        return;
    }
    responseSerializationService.serialize(webRequest, webResponse, res);
}
Also used : WebSocketConfig(com.enonic.xp.web.websocket.WebSocketConfig) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) WebSocketContext(com.enonic.xp.web.websocket.WebSocketContext)

Aggregations

WebRequest (com.enonic.xp.web.WebRequest)14 Test (org.junit.jupiter.api.Test)12 WebResponse (com.enonic.xp.web.WebResponse)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Cookie (javax.servlet.http.Cookie)6 ServletOutputStream (javax.servlet.ServletOutputStream)5 PortalRequest (com.enonic.xp.portal.PortalRequest)1 ResourceKey (com.enonic.xp.resource.ResourceKey)1 UrlResource (com.enonic.xp.resource.UrlResource)1 HttpStatus (com.enonic.xp.web.HttpStatus)1 WebException (com.enonic.xp.web.WebException)1 ExceptionMapper (com.enonic.xp.web.exception.ExceptionMapper)1 ExceptionRenderer (com.enonic.xp.web.exception.ExceptionRenderer)1 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)1 WebSocketConfig (com.enonic.xp.web.websocket.WebSocketConfig)1 WebSocketContext (com.enonic.xp.web.websocket.WebSocketContext)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1