XTrackMDCPutFilter extends AbstractMDCPutFilter { @Override protected String getMDCValue(HttpServletRequest request, HttpServletResponse response) { String xTrack = request.getHeader(attributeName); if (xTrack == null) { xTrack = createXTrack(); } String cutXTrack = cutValue(xTrack); response.setHeader(attributeName, cutXTrack); request.setAttribute(attributeName, cutXTrack); return cutXTrack; }  void setAttributeName(String attributeName);  }
@Test public void testGetMDCValue_default_attributeName() throws ServletException { xTrackMDCPutFilter.init(mockFilterConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); String xTrack = xTrackMDCPutFilter.getMDCValue(request, response); assertThat(xTrack, is(notNullValue())); assertThat(xTrack.matches("^[a-f0-9]{32}$"), is(true)); assertThat(response.getHeader("X-Track"), is(xTrack)); assertThat((String) request.getAttribute("X-Track"), is(xTrack)); }
@Test public void testGetMDCValue_changed_by_initParam() throws ServletException { mockFilterConfig.addInitParameter("attributeName", "X-Hoge"); xTrackMDCPutFilter.init(mockFilterConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); String xTrack = xTrackMDCPutFilter.getMDCValue(request, response); assertThat(xTrack, is(notNullValue())); assertThat(xTrack.matches("^[a-f0-9]{32}$"), is(true)); assertThat(response.getHeader("X-Hoge"), is(xTrack)); assertThat((String) request.getAttribute("X-Hoge"), is(xTrack)); }
@Test public void testGetMDCValue_default_attributeName_set_in_http_request() throws ServletException { xTrackMDCPutFilter.init(mockFilterConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("X-Track", "hoge"); String xTrack = xTrackMDCPutFilter.getMDCValue(request, response); assertThat(xTrack, is(notNullValue())); assertThat(xTrack, is("hoge")); assertThat(response.getHeader("X-Track"), is("hoge")); assertThat((String) request.getAttribute("X-Track"), is("hoge")); }
@Test public void testGetMDCValue_changed_by_initParam_set_in_http_request() throws ServletException { mockFilterConfig.addInitParameter("attributeName", "X-Hoge"); xTrackMDCPutFilter.init(mockFilterConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("X-Hoge", "12345678901234567890123456789012"); String xTrack = xTrackMDCPutFilter.getMDCValue(request, response); assertThat(xTrack, is(notNullValue())); assertThat(xTrack, is("12345678901234567890123456789012")); assertThat(response.getHeader("X-Hoge"), is( "12345678901234567890123456789012")); assertThat((String) request.getAttribute("X-Hoge"), is( "12345678901234567890123456789012")); }
@Test public void testGetMDCValue_default_attributeName_set_in_http_request_too_long_length() throws ServletException { xTrackMDCPutFilter.init(mockFilterConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("X-Track", "12345678901234567890123456789012345678901234567890"); String xTrack = xTrackMDCPutFilter.getMDCValue(request, response); assertThat(xTrack, is(notNullValue())); assertThat(xTrack, is("12345678901234567890123456789012")); assertThat(response.getHeader("X-Track"), is( "12345678901234567890123456789012")); assertThat((String) request.getAttribute("X-Track"), is( "12345678901234567890123456789012")); }