@Test public void testDataValid() { printer.setSetting("opta", "valuea"); EumScriptTagPrinter copy = printer.clone(); assertThat(copy.getSettings(), equalTo(printer.getSettings())); assertThat(copy.getScriptSourceURL(), equalTo(printer.getScriptSourceURL())); }
/** * {@inheritDoc} */ @Override protected EumScriptTagPrinter clone() { // NOPMD EumScriptTagPrinter result = new EumScriptTagPrinter(); result.scriptSourceURL = this.getScriptSourceURL(); result.settings = new HashMap<String, String>(this.getSettings()); return result; }
@Test public void testModification() { printer.setSetting("opta", "valuea"); EumScriptTagPrinter copy = printer.clone(); copy.setSetting("opta", "notvaluea"); assertThat(printer.getSettings().get("opta"), equalTo("valuea")); } }
@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()); } }
jsAgentURLRegEx = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); scriptTags = new EumScriptTagPrinter(); scriptSrcURL.append(".js"); scriptTags.setScriptSourceURL(scriptSrcURL.toString()); scriptTags.setSetting("eumManagementServer", beaconPath.toString()); scriptTags.setSetting("relevancyThreshold", String.valueOf(endUserMonitoringConfig.getRelevancyThreshold())); scriptTags.setSetting("allowListenerInstrumentation", String.valueOf(endUserMonitoringConfig.isListenerInstrumentationAllowed()));
@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 testTraceCorrelationInfoSet() throws IOException { when(tagPrinter.clone()).thenReturn(tagPrinter); SpanContextImpl context = mock(SpanContextImpl.class); when(context.getTraceId()).thenReturn(1234L); String traceID = ConversionUtils.toHexString(1234L); when(tracer.getCurrentContext()).thenReturn(context); ArgumentCaptor<String> traceIdJSSetting = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Cookie> cookieCapture = ArgumentCaptor.forClass(Cookie.class); respWrapper.getWriter(); verify(tagPrinter).setSetting(eq(JSAgentBuilder.TRACEID_CORRELATION_SETTING), traceIdJSSetting.capture()); verify(dummyResponse, atLeast(1)).addCookie(cookieCapture.capture()); boolean cookieFound = false; for (Cookie cookie : cookieCapture.getAllValues()) { if (cookie.getName().equals(JSAgentBuilder.TRACEID_CORRELATION_COOKIE_PREFIX + traceID)) { cookieFound = true; } } assertThat("Trace Correlation cookie not set", cookieFound); assertThat(traceIdJSSetting.getValue(), equalTo("\"" + traceID + "\"")); }
/** * 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; }
/** * Constructor. After the Construction, a proxy has to be generated using a * {@link IRuntimeLinker}. * * @param requestObject * the javax.servlet.http.HTTPServletResponse which triggered this response. * @param responseObject * the javax.servlet.http.HTTPServletResponse to wrap. * @param tracer * the tracer used for request correlation * @param tagToInject * the tag to inject */ public TagInjectionResponseWrapper(Object requestObject, Object responseObject, TracerImpl tracer, EumScriptTagPrinter tagToInject) { // copy the tag printer we further modify it, for example with request correlation // information this.tagToInject = tagToInject.clone(); this.tracer = tracer; wrappedResponse = WHttpServletResponse.wrap(responseObject); wrappedRequest = WHttpServletRequest.wrap(requestObject); contentLengthHeaderModifications = new ArrayList<Runnable>(); }
/** * Sets the information for correlating the initial request. * */ private void setTraceCorrelationInformation() { SpanContextImpl context = tracer.getCurrentContext(); if (context != null) { String traceID = ConversionUtils.toHexString(context.getTraceId()); // add it to the JS settings tagToInject.setSetting(JSAgentBuilder.TRACEID_CORRELATION_SETTING, "\"" + traceID + "\""); // add a cookie with short time-to-live to detect caching at the client side ClassLoader cl = wrappedResponse.getWrappedElement().getClass().getClassLoader(); Object cookie = WCookie.newInstance(cl, JSAgentBuilder.TRACEID_CORRELATION_COOKIE_PREFIX + traceID, "1"); WCookie wrappedCookie = WCookie.wrap(cookie); wrappedCookie.setPath("/"); // short time to live just in case our script is not running wrappedCookie.setMaxAge(2 * 60); wrappedResponse.addCookie(cookie); } }
/** * 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; }