Liz Douglass

DefaultHttpClientIntegrationTest

leave a comment »

Today Tom and I were looking at our DefaultHttpClient class. This class wraps/adapts an Apache HttpClient. It implements our HttpWebClient interface:

public interface HttpWebClient {

    public HttpResponse post(String endpointURL, String requestBody);

    public HttpResponse get(String endpointURL, QueryString query);
}

Until today we didn’t have any tests for just this class, although there was some coverage from integration tests for other classes. We added tests that are very similar to some written by Cam and Silvio in another project (full credit to them). There’s a couple of interesting things happening our test class (below):

  • A Jetty server is created in the setup method. The DefaultHttpWebClient test instance makes a request to this server.
  • The findFreePort method uses java.net.ServerSocket to identify a free port for a Jetty Server to run on.
  • Each test sets the handler for the test Jetty server as a new TestHandler (line 84). The TestHandler class implements the Jetty AbstractHandler interface. It delegates to an overloaded handle method that is defined in each test method.
  • In the shouldPerformHttpPost test the handle method returns a response with an OK status code. It also puts some information into a map called ‘inspector’. Request information is put into the ‘inspector’ and it is used in several assertions.

Here tis:

public class DefaultHttpWebClientIntegrationTest {

    private int freePort;
    private Server server;

    @Before
    public void setup() throws Exception {
        freePort = findFreePort();
        server = new Server(freePort);
        server.start();
    }

    @After
    public void teardown() throws Exception {
        server.stop();
    }

    private int findFreePort() throws Exception {
        ServerSocket socket = new ServerSocket(0);
        int port = socket.getLocalPort();
        socket.close();
        return port;
    }

    @Test
    public void shouldPerformHttpPost() {
        final Map<String, String> inspector = Maps.create();

        server.setHandler(new TestHandler() {
            public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {

                inspector.put("method", request.getMethod());
                inspector.put("contentType", request.getContentType());
                inspector.put("body", IOUtils.toString(request.getInputStream()));

                response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("text/plain");
                response.getWriter().print("test response");
            }
        });

        DefaultHttpWebClient client = new DefaultHttpWebClient();
        HttpResponse response = client.post("http://localhost:" + freePort + "/test", "test payload");

        assertThat(inspector.get("method"), equalTo("POST"));
        assertThat(inspector.get("contentType"), equalTo("text/xml; charset=UTF-8"));
        assertThat(inspector.get("body"), equalTo("test payload"));

        assertThat(response.isOK(), equalTo(true));
        assertThat(response.getContentType(), startsWith("text/plain"));
        assertThat(response.getResponseBodyAsText(), equalTo("test response"));
    }

    @Test
    public void shouldPerformHttpGet() {
        final Map<String, String> inspector = Maps.create();

        server.setHandler(new TestHandler() {
            public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {

                inspector.put("method", request.getMethod());
                inspector.put("foo", request.getParameter("foo"));

                response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("text/plain");
                response.getWriter().print("test response");
            }
        });

        QueryString queryString = new QueryString();
        queryString.add("foo", "bar");

        DefaultHttpWebClient client = new DefaultHttpWebClient();
        HttpResponse response = client.get("http://localhost:" + freePort + "/test", queryString);

        assertThat(inspector.get("method"), equalTo("GET"));
        assertThat(inspector.get("foo"), equalTo("bar"));

        assertThat(response.isOK(), equalTo(true));
        assertThat(response.getContentType(), startsWith("text/plain"));
        assertThat(response.getResponseBodyAsText(), equalTo("test response"));
    }

    private static abstract class TestHandler extends AbstractHandler {

        public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
                throws IOException, ServletException {

            handle(request, response);
            ((Request) request).setHandled(true);
        }

        public abstract void handle(HttpServletRequest request, HttpServletResponse response) throws IOException;
    }
}
Advertisements

Written by lizdouglass

December 14, 2009 at 10:19 am

Posted in Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: