Menu

#385 Bug in URL-decoding of GET-Request-Parameters

closed
None
5
2007-12-28
2007-04-23
No

HttpUnit doesn't decode parameter values of GetMethodWebRequests appropriately if HttpUnitOptions.setDefaultCharacterSet() is set to something different than ISO-8859-1. For example, URL-(En/De-)coding of request parameters using UTF-8 doesn't work. Thus, I can't test servlets which are supposed to run in a container (say Tomcat) which is configured using UTF-8-URIEncoding.

What I found HttpUnit does, it encodes parameter values using UTF-8 (which I activated by HttpUnitOptions.setDefaultCharacterSet("utf-8");). But invoking HttpServletRequest#getParameter(...) from inside the servlet (running in httpunit) returns parameter values decoded using ISO-8859-1. This
behaviour causes my servlet tests to fail when I test them using ServletRunner.

Here are two test cases (taken from HttpServletRequestTest and modified for my purposes):

public void testGetMethodRequestParametersEncodedWithDefaultCharacterSet_Hebrew() throws Exception {
String hebrewValue = "\u05d0\u05d1\u05d2\u05d3";
String paramString = "param1=red&param2=%E0%E1%E2%E3"; // use iso-8859-8 to encode the data, then string is URL encoded
HttpUnitOptions.setDefaultCharacterSet("ISO-8859-8");
WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" );
wr.setParameter("param1", "red");
wr.setParameter("param2", hebrewValue);
ServletUnitHttpRequest request = new ServletUnitHttpRequest(NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), new byte[0]);
assertEquals( "param1 value", "red", request.getParameter( "param1") );
assertEquals( "param2 value", hebrewValue, request.getParameter( "param2") );
}

public void testGetMethodRequestParametersEncodedWithDefaultCharacterSet_UTF8() throws Exception {
String hebrewValue = "\u05d0\u05d1\u05d2\u05d3";
String paramString = "param1=red&param2=%E0%E1%E2%E3"; // use utf-8 to encode the data, then string is URL encoded
HttpUnitOptions.setDefaultCharacterSet("UTF-8");
WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" );
wr.setParameter("param1", "red");
wr.setParameter("param2", hebrewValue);
ServletUnitHttpRequest request = new ServletUnitHttpRequest(NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), new byte[0]);
assertEquals( "param1 value", "red", request.getParameter( "param1") );
assertEquals( "param2 value", hebrewValue, request.getParameter( "param2") );
}

Discussion

  • Wolfgang Fahl

    Wolfgang Fahl - 2007-12-28

    Logged In: YES
    user_id=1220573
    Originator: NO

    Thanks for the patch - the issue is fixed - I'll set it like this when I get tracker rights from Russell

     
  • Wolfgang Fahl

    Wolfgang Fahl - 2007-12-28

    Logged In: YES
    user_id=1220573
    Originator: NO

    state changed now

     
  • Wolfgang Fahl

    Wolfgang Fahl - 2007-12-28
    • assigned_to: nobody --> wolfgang_fahl
    • status: open --> closed
     

Log in to post a comment.