@Override public boolean getDebug() { return "1".equalsIgnoreCase(getParameter(Param.DEBUG.getKey())); } });
@Override public boolean getDebug() { return "1".equalsIgnoreCase(getParameter(Param.DEBUG.getKey())); } });
public static boolean isHtml(HttpRequest request, HttpResponseBuilder original) { String mimeType = getMimeType(request, original); return mimeType != null && (mimeType.contains("html")) && maybeAcceptHtml(request.getParam(UriCommon.Param.HTML_TAG_CONTEXT.getKey())); }
@Test public void renderWithBadTtl() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); expect(request.getParameter(Param.REFRESH.getKey())).andReturn(""); control.replay(); servlet.doGet(request, recorder); assertEquals("private,max-age=300", recorder.getHeader("Cache-Control")); }
@Test public void validateOldStyleTypeUri() { DefaultConcatUriManager manager = makeManager("host.com", "/path", null, null); ConcatUriManager.ConcatUri validated = manager.process( Uri.parse("http://host.com/path?" + Param.CONTAINER.getKey() + '=' + CONTAINER + "&1=http://legit.com/1.dat&" + Param.TYPE.getKey() + "=NOTATYPE&rewriteMime=text/css")); assertEquals(UriStatus.VALID_UNVERSIONED, validated.getStatus()); assertEquals(ConcatUriManager.Type.CSS, validated.getType()); }
@Test public void validateInvalidChildUri() { DefaultConcatUriManager manager = makeManager("host.com", "/path", null, null); ConcatUriManager.ConcatUri validated = manager.process( Uri.parse("http://host.com/path?" + Param.CONTAINER.getKey() + '=' + CONTAINER + "&1=!!!")); assertEquals(UriStatus.BAD_URI, validated.getStatus()); }
@Test public void validateHostMismatchStrict() { DefaultConcatUriManager manager = makeManager("host.com", "/path", null, null); manager.setUseStrictParsing(true); ConcatUriManager.ConcatUri validated = manager.process(Uri.parse("http://another.com/path?" + Param.CONTAINER.getKey() + '=' + CONTAINER + "&type=css")); assertEquals(UriStatus.BAD_URI, validated.getStatus()); }
@Test public void typeJsMissingSplitTokenConfig() throws Exception { Gadget gadget = mockGadget(false, false); DefaultConcatUriManager manager = makeManager("host.com", "/foo", null, null); List<List<Uri>> resourceUris = ImmutableList.<List<Uri>>of(ImmutableList.of(RESOURCE_1)); List<ConcatData> concatUris = manager.make(fromList(gadget, resourceUris, ConcatUriManager.Type.JS), false); assertEquals(1, concatUris.size()); assertNull(concatUris.get(0).getUri().getQueryParameter(Param.JSON.getKey())); }
@Test public void mismatchedHostNonStrict() throws Exception { String host = "host.com"; String path = "/proxy/path"; DefaultProxyUriManager manager = makeManager("foo" + host, path, null); Uri testUri = new UriBuilder().setAuthority(host).setPath(path) .addQueryParameter(Param.URL.getKey(), "http://foo.com") .addQueryParameter(Param.CONTAINER.getKey(), CONTAINER).toUri(); manager.process(testUri); }
@Test(expected = GadgetException.class) public void mismatchedHostStrict() throws Exception { String host = "host.com"; String path = "/proxy/path"; DefaultProxyUriManager manager = makeManager("foo" + host, path, null); manager.setUseStrictParsing(true); Uri testUri = new UriBuilder().setAuthority(host).setPath(path) .addQueryParameter(Param.URL.getKey(), "http://foo.com").toUri(); manager.process(testUri); }
@Test public void testGetContainerWithContainer() { expect(request.getParameter(Param.CONTAINER.getKey())).andReturn("bar"); replay(); assertEquals("bar", MakeRequestHandler.getContainer(request)); }
@Test public void testSetResponseHeadersForceParamInvalid() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("foo").anyTimes(); replay(); try { handler.setResponseHeaders(request, recorder, results); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_PARAMETER, e.getCode()); } }
@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 validateNullTypeUri() { DefaultConcatUriManager manager = makeManager("host.com", "/path", null, null); ConcatUriManager.ConcatUri validated = manager.process( Uri.parse("http://host.com/path?" + Param.CONTAINER.getKey() + '=' + CONTAINER + "&1=http://legit.com/1.dat")); assertEquals(UriStatus.BAD_URI, validated.getStatus()); }
@Test public void badUriValidatingUri() throws Exception { Uri uri = new UriBuilder().addQueryParameter(Param.URL.getKey(), "^': bad:").toUri(); TestDefaultIframeUriManager manager = makeManager(false, false); UriStatus status = manager.validateRenderingUri(uri); assertEquals(UriStatus.BAD_URI, status); }
@Test(expected = GadgetException.class) public void invalidUrlParamQuery() throws Exception { // Only test query style, since chained style should be impossible. String host = "host.com"; String path = "/proxy/path"; DefaultProxyUriManager manager = makeManager(host, path, null); Uri testUri = new UriBuilder().setAuthority(host).setPath(path) .addQueryParameter(Param.CONTAINER.getKey(), CONTAINER) .addQueryParameter(Param.URL.getKey(), "!^!").toUri(); manager.process(testUri); }
@Test(expected = GadgetException.class) public void mismatchedHostStrict() throws Exception { String host = "host.com"; String path = "/proxy/path"; DefaultProxyUriManager manager = makeManager("foo" + host, path, null); manager.setUseStrictParsing(true); Uri testUri = new UriBuilder().setAuthority(host).setPath(path) .addQueryParameter(Param.URL.getKey(), "http://foo.com").toUri(); manager.process(testUri); }
@Test public void renderWithBadTtl() throws Exception { servlet.setRenderer(renderer); expect(renderer.render(isA(GadgetContext.class))) .andReturn(RenderingResults.ok("working")); expect(request.getParameter(Param.REFRESH.getKey())).andReturn(""); control.replay(); servlet.doGet(request, recorder); assertEquals("private,max-age=300", recorder.getHeader("Cache-Control")); }
@Test public void testIsHtmlReturnsTrueIfHtmlAcceptingTagContext() throws Exception { HttpRequest req = new HttpRequest(Uri.parse("http://www.example.org/")); req.setParam(UriCommon.Param.HTML_TAG_CONTEXT.getKey(), "link"); HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Content-Type", "text/html"); assertTrue(RewriterUtils.isHtml(req, builder)); req.setParam(UriCommon.Param.HTML_TAG_CONTEXT.getKey(), "iframe"); assertTrue(RewriterUtils.isHtml(req, builder)); } }
/** * Extracts the container name from the request. */ @SuppressWarnings("deprecation") protected static String getContainer(HttpServletRequest request) { String container = getParameter(request, Param.CONTAINER.getKey(), null); if (container == null) { container = getParameter(request, Param.SYND.getKey(), null); } return container != null ? container : ContainerConfig.DEFAULT_CONTAINER; }