@Test public void testThreeConcat() throws Exception { String results = addComment(SCRT1, URL1.toString()) + addComment(SCRT2,URL2.toString()) + addComment(SCRT3, URL3.toString()); runConcat(sequentialExecutor, results, null, URL1, URL2, URL3); }
@Test public void testThreeAsJsonConcat() throws Exception { String results = "_js={\r\n" + addVar(URL1.toString(), SCRT1_ESCAPED) + addVar(URL2.toString(), SCRT2_ESCAPED) + addLastVar(URL3.toString(), SCRT3_ESCAPED) + "};\r\n"; runConcat(sequentialExecutor, results, "_js", URL1, URL2, URL3); }
@Test public void testBadJsonVarConcat() throws Exception { expectRequestWithUris(Lists.<Uri>newArrayList(), "bad code;"); servlet.doGet(request, recorder); verify(); String results = "/* ---- Error 400, Bad json variable name bad code; ---- */\r\n"; assertEquals(results, recorder.getResponseAsString()); assertEquals(400, recorder.getHttpStatusCode()); }
@Test public void testDefaultCacheTtlCacheHeaderMissing() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); final Uri URL5 = Uri.parse("http://example.org/5.js"); expectGetAndReturnData(URL4, ""); expectGetAndReturnData(URL5, ""); expectRequestWithUris(Lists.newArrayList(URL4, URL5)); servlet.doGet(request, recorder); verify(); int cacheValue = getCacheControlMaxAge(recorder); // HttpResponse.defaultTtl is in msec, division by 1000 is required to convert into sec. assertEquals((int) (HttpResponse.defaultTtl / 1000), cacheValue, 10); }
@Test public void testConcat404() throws Exception { String url = "http://nobodyhome.com/"; HttpRequest req = new HttpRequest(Uri.parse(url)); HttpResponse resp = new HttpResponseBuilder().setHttpStatusCode(404).create(); expect(pipeline.execute(req)).andReturn(resp).anyTimes(); expectRequestWithUris(Lists.newArrayList(URL1, Uri.parse(url))); servlet.doGet(request, recorder); verify(); String results = addComment(SCRT1, URL1.toString()) + addErrComment(url,404); assertEquals(results, recorder.getResponseAsString()); assertEquals(200, recorder.getHttpStatusCode()); }
@Test public void testAsJsonConcatException() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); HttpRequest req = new HttpRequest(URL4); expect(pipeline.execute(req)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT)).anyTimes(); expectRequestWithUris(Lists.newArrayList(URL1, URL4), "_js"); servlet.doGet(request, recorder); verify(); String results = "_js={\r\n" + addLastVar(URL1.toString(), SCRT1_ESCAPED) + addConcatErrComment(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, URL4.toString()) + "};\r\n"; assertEquals(results, recorder.getResponseAsString()); assertEquals(400, recorder.getHttpStatusCode()); }
@Test public void testMinimumCacheTtl() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); final Uri URL5 = Uri.parse("http://example.org/5.js"); final Uri URL6 = Uri.parse("http://example.org/6.js"); final Integer cacheTtl4 = Integer.MAX_VALUE; final Integer cacheTtl5 = 100000; final Integer cacheTtl6 = Integer.MAX_VALUE; expectGetAndSetCacheTtl(URL4, cacheTtl4); expectGetAndSetCacheTtl(URL5, cacheTtl5); expectGetAndSetCacheTtl(URL6, cacheTtl6); expectRequestWithUris(Lists.newArrayList(URL4, URL5, URL6)); // Run the servlet servlet.doGet(request, recorder); verify(); int cacheValue = getCacheControlMaxAge(recorder); assertEquals(cacheTtl5, cacheValue, 10); }
@Test public void testAsJsonConcatException() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); HttpRequest req = new HttpRequest(URL4); expect(pipeline.execute(req)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT)).anyTimes(); expectRequestWithUris(Lists.newArrayList(URL1, URL4), "_js"); servlet.doGet(request, recorder); verify(); String results = "_js={\r\n" + addVar(URL1.toString(), SCRT1_ESCAPED) + "FAILED_TO_RETRIEVE_CONTENT concat(http://example.org/4.js) null};\r\n"; assertEquals(results, recorder.getResponseAsString()); assertEquals(400, recorder.getHttpStatusCode()); }
@Test public void testAsJsonConcat404() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); HttpRequest req = new HttpRequest(URL4); HttpResponse resp = new HttpResponseBuilder().setHttpStatusCode(404).create(); expect(pipeline.execute(req)).andReturn(resp).anyTimes(); String results = "_js={\r\n" + addLastVar(URL1.toString(), SCRT1_ESCAPED) + "/* ---- Error 404 (http://example.org/4.js) ---- */\r\n" + "};\r\n"; runConcat(sequentialExecutor, results, "_js", URL1, URL4); }
private void expectRequestWithUris(List<Uri> uris, String tok) { expect(request.getScheme()).andReturn("http").anyTimes(); expect(request.getServerPort()).andReturn(80).anyTimes(); expect(request.getServerName()).andReturn("example.com").anyTimes(); expect(request.getRequestURI()).andReturn("/path").anyTimes(); expect(request.getQueryString()).andReturn("").anyTimes(); replay(); Uri uri = new UriBuilder(request).toUri(); uriManager.expect(uri, uris, tok); }
@Before public void setUp() throws Exception { servlet.setRequestPipeline(pipeline); uriManager = new TestConcatUriManager(); servlet.setConcatUriManager(uriManager); expect(request.getHeader("Host")).andReturn(REQUEST_DOMAIN).anyTimes(); expect(lockedDomainService.isSafeForOpenProxy(REQUEST_DOMAIN)) .andReturn(true).anyTimes(); expectGetAndReturnData(URL1, SCRT1); expectGetAndReturnData(URL2, SCRT2); expectGetAndReturnData(URL3, SCRT3); }
private void expectRequestWithUris(List<Uri> uris) { expectRequestWithUris(uris, null); }
@Test public void testConcat404() throws Exception { String url = "http://nobodyhome.com/"; HttpRequest req = new HttpRequest(Uri.parse(url)); HttpResponse resp = new HttpResponseBuilder().setHttpStatusCode(404).create(); expect(pipeline.execute(req)).andReturn(resp).anyTimes(); expectRequestWithUris(Lists.newArrayList(URL1, Uri.parse(url))); servlet.doGet(request, recorder); verify(); String results = addComment(SCRT1, URL1.toString()) + addErrComment(url,404); assertEquals(results, recorder.getResponseAsString()); assertEquals(200, recorder.getHttpStatusCode()); }
@Test public void testAsJsonConcatBadException() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); HttpRequest req = new HttpRequest(URL4); expect(pipeline.execute(req)).andThrow( new GadgetException(GadgetException.Code.HTML_PARSE_ERROR)).anyTimes(); String results = "_js={\r\n" + addLastVar(URL1.toString(), SCRT1_ESCAPED) + addConcatErrComment(GadgetException.Code.HTML_PARSE_ERROR, URL4.toString()) + "};\r\n"; expectRequestWithUris(Lists.newArrayList(URL1, URL4), "_js"); // Run the servlet servlet.doGet(request, recorder); verify(); assertEquals(results, recorder.getResponseAsString()); assertEquals(400, recorder.getHttpStatusCode()); }
@Test public void testDefaultCacheTtlCacheHeaderMissing() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); final Uri URL5 = Uri.parse("http://example.org/5.js"); expectGetAndReturnData(URL4, ""); expectGetAndReturnData(URL5, ""); expectRequestWithUris(Lists.newArrayList(URL4, URL5)); servlet.doGet(request, recorder); verify(); int cacheValue = getCacheControlMaxAge(recorder); // HttpResponse.defaultTtl is in msec, division by 1000 is required to convert into sec. assertEquals((int) (HttpResponse.defaultTtl / 1000), cacheValue, 10); }
@Test public void testMinimumCacheTtl() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); final Uri URL5 = Uri.parse("http://example.org/5.js"); final Uri URL6 = Uri.parse("http://example.org/6.js"); final Integer cacheTtl4 = Integer.MAX_VALUE; final Integer cacheTtl5 = 100000; final Integer cacheTtl6 = Integer.MAX_VALUE; expectGetAndSetCacheTtl(URL4, cacheTtl4); expectGetAndSetCacheTtl(URL5, cacheTtl5); expectGetAndSetCacheTtl(URL6, cacheTtl6); expectRequestWithUris(Lists.newArrayList(URL4, URL5, URL6)); // Run the servlet servlet.doGet(request, recorder); verify(); int cacheValue = getCacheControlMaxAge(recorder); assertEquals(cacheTtl5, cacheValue, 10); }
@Test public void testAsJsonConcatBadException() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); HttpRequest req = new HttpRequest(URL4); expect(pipeline.execute(req)).andThrow( new GadgetException(GadgetException.Code.HTML_PARSE_ERROR)).anyTimes(); String results = "_js={\r\n" + addVar(URL1.toString(), SCRT1_ESCAPED) + "HTML_PARSE_ERROR concat(http://example.org/4.js) null};\r\n"; expectRequestWithUris(Lists.newArrayList(URL1, URL4), "_js"); // Run the servlet servlet.doGet(request, recorder); verify(); assertEquals(results, recorder.getResponseAsString()); assertEquals(400, recorder.getHttpStatusCode()); }
@Test public void testAsJsonConcat404() throws Exception { final Uri URL4 = Uri.parse("http://example.org/4.js"); HttpRequest req = new HttpRequest(URL4); HttpResponse resp = new HttpResponseBuilder().setHttpStatusCode(404).create(); expect(pipeline.execute(req)).andReturn(resp).anyTimes(); String results = "_js={\r\n" + addLastVar(URL1.toString(), SCRT1_ESCAPED) + "/* ---- Error 404 (http://example.org/4.js) ---- */\r\n" + "};\r\n"; runConcat(sequentialExecutor, results, "_js", URL1, URL4); }