HttpHeadersFilterRequestWrapper extends HttpServletRequestWrapper { @Override public Enumeration<String> getHeaderNames() { List<String> headerNames = Collections.list(super.getHeaderNames()); headerNames.removeIf(header -> shouldFilter(header)); return Collections.enumeration(headerNames); } HttpHeadersFilterRequestWrapper(List<String> badHeaders, HttpServletRequest request); @Override String getHeader(String name); @Override Enumeration<String> getHeaders(String name); @Override Enumeration<String> getHeaderNames();  }
@Test public void null_filter_list() { request = new HttpHeadersFilterRequestWrapper(null, mock); List<String> actual = Collections.list(request.getHeaderNames()); List<String> wanted = new ArrayList<>(BAD_HEADERS); wanted.add("Other-header"); assertThat(actual, containsInAnyOrder(wanted.toArray())); }
@Test public void filtered_available_headers() { request = new HttpHeadersFilterRequestWrapper(BAD_HEADERS, mock); List<String> actual = Collections.list(request.getHeaderNames()); List<String> wanted = Collections.singletonList("Other-header"); assertThat(actual, containsInAnyOrder(wanted.toArray())); }
@Test public void non_filtered_available_headers() { request = new HttpHeadersFilterRequestWrapper(Collections.emptyList(), mock); List<String> actual = Collections.list(request.getHeaderNames()); List<String> wanted = new ArrayList<>(BAD_HEADERS); wanted.add("Other-header"); assertThat(actual, containsInAnyOrder(wanted.toArray())); }