users@jersey.java.net

Re: [Jersey] Jersey and UriInfo.getQueryParameters()

From: Wilhelmsen Tor Iver <TorIverW_at_arrive.no>
Date: Tue, 7 Apr 2009 10:53:59 +0200

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