Search in sources :

Example 56 with WebResponse

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

the class AssetHandlerTest method testOptions.

@Test
public void testOptions() throws Exception {
    addResource("demo:/assets/css/main.css");
    this.request.setMethod(HttpMethod.OPTIONS);
    final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.OK, res.getStatus());
    assertEquals("GET,HEAD,OPTIONS", res.getHeaders().get("Allow"));
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 57 with WebResponse

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

the class ImageHandlerTest method imageWithOrientation.

@Test
void imageWithOrientation() throws Exception {
    setupContent();
    when(this.mediaInfoService.getImageOrientation(any(ByteSource.class))).thenReturn(ImageOrientation.LeftBottom);
    this.request.setEndpointPath("/_/image/123456/scale-100-100/image-name.jpg");
    this.request.getParams().put("filter", "sepia()");
    this.request.getParams().put("quality", "75");
    this.request.getParams().put("background", "0x0");
    final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.OK, res.getStatus());
    assertEquals(MediaType.PNG, res.getContentType());
    assertTrue(res.getBody() instanceof ByteSource);
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) ByteSource(com.google.common.io.ByteSource) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 58 with WebResponse

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

the class ImageHandlerTest method cacheHeader_fingerprint_missmatch.

@Test
void cacheHeader_fingerprint_missmatch() throws Exception {
    mockCachableContent();
    this.request.setEndpointPath("/_/image/123456:654321/scale-100-100/image-name.jpg.png");
    final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertThat(res.getHeaders()).doesNotContainKey("Cache-Control");
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 59 with WebResponse

use of com.enonic.xp.web.WebResponse 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 60 with WebResponse

use of com.enonic.xp.web.WebResponse 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)

Aggregations

WebResponse (com.enonic.xp.web.WebResponse)63 Test (org.junit.jupiter.api.Test)53 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)32 PortalResponse (com.enonic.xp.portal.PortalResponse)11 WebRequest (com.enonic.xp.web.WebRequest)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 ByteSource (com.google.common.io.ByteSource)6 Cookie (javax.servlet.http.Cookie)6 ResourceKey (com.enonic.xp.resource.ResourceKey)5 WebException (com.enonic.xp.web.WebException)5 ServletOutputStream (javax.servlet.ServletOutputStream)5 PortalRequest (com.enonic.xp.portal.PortalRequest)4 Content (com.enonic.xp.content.Content)2 PropertyTree (com.enonic.xp.data.PropertyTree)2 PageUrlParams (com.enonic.xp.portal.url.PageUrlParams)2 MockResource (com.enonic.xp.resource.MockResource)2 Resource (com.enonic.xp.resource.Resource)2 ControllerMappingDescriptor (com.enonic.xp.site.mapping.ControllerMappingDescriptor)2 Trace (com.enonic.xp.trace.Trace)2 VirtualHost (com.enonic.xp.web.vhost.VirtualHost)2