ClientSpanJavaScriptServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (servletPlugin.isClientSpanCollectionEnabled()) { resp.setHeader(ETAG, javaScriptEtag); resp.setHeader(CACHE_CONTROL, getCacheControlMaxAge()); if (isInClientCache(req)) { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } else { resp.setContentType(MIME_APPLICATION_JAVASCRIPT); resp.getWriter().write(javaScript); } } else { log.debug("Client span collection is disabled"); resp.sendError(HttpServletResponse.SC_NOT_FOUND); } } ClientSpanJavaScriptServlet();  ClientSpanJavaScriptServlet(ServletPlugin servletPlugin); void rebuildJavaScriptAndEtag();  }
@Test public void testDoGet_simple_Cache1Minute() throws Exception { ClientSpanJavaScriptServlet clientSpanJavaScriptServlet = new ClientSpanJavaScriptServlet(mockServletPluginWithCacheTime(1)); final MockHttpServletRequest request = new MockHttpServletRequest(); final MockHttpServletResponse response = new MockHttpServletResponse(); clientSpanJavaScriptServlet.doGet(request, response); assertThat(response.getContentAsString()).contains("EumObject"); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader(CACHE_CONTROL)).isEqualTo("max-age=60"); }
@Test public void testDoGet_simple_DoNotCache() throws Exception { ClientSpanJavaScriptServlet clientSpanJavaScriptServlet = new ClientSpanJavaScriptServlet(mockServletPluginWithCacheTime(0)); final MockHttpServletRequest request = new MockHttpServletRequest(); final MockHttpServletResponse response = new MockHttpServletResponse(); clientSpanJavaScriptServlet.doGet(request, response); assertThat(response.getContentAsString()).contains("EumObject"); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader(CACHE_CONTROL)).isEqualTo("no-cache"); }
@Test public void testDoGet_conditionalGet() throws Exception { ClientSpanJavaScriptServlet clientSpanJavaScriptServlet = new ClientSpanJavaScriptServlet(mockServletPluginWithCacheTime(0)); final MockHttpServletResponse response = new MockHttpServletResponse(); clientSpanJavaScriptServlet.doGet(new MockHttpServletRequest(), response); final String etag = response.getHeader("etag"); assertThat(etag).isNotNull(); final MockHttpServletRequest conditionalGetRequest = new MockHttpServletRequest(); conditionalGetRequest.addHeader("if-none-match", etag); final MockHttpServletResponse conditionalGetResponse = new MockHttpServletResponse(); clientSpanJavaScriptServlet.doGet(conditionalGetRequest, conditionalGetResponse); assertThat(conditionalGetResponse.getStatus()).isEqualTo(304); assertThat(conditionalGetResponse.getContentLength()).isZero(); }
@Test public void testDoGet_returns404IfEumDisabled() throws Exception { ServletPlugin servletPlugin = mockServletPluginWithCacheTime(0); when(servletPlugin.isClientSpanCollectionEnabled()).thenReturn(false); ClientSpanJavaScriptServlet clientSpanJavaScriptServlet = new ClientSpanJavaScriptServlet(servletPlugin); final MockHttpServletResponse response = new MockHttpServletResponse(); clientSpanJavaScriptServlet.doGet(new MockHttpServletRequest(), response); assertThat(response.getStatus()).isEqualTo(404); assertThat(response.getContentAsString()).isEmpty(); }