HttpRequest { public String getEncodedParameter(String name) { Matcher matcher = Pattern.compile(Pattern.quote(name) + "=([^&#]+)").matcher(queryString); if (matcher.find()) { return matcher.group(1); } else { return Util.urlEncoder(getParameter(name)); } } @Deprecated  HttpRequest(String requestURL);  HttpRequest(String requestURL, String queryString); @Deprecated  HttpRequest(String requestURL, Map<String, List<String>> parameters);  HttpRequest(String requestURL, Map<String, List<String>> parameters, String queryString); HttpRequest addParameter(String name, String value); HttpRequest removeParameter(String name); String getRequestURL(); String getParameter(String name); List<String> getParameters(String name); Map<String, List<String>> getParameters(); String getEncodedParameter(String name); String getEncodedParameter(String name, String defaultValue); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static final Map<String, List<String>> EMPTY_PARAMETERS; }
@Test public void testGetEncodedParameter_returnsExactAsGivenInQueryString() throws Exception { final String url = "url"; final String name = "name"; String encodedValue1 = NaiveUrlEncoder.encode("do not alter!"); final String queryString = name + "=" + encodedValue1; final HttpRequest request = new HttpRequest(url, queryString); assertThat(request.getEncodedParameter(name), equalTo(encodedValue1)); }
@Test public void testGetEncodedParameter_handlesMultipleValuesOnQueryString() throws Exception { final String url = "url"; final String queryString = "k1=v1&k2=v2&k3=v3"; final Map<String, List<String>> parametersMap = new HashMap<>(); final HttpRequest request = new HttpRequest(url, parametersMap, queryString); assertThat(request.getEncodedParameter("k1"), equalTo("v1")); assertThat(request.getEncodedParameter("k2"), equalTo("v2")); assertThat(request.getEncodedParameter("k3"), equalTo("v3")); }
@Test public void testGetEncodedParameter_stopsAtUrlFragment() throws Exception { final String url = "url"; final String queryString = "first=&foo=bar#ignore"; final HttpRequest request = new HttpRequest(url, queryString); assertThat(request.getEncodedParameter("foo"), equalTo("bar")); }
@Test public void testGetEncodedParameter_withDefault_usesDefaultWhenParameterMissing() throws Exception { final String url = "url"; final String foobar = "foo/bar!"; final HttpRequest request = new HttpRequest(url, (String)null); assertThat(request.getEncodedParameter("missing", foobar), equalTo(Util.urlEncoder(foobar))); }