How to test a Servlet
The jetty infrastructure can be use to test servlets with an embedded server configured by the ServletTester class. The HTTP requests and responses for testing can be generated and parsed with the HttpTester class.
An example of a test harness that uses these classes is ServletTest.
Setting up the tester
The ServletTester can configure a single context. Servlets and Filters may be added to the context by class name or class instance. Context attributes, a resource base or a classloader may optionally be set. eg.
Raw HTTP requests and responses.
The ServletTester takes a string containing requests and
returns a string containing the corresponding responses (eventually byte arrays will be supported for testing character encoding and binary content). More than one request can be pipelined and multiple responses will be returned if persistent connection conditions are met. eg.
Generated Requests and Parsed Responses
Dealing with raw HTTP can be a bit verbose and difficult to test non protocol aspects. The HttpTester class allows for simple generation of requests and parsing of response (it can also parse requests and generate responses). eg.
Once setup, the HttpTester instances may be reused and only the parts that change need to be
set for subsequent requests. eg.