/** * Returns cache control max age from HttpServletResponseRecorder */ private int getCacheControlMaxAge(HttpServletResponseRecorder recorder) { String cacheControl = recorder.getHeader("Cache-Control"); assertTrue(cacheControl != null); String cacheValue = cacheControl.substring(cacheControl.indexOf('=') + 1); return Integer.decode(cacheValue); }
@Test public void testCORSstar() { HttpUtil.setCORSheader(recorder, Collections.singleton("*")); assertEquals(recorder.getHeader(HttpUtil.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER), "*"); }
@Test public void testCORSstar() { HttpUtil.setCORSheader(recorder, Collections.singleton("*")); assertEquals(recorder.getHeader(HttpUtil.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER), "*"); }
@Test public void testSetResponseHeaders() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); replay(); handler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertNotNull("Cache-Control header not set", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeaders() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); replay(); handler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertNotNull("Cache-Control header not set", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void refreshParameter_specified() throws Exception { servlet.setRenderer(renderer); expect(request.getParameter("refresh")).andReturn("1000"); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); control.replay(); servlet.doGet(request, recorder); assertEquals("private,max-age=1000", recorder.getHeader("Cache-Control")); }
@Test public void testCORSnull() { HttpUtil.setCORSheader(recorder, null); assertEquals(recorder.getHeader(HttpUtil.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER), null); }
@Test public void testNullSecurityToken() throws Exception { filter.setAuthenticationHandlers(ImmutableList.<AuthenticationHandler>of(nullStHandler)); filter.doFilter(request, recorder, chain); assertEquals(TEST_AUTH_HEADER, recorder.getHeader(AuthenticationServletFilter.WWW_AUTHENTICATE_HEADER)); }
@Test public void renderWithTtl() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("120"); control.replay(); servlet.doGet(request, recorder); assertEquals("private,max-age=120", recorder.getHeader("Cache-Control")); }
@Test public void renderWithTtl() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("120"); control.replay(); servlet.doGet(request, recorder); assertEquals("private,max-age=120", recorder.getHeader("Cache-Control")); }
@Test public void testSetResponseHeadersForceParam() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("30").anyTimes(); replay(); // not sure why but the following line seems to help this test past deterministically System.out.println("request started at " + HttpUtilTest.testStartTime); handler.setResponseHeaders(request, recorder, results); HttpUtilTest.checkCacheControlHeaders(HttpUtilTest.testStartTime, recorder, 30, false); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeadersForceParam() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("30").anyTimes(); replay(); // not sure why but the following line seems to help this test past deterministically System.out.println("request started at " + HttpUtilTest.testStartTime); MakeRequestHandler.setResponseHeaders(request, recorder, results); HttpUtilTest.checkCacheControlHeaders(HttpUtilTest.testStartTime, recorder, 30, false); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testGetRequestWithRefresh() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("120").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); handler.fetch(request, recorder); HttpRequest httpRequest = requestCapture.getValue(); assertEquals("public,max-age=120", recorder.getHeader("Cache-Control")); assertEquals(120, httpRequest.getCacheTtl()); }
@Test public void testGetRequestWithRefresh() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("120").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); handler.fetch(request, recorder); HttpRequest httpRequest = requestCapture.getValue(); assertEquals("public,max-age=120", recorder.getHeader("Cache-Control")); assertEquals(120, httpRequest.getCacheTtl()); }
@Test public void refreshParameter_default() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); control.replay(); servlet.doGet(request, recorder); assertEquals("private,max-age=300", recorder.getHeader("Cache-Control")); }
@Test public void testSetContentTypeHeader() throws Exception { HttpResponse results = new HttpResponseBuilder() .create(); replay(); handler.setResponseHeaders(request, recorder, results); assertEquals("application/octet-stream", recorder.getHeader("Content-Type")); }
@Test public void testSetContentTypeHeader() throws Exception { HttpResponse results = new HttpResponseBuilder() .create(); replay(); handler.setResponseHeaders(request, recorder, results); assertEquals("application/octet-stream", recorder.getHeader("Content-Type")); }
@Test public void normalResponse() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); control.replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_OK, recorder.getHttpStatusCode()); assertEquals("private,max-age=" + GadgetRenderingServlet.DEFAULT_CACHE_TTL, recorder.getHeader("Cache-Control")); assertEquals("working", recorder.getResponseAsString()); }
@Test public void normalResponse() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); control.replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_OK, recorder.getHttpStatusCode()); assertEquals("private,max-age=" + GadgetRenderingServlet.DEFAULT_CACHE_TTL, recorder.getHeader("Cache-Control")); assertEquals("working", recorder.getResponseAsString()); }
@Test public void errorsPassedThrough() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.error("busted", HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); control.replay(); servlet.doGet(request, recorder); assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, recorder.getHttpStatusCode()); assertNull("Cache-Control header passed where it should not be.", recorder.getHeader("Cache-Control")); assertEquals("busted", recorder.getResponseAsString()); }