### Question:
ContextAwareRegistry implements ResponseRewriterRegistry { public HttpResponse rewriteHttpResponse(HttpRequest req, HttpResponse resp, Gadget gadget) throws RewritingException { HttpResponseBuilder builder = new HttpResponseBuilder(htmlParser, resp); for (ResponseRewriter rewriter : getResponseRewriters(req.getContainer())) { rewriter.rewrite(req, builder, gadget); } return builder.create(); } ContextAwareRegistry(GadgetHtmlParser htmlParser,
                              RewriteFlow rewriteFlow,
                              Provider<Map<RewritePath, Provider<List<ResponseRewriter>>>>
                                  rewritePathToRewriterList); HttpResponse rewriteHttpResponse(HttpRequest req, HttpResponse resp,
          Gadget gadget);  }### Answer:
@Test public void testRewriteResponse() throws Exception { final Map<RewritePath, Provider<List<ResponseRewriter>>> rewritePathToList = Maps.newHashMap(); List<ResponseRewriter> list = ImmutableList.<ResponseRewriter>of( new TestRewriter("helo"), new TestRewriter("buffalo")); List<ResponseRewriter> emptyList = ImmutableList.of(); addBindingForRewritePath(TEST_CONTAINER, RewriteFlow.ACCELERATE, Providers.of(list), rewritePathToList); addBindingForRewritePath(TEST_CONTAINER, RewriteFlow.DEFAULT, Providers.of(emptyList), rewritePathToList); contextAwareRegistry = new ContextAwareRegistry( null, RewriteFlow.ACCELERATE, Providers.of(rewritePathToList)); HttpRequest req = new HttpRequest(Uri.parse("http: req.setContainer(TEST_CONTAINER); HttpResponseBuilder builder = new HttpResponseBuilder(); HttpResponse resp = contextAwareRegistry.rewriteHttpResponse( req, builder.create(), null); List<String> headers = Lists.newArrayList(resp.getHeaders("helloo")); assertEquals(2, headers.size()); assertEquals("helo", headers.get(0)); assertEquals("buffalo", headers.get(1)); }
@Test public void testRewriteResponseGadget() throws Exception { Gadget gadget = mockGadget(); control.replay(); final Map<RewritePath, Provider<List<ResponseRewriter>>> rewritePathToList = Maps.newHashMap(); List<ResponseRewriter> list = ImmutableList.<ResponseRewriter>of( new TestRewriter("helo"), new TestRewriter("buffalo")); List<ResponseRewriter> emptyList = ImmutableList.of(); addBindingForRewritePath(TEST_CONTAINER, RewriteFlow.ACCELERATE, Providers.of(list), rewritePathToList); addBindingForRewritePath(TEST_CONTAINER, RewriteFlow.DEFAULT, Providers.of(emptyList), rewritePathToList); contextAwareRegistry = new ContextAwareRegistry( null, RewriteFlow.ACCELERATE, Providers.of(rewritePathToList)); HttpRequest req = new HttpRequest(Uri.parse("http: req.setContainer(TEST_CONTAINER); HttpResponseBuilder builder = new HttpResponseBuilder(); HttpResponse resp = contextAwareRegistry.rewriteHttpResponse( req, builder.create(), gadget); List<String> headers = Lists.newArrayList(resp.getHeaders("helloo")); assertEquals(2, headers.size()); assertEquals("helo", headers.get(0)); assertEquals("buffalo", headers.get(1)); headers = Lists.newArrayList(resp.getHeaders("gadget")); assertEquals(2, headers.size()); assertEquals("helo", headers.get(0)); assertEquals("buffalo", headers.get(1)); }