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 testSkipsWhenNoOnloadAndWithHintSpecified() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); response = control.createMock(JsResponseBuilder.class); control.replay(); assertTrue(processor.process(request, response)); control.verify(); }
public boolean process(JsRequest request, JsResponseBuilder builder) throws JsException { JsUri jsUri = request.getJsUri(); // Add onload handler to add callback function. String onloadStr = jsUri.getOnload(); if (onloadStr != null) { if (!ONLOAD_FN_PATTERN.matcher(onloadStr).matches()) { throw new JsException(HttpServletResponse.SC_BAD_REQUEST, ONLOAD_FUNCTION_NAME_ERROR); } builder.appendJs(createOnloadScript(onloadStr), ONLOAD_CODE_ID); } else if (jsUri.isNohint()) { // "Second-stage" JS, which may have had a callback set by loader. // This type of JS doesn't create a hint, but does attempt to use one. builder.appendJs(JSL_CALLBACK_JS, JSL_CODE_ID, true); } return true; }
@Test public void testSkipsWhenNoOnloadAndWithHintSpecified() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); response = control.createMock(JsResponseBuilder.class); control.replay(); assertTrue(processor.process(request, response)); 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 testWithoutHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(true); control.replay(); assertTrue(processor.process(request, response)); assertEquals(AddOnloadFunctionProcessor.JSL_CALLBACK_JS, response.build().toJsString()); control.verify(); }
@Before public void setUp() { control = EasyMock.createControl(); jsUri = control.createMock(JsUri.class); request = control.createMock(JsRequest.class); response = new JsResponseBuilder(); processor = new AddOnloadFunctionProcessor(); EasyMock.expect(request.getJsUri()).andReturn(jsUri); }
@Test public void testWithHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); control.replay(); assertTrue(processor.process(request, response)); assertEquals("", response.build().toJsString()); control.verify(); } }
@Before public void setUp() { control = EasyMock.createControl(); jsUri = control.createMock(JsUri.class); request = control.createMock(JsRequest.class); response = new JsResponseBuilder(); processor = new AddOnloadFunctionProcessor(); EasyMock.expect(request.getJsUri()).andReturn(jsUri); }
@Test public void testWithHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); control.replay(); assertTrue(processor.process(request, response)); assertEquals("", response.build().toJsString()); control.verify(); } }
@Test public void testWithoutHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(true); control.replay(); assertTrue(processor.process(request, response)); assertEquals(AddOnloadFunctionProcessor.JSL_CALLBACK_JS, response.build().toJsString()); control.verify(); }
@Test public void testGeneratesCallbackCode() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(ONLOAD_FUNCTION); control.replay(); assertTrue(processor.process(request, response)); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); String expectedBody = String.format(AddOnloadFunctionProcessor.ONLOAD_JS_TPL, ONLOAD_FUNCTION); assertEquals(expectedBody, response.build().toJsString()); control.verify(); }
@Test public void testGeneratesCallbackCode() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(ONLOAD_FUNCTION); control.replay(); assertTrue(processor.process(request, response)); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); String expectedBody = String.format(AddOnloadFunctionProcessor.ONLOAD_JS_TPL, ONLOAD_FUNCTION); assertEquals(expectedBody, response.build().toJsString()); control.verify(); }
@Test public void testFailsWithInvalidFunctionName() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn("!!%%!!%%"); 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(AddOnloadFunctionProcessor.ONLOAD_FUNCTION_NAME_ERROR, e.getMessage()); } control.verify(); }
@Test public void testFailsWithInvalidFunctionName() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn("!!%%!!%%"); 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(AddOnloadFunctionProcessor.ONLOAD_FUNCTION_NAME_ERROR, e.getMessage()); } control.verify(); }