Search in sources :

Example 1 with WebRequest

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

the class EndpointHandlerTest method testPreRestPath.

@Test
public void testPreRestPath() {
    final WebRequest webRequest = new WebRequest();
    webRequest.setEndpointPath("/_/endpoint/a/b");
    webRequest.setRawPath("/site/www/_/endpoint/a/b");
    assertEquals("/site/www/_/endpoint", endpointHandler.findPreRestPath(webRequest));
    webRequest.setEndpointPath("/_/endpoint/");
    webRequest.setRawPath("/site/www/_/endpoint/");
    assertEquals("/site/www/_/endpoint", endpointHandler.findPreRestPath(webRequest));
    webRequest.setEndpointPath("/_/endpoint");
    webRequest.setRawPath("/site/www/_/endpoint");
    assertEquals("/site/www/_/endpoint", endpointHandler.findPreRestPath(webRequest));
}
Also used : WebRequest(com.enonic.xp.web.WebRequest) Test(org.junit.jupiter.api.Test)

Example 2 with WebRequest

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

the class EndpointHandlerTest method testCanHandle.

@Test
public void testCanHandle() {
    final WebRequest webRequest = new WebRequest();
    webRequest.setEndpointPath("/_/endpoint/a/b");
    assertTrue(endpointHandler.canHandle(webRequest));
    webRequest.setEndpointPath("/_/endpoint/");
    assertTrue(endpointHandler.canHandle(webRequest));
    webRequest.setEndpointPath("/_/endpoint");
    assertTrue(endpointHandler.canHandle(webRequest));
    webRequest.setEndpointPath("/_/otherendpoint");
    assertFalse(endpointHandler.canHandle(webRequest));
    webRequest.setEndpointPath("/_/endpointbis");
    assertFalse(endpointHandler.canHandle(webRequest));
}
Also used : WebRequest(com.enonic.xp.web.WebRequest) Test(org.junit.jupiter.api.Test)

Example 3 with WebRequest

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

the class WebDispatcherServlet method newWebRequest.

private WebRequest newWebRequest(final HttpServletRequest req) throws IOException {
    final WebRequest result = new WebRequest();
    result.setMethod(HttpMethod.valueOf(req.getMethod().toUpperCase()));
    final String rawPath = req.getPathInfo();
    result.setRawPath(rawPath);
    result.setEndpointPath(findEndpointPath(rawPath));
    result.setRawRequest(req);
    result.setContentType(req.getContentType());
    result.setBody(RequestBodyReader.readBody(req));
    result.setScheme(req.getScheme());
    result.setHost(req.getServerName());
    result.setPort(req.getServerPort());
    result.setRemoteAddress(req.getRemoteAddr());
    result.setPath(ServletRequestUrlHelper.getPath(req));
    result.setUrl(ServletRequestUrlHelper.getFullUrl(req));
    setParameters(req, result);
    setHeaders(req, result);
    setCookies(req, result);
    return result;
}
Also used : WebRequest(com.enonic.xp.web.WebRequest)

Example 4 with WebRequest

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

the class ResponseSerializerTest method serializeBodyString.

@Test
public void serializeBodyString() throws Exception {
    final WebRequest req = new WebRequest();
    req.setMethod(HttpMethod.GET);
    final String string_body = "String body";
    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(string_body).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(string_body.getBytes(StandardCharsets.UTF_8));
}
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 5 with WebRequest

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

the class ResponseSerializerTest method serializeBodyResource.

@Test
public void serializeBodyResource() throws Exception {
    final ResourceKey resourceKey = ResourceKey.from("myapp:/site/test/view/body_file.txt");
    final URL resourceUrl = ResponseSerializerTest.class.getResource("body_file.txt");
    final WebRequest req = new WebRequest();
    req.setMethod(HttpMethod.GET);
    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(new UrlResource(resourceKey, resourceUrl)).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(any(byte[].class));
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) UrlResource(com.enonic.xp.resource.UrlResource) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) URL(java.net.URL) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

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