### Question:
CajaResponseRewriter implements ResponseRewriter { public void rewrite(HttpRequest req, HttpResponseBuilder resp, Gadget gadget) throws RewritingException { if (!req.isCajaRequested()) { return; } if (!RewriterUtils.isJavascript(req, resp)) { resp.setContent(""); resp.setHttpStatusCode(HttpResponse.SC_BAD_REQUEST); return; } boolean passed = false; MessageQueue mq = new SimpleMessageQueue(); MessageContext mc = new MessageContext(); Uri contextUri = req.getUri(); InputSource is = new InputSource(contextUri.toJavaUri()); PluginMeta pluginMeta = new PluginMeta( proxyFetcher(req, contextUri), proxyUriPolicy(req)); PluginCompiler compiler = new PluginCompiler(BuildInfo.getInstance(), pluginMeta, mq); compiler.setMessageContext(mc); try { StringReader strReader = new StringReader(resp.getContent()); CharProducer cp = CharProducer.Factory.create(strReader, is); JsTokenQueue tq = new JsTokenQueue(new JsLexer(cp), is); ParseTreeNode input = new Parser(tq, mq).parse(); tq.expectEmpty(); compiler.addInput(AncestorChain.instance(input).node, contextUri.toJavaUri()); } catch (ParseException e) { resp.setContent(""); return; } try { if (RewriterUtils.isJavascript(req, resp)) { compiler.setGoals( compiler.getGoals().without(PipelineMaker.HTML_SAFE_STATIC)); } passed = compiler.run(); CajoledModule outputJs = passed ? compiler.getJavascript() : null; StringBuilder jsOut = new StringBuilder(); TokenConsumer printer; if ("1".equals(req.getParam("debug"))) { printer = new JsPrettyPrinter(new Concatenator(jsOut)); } else { printer = new JsMinimalPrinter(new Concatenator(jsOut)); } RenderContext renderContext = new RenderContext(printer).withEmbeddable(true); if (outputJs != null) { outputJs.render(renderContext); } renderContext.getOut().noMoreTokens(); resp.setContent(jsOut.toString()); } finally { if (!passed) { resp.setContent(""); } } } @Inject  CajaResponseRewriter(RequestPipeline requestPipeline); void rewrite(HttpRequest req, HttpResponseBuilder resp, Gadget gadget);  }### Answer:
@Test public void testJs() throws Exception { Gadget gadget = mockGadget(); control.replay(); HttpRequest req = new HttpRequest(CONTENT_URI); req.setRewriteMimeType("text/javascript"); req.setCajaRequested(true); HttpResponse response = new HttpResponseBuilder().setResponseString("var a;").create(); String sanitized = "___.di(IMPORTS___,'a');"; assertTrue(rewrite(req, response).contains(sanitized)); assertTrue(rewrite(req, response, gadget).contains(sanitized)); }
@Test public void testJsWithoutCaja() throws Exception { Gadget gadget = mockGadget(); control.replay(); HttpRequest req = new HttpRequest(CONTENT_URI); req.setRewriteMimeType("text/javascript"); req.setCajaRequested(false); HttpResponse response = new HttpResponseBuilder().setResponseString("var a;").create(); String sanitized = "var a;"; assertTrue(rewrite(req, response).contains(sanitized)); assertTrue(rewrite(req, response, gadget).contains(sanitized)); }
@Test public void testNonJs() throws Exception { Gadget gadget = mockGadget(); control.replay(); HttpRequest req = new HttpRequest(CONTENT_URI); req.setRewriteMimeType("text/html"); req.setCajaRequested(true); HttpResponse response = new HttpResponseBuilder().setResponseString("<html></html>").create(); assertEquals("", rewrite(req, response)); assertEquals("", rewrite(req, response, gadget)); }