### Question:
JaasHttpCallbackHandlerFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { capture(request, response); chain.doFilter(request, response); return; } finally { release(); } }  @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); static final JaasAuthenticationCallbackHandler CALLBACK_HANDLER; }### Answer:
@Test public void testExistingHeader() throws IOException, ServletException { Mockito.when(request.getHeaders(anyString())).thenReturn( new Vector<>(Arrays.asList("value1")).elements() ); FilterChain test = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { try { HttpHeaderCallback callback = new HttpHeaderCallback("header"); JaasHttpCallbackHandlerFilter.CALLBACK_HANDLER.handle(callback, null); assertThat(callback.isMultiValued()).isFalse(); assertThat(callback.getValue()).hasValue("value1"); assertThat(callback.getValues()).hasSize(1).contains("value1"); } catch (UnsupportedCallbackException e) { fail("Failure on handle() call", e); } } }; filter.doFilter(request, response, test); }
@Test public void testExistingHeaders() throws IOException, ServletException { Mockito.when(request.getHeaders(anyString())).thenReturn( new Vector<>(Arrays.asList("value1", "value2")).elements() ); FilterChain test = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { try { HttpHeaderCallback callback = new HttpHeaderCallback("header"); JaasHttpCallbackHandlerFilter.CALLBACK_HANDLER.handle(callback, null); assertThat(callback.isMultiValued()).isTrue(); assertThat(callback.getValue()).hasValue("value1,value2"); assertThat(callback.getValues()).hasSize(2).contains("value1", "value2"); } catch (UnsupportedCallbackException e) { fail("Failure on handle() call", e); } } }; filter.doFilter(request, response, test); }
@Test public void testMissingHeader() throws IOException, ServletException { Mockito.when(request.getHeaders(anyString())).thenReturn( new Vector<String>().elements() ); FilterChain test = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { try { HttpHeaderCallback callback = new HttpHeaderCallback("header"); JaasHttpCallbackHandlerFilter.CALLBACK_HANDLER.handle(callback, null); assertThat(callback.isMultiValued()).isFalse(); assertThat(callback.getValue()).isNotPresent(); assertThat(callback.getValues()).isEmpty();; } catch (UnsupportedCallbackException e) { fail("Failure on handle() call", e); } } }; filter.doFilter(request, response, test); }
@Test public void testExistingCookie() throws IOException, ServletException { Mockito.when(request.getCookies()).thenReturn( new Cookie[] { new Cookie("cookie", "value") } ); FilterChain test = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { try { HttpCookieCallback callback = new HttpCookieCallback("cookie"); JaasHttpCallbackHandlerFilter.CALLBACK_HANDLER.handle(callback, null); assertThat(callback.getCookie()) .containsInstanceOf(Cookie.class) .hasValueSatisfying(cookie -> assertThat(cookie.getValue()).isEqualTo("value")); } catch (UnsupportedCallbackException e) { fail("Failure on handle() call", e); } } }; filter.doFilter(request, response, test); }
@Test public void testMissiongCookie() throws IOException, ServletException { Mockito.when(request.getCookies()).thenReturn( new Cookie[] { new Cookie("bogus", "") } ); FilterChain test = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { try { HttpCookieCallback callback = new HttpCookieCallback("cookie"); JaasHttpCallbackHandlerFilter.CALLBACK_HANDLER.handle(callback, null); assertThat(callback.getCookie()).isEmpty(); } catch (UnsupportedCallbackException e) { fail("Failure on handle() call", e); } } }; filter.doFilter(request, response, test); }