URLEncodedUtils { public static Map<String, Set<String>> parse(final URI uri, final String encoding) { Map<String, Set<String>> result = Collections.emptyMap(); final String query = uri.getRawQuery(); if (query != null && query.length() > 0) { result = new HashMap<>(); parse(result, new Scanner(query), encoding, true); } return result; }  static Map<String, Set<String>> parse(final URI uri, final String encoding); static Map<String, Set<String>> parse(final URI uri); static Map<String, Set<String>> parse(final URI uri, boolean decodeQueryParam); static String format(final Map<String, Set<String>> parameters, final String encoding);  }
@Test public void shouldBeAbleToParseEmptyURI() throws Exception { Map<String, Set<String>> parameters = parse("", null); assertTrue(parameters.isEmpty()); }
@Test(dataProvider = "uris") public void shouldParseURI(String url, String expectedName, String[] expectedValues) throws Exception { assertNameValuePair(parse(url, "UTF-8"), expectedName, expectedValues); }
@Test public void shouldParseURIWithMultipleParams() throws Exception { Map<String, Set<String>> parameters = parse("Name5=aaa&Name6=bbb", null); assertNameValuePair(parameters, "Name5", "aaa"); assertNameValuePair(parameters, "Name6", "bbb"); }
@Test public void shouldParseUTF8() { String ru_hello = constructString(RUSSIAN_HELLO); String ch_hello = constructString(SWISS_GERMAN_HELLO); Map<String, Set<String>> parameters = parse("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" + "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", "UTF-8"); assertNameValuePair(parameters, "russian", ru_hello); assertNameValuePair(parameters, "swiss", ch_hello); }
@Test public void shouldParseWithoutDecoding() { String ru_hello = "%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82"; String ch_hello = "Gr%C3%BCezi_z%C3%A4m%C3%A4"; Map<String, Set<String>> parameters = URLEncodedUtils.parse( URI.create("http: assertNameValuePair(parameters, "russian", ru_hello); assertNameValuePair(parameters, "swiss", ch_hello); }
@Test public void shouldParseUTF8() { String ru_hello = constructString(RUSSIAN_HELLO); String ch_hello = constructString(SWISS_GERMAN_HELLO); Map<String, Set<String>> parameters = parse( "russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" + "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", "UTF-8"); assertNameValuePair(parameters, "russian", ru_hello); assertNameValuePair(parameters, "swiss", ch_hello); }
@Test public void shouldParseWithoutDecoding() { String ru_hello = "%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82"; String ch_hello = "Gr%C3%BCezi_z%C3%A4m%C3%A4"; Map<String, Set<String>> parameters = URLEncodedUtils.parse( URI.create("http: false); assertNameValuePair(parameters, "russian", ru_hello); assertNameValuePair(parameters, "swiss", ch_hello); }