/** * 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>(); }
@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 testModification() { printer.setSetting("opta", "valuea"); EumScriptTagPrinter copy = printer.clone(); copy.setSetting("opta", "notvaluea"); assertThat(printer.getSettings().get("opta"), equalTo("valuea")); } }
@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())); }