HeaderUtils { public static Map<String, List<String>> expandHeaders(Map<String, String> headers) { return headers.entrySet().stream() .filter(h -> h.getKey() != null) .filter(h -> h.getValue() != null) .collect(Collectors.collectingAndThen( Collectors.toMap( Map.Entry::getKey, HeaderUtils::expandHeaders), Collections::unmodifiableMap)); } private  HeaderUtils(); static Map<String, String> flattenHeaders(Map<String, List<String>> headers); static Map<String, String> flattenHeaders(Map<String, List<String>> headers,
			Predicate<String> headerNameFilter); static Map<String, List<String>> expandHeaders(Map<String, String> headers);  }
@Test public void expandHeaders_NullValueGiven_ShouldFilterOutHeader() { Map<String, String> flatHeaders = new HashMap<>(); flatHeaders.put("a_k", "a_v"); flatHeaders.put("b_k", null); flatHeaders.put("c_k", "c_v"); Map<String, List<String>> expandedHeaders = expandHeaders(flatHeaders); assertEquals(ImmutableMap.of("a_k", singletonList("a_v"), "c_k", singletonList("c_v")), expandedHeaders); }
@Test public void expandHeaders_CommaSeparatedValuesGiven_ShouldNotSpread() { Map<String, String> flatHeaders = new HashMap<>(); flatHeaders.put("a_k", "a_v0,a_v1"); Map<String, List<String>> expandedHeaders = expandHeaders(flatHeaders); assertEquals(ImmutableMap.of("a_k", singletonList("a_v0,a_v1")), expandedHeaders); }
@Test public void exandHeaders_AnyGiven_ShouldReturnImmutableMap() { assertThrows(UnsupportedOperationException.class, () -> expandHeaders(new HashMap<>()).put("k", new ArrayList<>())); }
@Test public void exandHeaders_AnyGiven_ShouldReturnImmutableList() { assertThrows(UnsupportedOperationException.class, () -> expandHeaders(ImmutableMap.of("k", "v0")).get("k").add("v1")); }
@Test public void expandHeaders_NullKeyGiven_ShouldFilterOutHeader() { Map<String, String> flatHeaders = new HashMap<>(); flatHeaders.put("a_k", "a_v"); flatHeaders.put(null, "b_v"); flatHeaders.put("c_k", "c_v"); Map<String, List<String>> expandedHeaders = expandHeaders(flatHeaders); assertEquals(ImmutableMap.of("a_k", singletonList("a_v"), "c_k", singletonList("c_v")), expandedHeaders); }
@Test public void expandHeaders_NoHeadersGiven_ShouldExpandToEmptyMap() { Map<String, String> flatHeaders = new HashMap<>(); Map<String, List<String>> expandedHeaders = expandHeaders(flatHeaders); assertEquals(ImmutableMap.of(), expandedHeaders); }