Search in sources :

Example 6 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 7 with WebRequest

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

the class EndpointHandlerTest method testFindRestPath.

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

Example 8 with WebRequest

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

the class ResponseSerializerTest method serializeBodyByteSource.

@Test
public void serializeBodyByteSource() throws Exception {
    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(Resources.asByteSource(ResponseSerializerTest.class.getResource("body_file.txt"))).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, times(0)).setContentLength(anyInt());
    verify(servletOutputStream).write(any(byte[].class), eq(0), eq(11));
}
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 9 with WebRequest

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

the class ResponseSerializerTest method serializeHeadRequest.

@Test
public void serializeHeadRequest() throws Exception {
    final WebRequest req = new WebRequest();
    req.setMethod(HttpMethod.HEAD);
    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);
    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(httpResponse, times(0)).getOutputStream();
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 10 with WebRequest

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

the class ResponseSerializerTest method serializeBodyJsonMap.

@Test
public void serializeBodyJsonMap() throws Exception {
    final Map<String, Object> jsonObject = new HashMap<>();
    jsonObject.put("key1", "value1");
    jsonObject.put("key2", true);
    jsonObject.put("key3", 42);
    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(jsonObject).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(39);
    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) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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