public void testHTTPPost() throws Exception {
// set up the mock request object
expect(request.getMethod()).andReturn(HttpMethod.POST); // note: POST instead of GET
// getRequestURL may be called multiple times in JAX-RS engine, hence
// we need to configure expectations to allow it to be called many times.
expect(request.getRequestURL()).andReturn(
new StringBuffer("/helloworld")).anyTimes();
expect(request.getContextPath()).andReturn("");
// make sure the input stream carries the data we intend to be passed as the query
expect(request.getInputStream()).andReturn(
new MockServletInputStream(new ByteArrayInputStream(
"Echo of Hello, World!".getBytes())));
// turn on the EasyMock checking
replay(request);
replay(response);
replay(config);
servlet.init(config);
// execute the test (note: doPost instead of doGet)
servlet.doPost(request, response);
// let EasyMock check that the behavior expectations are satisfied (in this example, the expectations are loosely defined)
verify(request);
// get the output stream buffer and verify the contents
StringBuffer buffer = writer.getBuffer();
assertEquals("Echo of Hello, World!", buffer.toString());
}
public class MockServletInputStream extends javax.servlet.ServletInputStream {
private InputStream content;
public MockServletInputStream(InputStream is) {
content = is;
}
@Override
public int read() throws IOException {
return content.read();
}
}