HostnameCollapser extends AbstractPathCollapser implements StringCollapser { @Override public <T> Map<String, Collection<T>> collapse(Map<String, Collection<T>> input) { Map<PathName, Collection<T>> inputMap = new HashMap<PathName, Collection<T>>(input.size()); for (Map.Entry<String, Collection<T>> hostname : input.entrySet()) { if (hostname.getKey().equals("'none'") && input.size() > 1) { continue; } inputMap.put(toPathName(hostname.getKey()), hostname.getValue()); } Map<PathName, Collection<T>> outputMap = collapsePaths(inputMap); Map<String, Collection<T>> output = new HashMap<String, Collection<T>>(outputMap.size()); for (Map.Entry<PathName, Collection<T>> pathName : outputMap.entrySet()) { if (pathName.getKey().getPathComponents().length == 1 && pathName.getKey().getPathComponents()[0].charAt(0) == '\'') { output.put(pathName.getKey().getPathComponents()[0], pathName.getValue()); } else { String[] schemeAndPort = pathName.getKey().getNonPathComponents(); StringBuilder hostname = new StringBuilder(); if (schemeAndPort != null && schemeAndPort[0] != null) { hostname.append(schemeAndPort[0]).append(": } hostname.append(StringUtil.join(".", reverse(pathName.getKey().getPathComponents()))); if (schemeAndPort != null && schemeAndPort[1] != null) { hostname.append(":").append(schemeAndPort[1]); } output.put(hostname.toString(), pathName.getValue()); } } return output; } HostnameCollapser(int collapseThreshold); @Override boolean pathNameMatches(PathName matcher, PathName matchee); @Override Map<String, Collection<T>> collapse(Map<String, Collection<T>> input); boolean pathNameMatches(String matcher, String matchee);  }
@Test public void testRespectSchemeAndPort() { final HostnameCollapser collapser = new HostnameCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "http: "http: "https: "http: ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "http: "http: "https: )); }
@Test public void testCollapseNoneHostname() { final HostnameCollapser collapser = new HostnameCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "http: "'none'" ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "http: )); output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "'none'" ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "'none'" )); }
@Test public void testCollapseQuotedHostnames() { final HostnameCollapser collapser = new HostnameCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "example.com", "*.net", "'self'", "'unsafe-inline'", "'none'" ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "'self'", "'unsafe-inline'", "*.net", "example.com" )); }
@Test public void testBasicCollapse() { final HostnameCollapser collapser = new HostnameCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "a.b.c.example.com", "d.b.c.example.com", "a.b.c.d.example.com", "b.b.c.d.example.com", "x.y.c.d.example.com", "y.y.c.d.example.com" ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "*.b.c.example.com", "*.c.d.example.com" )); }