How to simulate UTF-8 encoded request parameters
"Stefan Hübner" <[email protected]>
| Newsgroups | gmane.comp.web.httpunit.devel |
|---|---|
| Message-ID | <[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