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¶m2=%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¶m2=%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") );
}
Logged In: YES
user_id=1776308
Originator: YES
A patch has been provided. Please see https://sourceforge.net/tracker/?func=detail&aid=1705937&group_id=6550&atid=306550
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
Logged In: YES
user_id=1220573
Originator: NO
state changed now