@BeforeMethod public void initMocks() throws IOException { ServletOutputStream stream = new ServletOutputStream() { @Override public void write(int b) throws IOException { streamResult.write(b); } }; streamResult = new ByteArrayOutputStream(); printerResult = new StringWriter(); PrintWriter pw = new PrintWriter(printerResult); when(tagPrinter.printTags()).thenReturn(TEST_TAG); when(tagPrinter.clone()).thenReturn(tagPrinter); when(dummyResponse.getCharacterEncoding()).thenReturn(CHARACTER_ENCODING); when(dummyResponse.getWriter()).thenReturn(pw); when(dummyResponse.getOutputStream()).thenReturn(stream); }
@Test public void test() { printer.setScriptSourceURL("my_script_url with space"); printer.setSetting("opt_a", "value_a"); printer.setSetting("opt_b", "\"value b\""); String printedTags = printer.printTags(); assertThat("Url not correctly set in script tag", Pattern.compile("<script[^>]*src(\\s*)=(\\s*)\"my_script_url with space\"").matcher(printedTags).find()); assertThat("opt_a not correctly set as option", Pattern.compile("window\\.inspectIT_settings\\s*=\\s*\\{\\s*(.+\\s*,\\s*)?opt_a\\s*:\\s*value_a\\s*(,|\\})").matcher(printedTags).find()); assertThat("opt_b not correctly set as option", Pattern.compile("window\\.inspectIT_settings\\s*=\\s*\\{\\s*(.+\\s*,\\s*)?opt_b\\s*:\\s*\"value b\"\\s*(,|\\})").matcher(printedTags).find()); } }
/** * Proxy for {@link javax.servlet.ServletResponse#getWriter()}. * * @return the instrumented writer * @throws IOException * if an exception getting the original writer occurs. */ @ProxyMethod public PrintWriter getWriter() throws IOException { commitHeaderData(); if (wrappedWriter == null) { PrintWriter originalWriter = wrappedResponse.getWriter(); // avoid rewrapping or unnecessary wrapping if (isNonHtmlContentTypeSet() || (originalWriter instanceof TagInjectionPrintWriter)) { wrappedWriter = originalWriter; } else { wrappedWriter = new TagInjectionPrintWriter(originalWriter, tagToInject.printTags()); } } return wrappedWriter; }
/** * Proxy for {@link javax.servlet.ServletResponse#getOutputStream()}. * * @return the instrumented stream * @throws IOException * if an exception getting the original stream occurs. */ @ProxyMethod(returnType = "javax.servlet.ServletOutputStream") public OutputStream getOutputStream() throws IOException { commitHeaderData(); if (wrappedStream == null) { OutputStream originalStream = wrappedResponse.getOutputStream(); // avoid rewrapping or unnecessary wrapping if (isNonHtmlContentTypeSet() || linker.isProxyInstance(originalStream, TagInjectionOutputStream.class)) { wrappedStream = originalStream; } else { TagInjectionOutputStream resultStr = new TagInjectionOutputStream(originalStream, tagToInject.printTags()); resultStr.setEncoding(wrappedResponse.getCharacterEncoding()); ClassLoader cl = wrappedResponse.getWrappedElement().getClass().getClassLoader(); wrappedStream = (OutputStream) linker.createProxy(TagInjectionOutputStream.class, resultStr, cl); if (wrappedStream == null) { // fallback to the normal stream if it can not be linked wrappedStream = originalStream; } } } return wrappedStream; }