private void setUp(int jsloadTtlSecs) { jsLoadProcessor = new JsLoadProcessor(jsUriManagerMock, jsloadTtlSecs, true); JsProcessorRegistry jsProcessorRegistry = new DefaultJsProcessorRegistry( ImmutableList.<JsProcessor>of(new IfModifiedSinceProcessor()), ImmutableList.<JsProcessor>of(jsLoadProcessor, getJsProcessorMock, new AddOnloadFunctionProcessor()), ImmutableList.<JsProcessor>of()); jsServingPipeline = new DefaultJsServingPipeline(jsProcessorRegistry); servlet.setJsServingPipeline(jsServingPipeline); // TODO: Abstract this out into a helper function associated with Uri class. expect(request.getScheme()).andReturn("http"); expect(request.getServerPort()).andReturn(8080); expect(request.getServerName()).andReturn("localhost"); expect(request.getRequestURI()).andReturn("/gadgets/js"); expect(request.getQueryString()).andReturn(null); }
public boolean process(JsRequest request, JsResponseBuilder builder) throws JsException { JsUri jsUri = request.getJsUri(); // Don't emit the JS itself; instead emit JS loader script that loads // versioned JS. The loader script is much smaller and cacheable for a // configurable amount of time. if (jsUri.isJsload()) { // Require users to specify &onload=. This ensures a reliable continuation // of JS execution. IE asynchronously loads script, before it loads // source-scripted and inlined JS. if (requireOnload && jsUri.getOnload() == null) { throw new JsException(HttpServletResponse.SC_BAD_REQUEST, JSLOAD_ONLOAD_ERROR); } int refresh = getCacheTtlSecs(jsUri); builder.setCacheTtlSecs(refresh); builder.setProxyCacheable(true); doJsload(jsUri, builder); return false; } return true; }
@Provides @Inject @Named("shindig.js.optional-processors") public List<JsProcessor> provideProcessors( AddJslInfoVariableProcessor addJslInfoVariableProcessor, DeferJsProcessor deferJsProcessor, JsLoadProcessor jsLoaderGeneratorProcessor, GetJsContentProcessor getJsContentProcessor, CajaJsSubtractingProcessor cajaJsSubtractingProcessor, ExportJsProcessor exportJsProcessor, SeparatorCommentingProcessor separatorCommentingProcessor, ConfigInjectionProcessor configInjectionProcessor, AddJslLoadedVariableProcessor addJslLoadedVariableProcessor, AddOnloadFunctionProcessor addOnloadFunctionProcessor) { jsLoaderGeneratorProcessor.setUseAsync(true); return ImmutableList.of( addJslInfoVariableProcessor, deferJsProcessor, jsLoaderGeneratorProcessor, getJsContentProcessor, cajaJsSubtractingProcessor, exportJsProcessor, separatorCommentingProcessor, configInjectionProcessor, addJslLoadedVariableProcessor, addOnloadFunctionProcessor); }
@Test public void testSkipsWhenNoJsLoad() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(false); response = control.createMock(JsResponseBuilder.class); control.replay(); assertTrue(processor.process(request, response)); control.verify(); }
/** * @throws JsException */ protected void doJsload(JsUri jsUri, JsResponseBuilder resp) throws JsException { jsUri.setJsload(false); jsUri.setNohint(true); Uri incUri = jsUriManager.makeExternJsUri(jsUri); // Make sure next fetch will get content: incUri = new UriBuilder(incUri).addQueryParameter(UriCommon.Param.JSLOAD.getKey(), "0").toUri(); resp.appendJs(createJsloadScript(incUri), CODE_ID); }
@Test public void testSkipsWhenNoJsLoad() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(false); response = control.createMock(JsResponseBuilder.class); control.replay(); assertTrue(processor.process(request, response)); control.verify(); }
@Test public void testFailsWhenNoOnloadIsSpecified() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(null); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(JsLoadProcessor.JSLOAD_ONLOAD_ERROR, e.getMessage()); } control.verify(); }
private void setUp(int jsloadTtlSecs) { jsLoadProcessor = new JsLoadProcessor(jsUriManagerMock, jsloadTtlSecs, true); JsProcessorRegistry jsProcessorRegistry = new DefaultJsProcessorRegistry( ImmutableList.<JsProcessor>of(new IfModifiedSinceProcessor()), ImmutableList.<JsProcessor>of(jsLoadProcessor, getJsProcessorMock, new AddOnloadFunctionProcessor()), ImmutableList.<JsProcessor>of()); jsServingPipeline = new DefaultJsServingPipeline(jsProcessorRegistry); servlet.setJsServingPipeline(jsServingPipeline); // TODO: Abstract this out into a helper function associated with Uri class. expect(request.getScheme()).andReturn("http"); expect(request.getServerPort()).andReturn(8080); expect(request.getServerName()).andReturn("localhost"); expect(request.getRequestURI()).andReturn("/gadgets/js"); expect(request.getQueryString()).andReturn(null); }
@Test public void testFailsWhenNoOnloadIsSpecified() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(null); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(JsLoadProcessor.JSLOAD_ONLOAD_ERROR, e.getMessage()); } control.verify(); }
@Before public void setUp() { control = EasyMock.createControl(); request = control.createMock(JsRequest.class); jsUriManager = control.createMock(JsUriManager.class); jsUri = control.createMock(JsUri.class); uri = Uri.parse("http://example.org/foo.xml"); response = new JsResponseBuilder(); processor = new JsLoadProcessor(jsUriManager, 1234, true); EasyMock.expect(request.getJsUri()).andReturn(jsUri); }
private void checkGeneratedCode(int expectedTtl) throws JsException { assertFalse(processor.process(request, response)); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); assertEquals(expectedTtl, response.getCacheTtlSecs()); String expectedBody = String.format(JsLoadProcessor.JSLOAD_JS_TPL, uri.toString() + "?jsload=0"); assertEquals(expectedBody, response.build().toJsString()); }
@Before public void setUp() { control = EasyMock.createControl(); request = control.createMock(JsRequest.class); jsUriManager = control.createMock(JsUriManager.class); jsUri = control.createMock(JsUri.class); uri = Uri.parse("http://example.org/foo.xml"); response = new JsResponseBuilder(); processor = new JsLoadProcessor(jsUriManager, 1234, true); EasyMock.expect(request.getJsUri()).andReturn(jsUri); }
private void checkGeneratedCode(int expectedTtl) throws JsException { assertFalse(processor.process(request, response)); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); assertEquals(expectedTtl, response.getCacheTtlSecs()); String expectedBody = String.format(JsLoadProcessor.JSLOAD_JS_TPL, uri.toString() + "?jsload=0"); assertEquals(expectedBody, response.build().toJsString()); }