### Question:
BaseTagRemoverRewriter implements GadgetRewriter, ResponseRewriter { public void rewrite(Gadget gadget, MutableContent mc) { Document doc = mc.getDocument(); NodeList list = doc.getElementsByTagName("base"); for (int i = 0; i < list.getLength(); i++) { Element baseElement = (Element) list.item(i); baseElement.getParentNode().removeChild(baseElement); if (baseElement.hasAttribute("href") && logger.isLoggable(Level.FINE)) { logger.fine("Removing base tag pointing to: " + baseElement.getAttribute("href") + " for gadget: " + gadget.getContext().getUrl().toString()); } } mc.documentChanged(); }  void rewrite(Gadget gadget, MutableContent mc); void rewrite(HttpRequest request, HttpResponseBuilder response);  }### Answer:
@Test public void testRemoveBaseTag() throws Exception { String content = "<html><head><base href='http: + "</head><body>" + "<img src='/img1.png'>" + "</body></html>"; String expected = "<html><head>" + "</head><body>" + "<img src=\"/img1.png\">" + "</body></html>"; HttpRequest req = new HttpRequest(Uri.parse("http: HttpResponse resp = new HttpResponseBuilder() .setHttpStatusCode(200) .setHeader("Content-Type", "text/html") .setResponse(content.getBytes()) .create(); HttpResponseBuilder builder = new HttpResponseBuilder(parser, resp); rewriter.rewrite(req, builder); assertEquals(StringUtils.deleteWhitespace(expected), StringUtils.deleteWhitespace(builder.getContent())); }
@Test public void testNoBaseTag() throws Exception { String content = "<html><head>" + "</head><body>" + "<img src='/img1.png'>" + "</body></html>"; String expected = "<html><head>" + "</head><body>" + "<img src=\"/img1.png\">" + "</body></html>"; HttpRequest req = new HttpRequest(Uri.parse("http: HttpResponse resp = new HttpResponseBuilder() .setHttpStatusCode(200) .setHeader("Content-Type", "text/html") .setResponse(content.getBytes()) .create(); HttpResponseBuilder builder = new HttpResponseBuilder(parser, resp); rewriter.rewrite(req, builder); assertEquals(StringUtils.deleteWhitespace(expected), StringUtils.deleteWhitespace(builder.getContent())); }
@Test public void testContentTypeString() throws Exception { String content = "Hello world. My name is gagan<html><head>" + "<base href='http: + "<img src='/img1.png'>" + "</body></html>"; String expected = "Hello world. My name is gagan<html><head>" + "<base href='http: + "<img src='/img1.png'>" + "</body></html>"; HttpRequest req = new HttpRequest(Uri.parse("http: HttpResponse resp = new HttpResponseBuilder() .setHttpStatusCode(200) .setHeader("Content-Type", "text/plain") .setResponse(content.getBytes()) .create(); HttpResponseBuilder builder = new HttpResponseBuilder(parser, resp); rewriter.rewrite(req, builder); assertEquals(StringUtils.deleteWhitespace(expected), StringUtils.deleteWhitespace(builder.getContent())); }
@Test public void testContentTypeXml() throws Exception { String content = "Hello world. My name is gagan<html><head>" + "<base href='http: + "<img src='/img1.png'>" + "</body></html>"; String expected = "Hello world. My name is gagan<html><head>" + "<base href='http: + "<img src='/img1.png'>" + "</body></html>"; HttpRequest req = new HttpRequest(Uri.parse("http: HttpResponse resp = new HttpResponseBuilder() .setHttpStatusCode(200) .setHeader("Content-Type", "text/xml") .setResponse(content.getBytes()) .create(); HttpResponseBuilder builder = new HttpResponseBuilder(parser, resp); rewriter.rewrite(req, builder); assertEquals(StringUtils.deleteWhitespace(expected), StringUtils.deleteWhitespace(builder.getContent())); }