Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
String requests=
   "GET /context/servlet/info?query=foo HTTP/1.1\r\n"+
   "Host: tester\r\n"+
   "\r\n"+         
   "GET /context/hello HTTP/1.1\r\n"+
   "Host: tester\r\n"+
   "\r\n";
            
String responses = tester.getResponses(requests);
        
String expected=
   "HTTP/1.1 200 OK\r\n"+
   "Content-Type: text/html; charset=iso-8859-1\r\n"+
   "Content-Length: 21\r\n"+
   "\r\n"+
   "<h1>Test Servlet</h1>" +
         
   "HTTP/1.1 200 OK\r\n"+
   "Content-Type: text/html; charset=iso-8859-1\r\n"+
   "Content-Length: 22\r\n"+
   "\r\n"+
   "<h1>Hello Servlet</h1>";
            
    assertEquals(expected,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.

Code Block

HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
request.setMethod("GET");
request.setHeader("Host","tester");
request.setURI("/context/hello/info");
request.setVersion("HTTP/1.0");

response.parse(tester.getResponses(request.generate()));

assertTrue(response.getMethod()==null);
assertEquals(200,response.getStatus());
assertEquals("<h1>Hello Servlet</h1>",response.getContent());

Once setup, the HttpTester instances may be reused and only the parts that change need to be
set for subsequent requests. eg.

Code Block
        
request.setURI("/context");
response.parse(tester.getResponses(request.generate()));
assertEquals(302,response.getStatus());
assertEquals("http://tester/context/",response.getHeader("location"));
Contact the core Jetty developers at www.webtide.com
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery