scriptTags.setSetting("eumManagementServer", beaconPath.toString()); scriptTags.setSetting("relevancyThreshold", String.valueOf(endUserMonitoringConfig.getRelevancyThreshold())); scriptTags.setSetting("allowListenerInstrumentation", String.valueOf(endUserMonitoringConfig.isListenerInstrumentationAllowed()));
@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()); } }
@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 + "\"")); }
@Test public void testDataValid() { printer.setSetting("opta", "valuea"); EumScriptTagPrinter copy = printer.clone(); assertThat(copy.getSettings(), equalTo(printer.getSettings())); assertThat(copy.getScriptSourceURL(), equalTo(printer.getScriptSourceURL())); }
/** * 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); } }