URLEncodedUtils { public static String format(final Map<String, Set<String>> parameters, final String encoding) { final StringBuilder result = new StringBuilder(); for (Map.Entry<String, Set<String>> parameter : parameters.entrySet()) { final String encodedName = encode(parameter.getKey(), encoding); final Set<String> values = parameter.getValue(); if (values == null || values.size() == 0) { result.append(encodedName); result.append(NAME_VALUE_SEPARATOR); result.append(""); } else { for (String value : values) { final String encodedValue = value != null ? encode(value, encoding) : ""; if (result.length() > 0) result.append(PARAMETER_SEPARATOR); result.append(encodedName); result.append(NAME_VALUE_SEPARATOR); result.append(encodedValue); } } } return result.toString(); }  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(dataProvider = "uris") public void shouldFormatURI(String url, String expectedName, String[] expectedValues) throws Exception { Map<String, Set<String>> parameters = new HashMap<>(); parameters.put(expectedName, new LinkedHashSet<>(Arrays.asList(expectedValues))); String actual = URLEncodedUtils.format(parameters, "UTF-8"); if ("Name3".equals(url)) { assertEquals(actual, url + "="); } else { assertEquals(actual, url); } }
@Test public void shouldFormatURIWithMultipleParams() throws Exception { Map<String, Set<String>> parameters = new LinkedHashMap<>(); parameters.put("Name5", new LinkedHashSet<>(Arrays.asList(new String[]{"aaa"}))); parameters.put("Name6", new LinkedHashSet<>(Arrays.asList(new String[]{"bbb"}))); String actual = URLEncodedUtils.format(parameters, "UTF-8"); assertEquals(actual, "Name5=aaa&Name6=bbb"); }
@Test public void shouldFormatUTF8() { String ru_hello = constructString(RUSSIAN_HELLO); String ch_hello = constructString(SWISS_GERMAN_HELLO); Map<String, Set<String>> parameters = new LinkedHashMap<>(); parameters.put("russian", new LinkedHashSet<>(Arrays.asList(new String[]{ru_hello}))); parameters.put("swiss", new LinkedHashSet<>(Arrays.asList(new String[]{ch_hello}))); String actual = URLEncodedUtils.format(parameters, "UTF-8"); assertEquals(actual, "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"); }
@Test public void shouldFormatURIWithMultipleParams() throws Exception { Map<String, Set<String>> parameters = new LinkedHashMap<>(); parameters.put("Name5", new LinkedHashSet<>(Arrays.asList(new String[] {"aaa"}))); parameters.put("Name6", new LinkedHashSet<>(Arrays.asList(new String[] {"bbb"}))); String actual = URLEncodedUtils.format(parameters, "UTF-8"); assertEquals(actual, "Name5=aaa&Name6=bbb"); }
@Test public void shouldFormatUTF8() { String ru_hello = constructString(RUSSIAN_HELLO); String ch_hello = constructString(SWISS_GERMAN_HELLO); Map<String, Set<String>> parameters = new LinkedHashMap<>(); parameters.put("russian", new LinkedHashSet<>(Arrays.asList(new String[] {ru_hello}))); parameters.put("swiss", new LinkedHashSet<>(Arrays.asList(new String[] {ch_hello}))); String actual = URLEncodedUtils.format(parameters, "UTF-8"); assertEquals( actual, "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"); }