On Apr 7, 2009, at 10:53 AM, Wilhelmsen Tor Iver wrote:
> To revisit this issue, I worked around the HttpUnit/ServletUnit
> behavior
> regarding stripping of query parameters in get requests so that my
> tests
> work, though I will tro to look into Paul's suggested alternatives as
> well.
>
> The code below should perhaps be rewritten to support multiple query
> parameters, but it works for my particular tests for now.
>
> import java.net.URI;
> import java.util.regex.Matcher;
> import java.util.regex.Pattern;
>
> import com.meterware.httpunit.GetMethodWebRequest;
>
> public class UnitTestUtil {
>
> public static final GetMethodWebRequest
> bypassBugInServletUnit(String complete) {
> URI uri = URI.create(complete);
> int p = uri.getPort();
> // This is the culprit that strips away query...
> GetMethodWebRequest ret = new
> GetMethodWebRequest(complete);
> // ... and forces us to add them afterwards
> String query = uri.getQuery();
> if (query != null) {
> Pattern pat =
> Pattern.compile("([^=]+)=([^&]+)");
> Matcher m = pat.matcher(query);
> while (m.find()) {
> ret.setParameter(m.group(1),
> m.group(2));
> }
> }
> return ret;
> }
> }
>
Great, glad you worked around it.
You could change your code to utilize the following Jersey feature:
MultivaluedMap<String, String> queryParams =
UriComponent.decodeQuery(uri, false);
For example, sometimes you can have a query parameter with no value
and your regex will will not match that case.
Paul.