UrlUtils { public static Map<String, List<String>> getQueryParameters(URL url) throws UnsupportedEncodingException { Map<String, List<String>> params = new HashMap<>(); String query = url.getQuery(); if (query != null) { for (String param : query.split("&")) { String pair[] = param.split("="); String key = URLDecoder.decode(pair[0], "UTF-8"); String value = null; if (pair.length > 1) { value = URLDecoder.decode(pair[1], "UTF-8"); } List<String> values = params.get(key); if (values == null) { values = new ArrayList<>(); params.put(key, values); } values.add(value); } } return params; } private  UrlUtils(); static Map<String, List<String>> getQueryParameters(URL url);  }
@Test public void shouldExtractParametersWithoutValue() throws Exception { Map<String, List<String>> params = UrlUtils.getQueryParameters(new URL("http: assertTrue(params.containsKey("v")); assertNull(params.get("v").iterator().next()); }
@Test public void shouldExtractParametersWithMultipleValues() throws Exception { Map<String, List<String>> expectedParams = new HashMap<>(); List<String> v = new LinkedList<>(); v.add("123"); v.add("qwe"); v.add("www"); expectedParams.put("v", v); Map<String, List<String>> params = UrlUtils.getQueryParameters(new URL("http: assertEquals(params, expectedParams); }
@Test public void shouldExtractParametersWithMultipleValuesDividedAnotherParameters() throws Exception { Map<String, List<String>> expectedParams = new HashMap<>(); List<String> v = new LinkedList<>(); v.add("123"); v.add("qwe"); v.add("www"); List<String> par = new LinkedList<>(); par.add("test"); expectedParams.put("v", v); expectedParams.put("par", par); Map<String, List<String>> params = UrlUtils.getQueryParameters(new URL("http: assertEquals(params, expectedParams); }
@Test public void shouldIgnoreSlashAtTheEndOfPath() throws Exception { Map<String, List<String>> expectedParams = new HashMap<>(); List<String> v = new LinkedList<>(); v.add("123"); v.add("qwe"); v.add("www"); List<String> par = new LinkedList<>(); par.add("test"); expectedParams.put("v", v); expectedParams.put("par", par); Map<String, List<String>> params = UrlUtils.getQueryParameters(new URL("http: assertEquals(params, expectedParams); }
@Test public void shouldExtractEncodedParameters() throws Exception { Map<String, List<String>> expectedParams = new HashMap<>(); List<String> vcsurl = new LinkedList<>(); vcsurl.add("http: expectedParams.put("vcsurl", vcsurl); Map<String, List<String>> params = UrlUtils.getQueryParameters(new URL("http: "http: assertEquals(params, expectedParams); }