Re: How to simulate UTF-8 encoded request parameters
"Stefan Hübner" <[email protected]>
| Newsgroups | gmane.comp.web.httpunit.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi, I have to give this post a second shot, since it really hurts. Maybe there's somebody out there, who could give me a hint or confirms this a bug in HTTPUnit? I want to test a servlet which serves GET requests. Those requests have parameter values encoded in UTF-8-URL-Encoding. My problem is, that my servlet under test gets wrongly i.e. ISO-8859-1 decoded request parameter values (by invoking getParameter() on HttpServletRequest). The values are completely messed up then, of course. The attached post has a simple test case to demonstrate this strange behaviour. Anybody who is familiar with this phenomenon? Any hint is very much appreciated! Thanks, Stefan Hübner 2007/3/15, Stefan Hübner <[email protected]>: > Hi all, > > I'm new to the list (although not new to HttpUnit) and couldn't figure > out a mail archive with convenient searchability. So sorry, if this > has been discussed before. > > My team is in the middle of converting a reasonable big web > application from ISO-8859-1 to UTF-8-encoding. Now, the incoming > requests start to be URL-encoded using character set UTF-8. we > configured the servlet container (tomcat here) to handle those > requests properly, which works fine. > > Coming to the point, though, I ran into issues trying to unit test > some servlet using HttpUnit. the servlets read request parameters. Of > course I want to simulate UTF-8 encoded parameters here. But couldn't > get it the right way - if there is one. > > The TestCase I'll attach fails with "ComparisonFailure: expected: > <äöü> but was: <äöü>". Could somebody please point me into a > direction of howto unit test those scenarios the right way? > > Any hints are highly appreciated! Thanks in advance > -Stefan Hübner > > > > > > > ps: here comes the code: > > ------------------------------------------------------------------- > import java.io.IOException; > import java.net.MalformedURLException; > > import javax.servlet.http.HttpServlet; > import javax.servlet.http.HttpServletRequest; > > import junit.framework.TestCase; > > import com.meterware.httpunit.GetMethodWebRequest; > import com.meterware.httpunit.HttpUnitOptions; > import com.meterware.httpunit.WebRequest; > import com.meterware.servletunit.InvocationContext; > import com.meterware.servletunit.ServletRunner; > import com.meterware.servletunit.ServletUnitClient; > > public class HttpUnitTest extends TestCase { > > /** > * Trying to figure out, how to simulate UTF-8-encoded request parameters > * > * @throws MalformedURLException > * @throws IOException > */ > public void testURIEncoding() throws MalformedURLException, IOException { > // Thought, this would simulate a containers URIEncoding-setting > HttpUnitOptions.setDefaultCharacterSet("utf-8"); > > ServletRunner servletRunner = new ServletRunner(); > servletRunner.registerServlet("dummy", DummyServlet.class.getName()); > > WebRequest request = new GetMethodWebRequest("http://localhost/dummy"); > request.setParameter("param", "\u00e4\u00f6\u00fc"); > > ServletUnitClient sc = servletRunner.newClient(); > InvocationContext ic = sc.newInvocation(request); > HttpServletRequest httpRequest = ic.getRequest(); > > // Test fails with "ComparisonFailure: expected: <äöü> but was: <äöü>" > assertEquals("äöü", httpRequest.getParameter("param")); > } > > class DummyServlet extends HttpServlet { > > } > } > ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Httpunit-develop mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/httpunit-develop