class 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));
        }
    }

}

class HttpRequestTest {

    @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"));
    }

}