Using Sun Java 6 HttpServer to write a functional HTTP test
September 2, 2009
(I originally planned this to be a single article, but because of the scope decided to split it into two parts. This first part explores the basics of using Sun’s
HttpServer to conduct functional HTTP testing. Part 2 revisits the following test using JUnit 4.7′s new interceptors (rules) feature and demonstrates a simpler HTTP handler.)
At work, we recently had the need to perform functional testing of a custom client that used HTTP as a transport. This isn’t strictly unit testing since we’re conducting actual HTTP over a socket & port instead of stubbing out or mocking the server, but in this case that was the only real way to test the client.
I could’ve fired up a standalone Web server and used that, but decided against it for a couple of reasons.
First, I wanted to have the server respond in a specific way to a particular client request. For example, if the request was for
GET /1234.xml I might want to respond with an
HTTP 200 and an XML response body. Another request for
GET /0.xml might return an
HTTP 404 instead.
To do that using, say, a Servlet container would mean writing multiple Servlets (mapped to various request URI) or a ‘rich’ Servlet with additional complexity. I didn’t want to have to write tests to test my test scaffolding!
Secondly, a standalone server would have to be started and stopped outside of our standard compile/test/package process (using Maven). Other people wouldn’t be able to run the tests successfully without having the test server up as well.
Clearly, the best way to go was to use an embedded HTTP server, which would allow us to provide specific responses tailored for each unit test.
As luck would have it, it turns out that Sun’s Java 6 implementation comes with a lightweight HTTP server API built in. Read on as I demonstrate the basic use of Sun’s HTTP server classes to write a functional test.
HTTP server in a box
The heart of our test solution involves taking advantage of the lightweight HTTP server API included in Sun’s Java 6 implementation. Note that since this isn’t part of the Java core API this package may not be available on all Java platforms. If this is a problem, you might be better off using another embedded HTTP server such as Jetty.
The class itself is com.sun.net.httpserver.HttpServer, and here’s how to use it in a nutshell:
- Create the server
- Create a server context and register a request handler
- Start the server
- Perform your test
- Stop the server, and verify/assert your expected behavior
Now let’s look at each step in detail with corresponding code.
Create the server
The second parameter to
HttpServer.create() is the ‘backlog’, “the maximum number of queued incoming connections to allow on the listening socket”. Since that doesn’t really affect us, we can just pass in a
0 and a system default value is used.
Creating and registering a request handler
Now we get to the meat of actually stubbing our server’s behavior by creating and registering a request handler.
HttpServer provides the
createContext(String path, HttpHandler handler) method to do just that.
HttpExchange is the class we need to work with when responding to HTTP requests. Let’s look at some code and I’ll explain what it does afterwards:
Basically: we convert our response string into a byte array. We send an
HTTP 200 (OK) along with the number of bytes we’re about to send as the response body. We then write out the bytes of our response body, then close the
HttpExchange. The complete
HttpExchange life cycle is detailed in its API documentation.
We now create a context for the URI we’re interested in, passing in our newly created
HttpHandler, then start the server:
At this point, an actual HTTP server will start running in a background thread ready to respond to requests. Let’s exercise our client code:
Our client code connects to our server, retrieves the associated URI, then, using a
BufferedReader reads in lines from the input stream. We make a few JUnit assertions on what we received.
The only thing left to do is to stop our
The parameter is the number of seconds (NOTE: not milliseconds) to block to wait for our
HttpServer to shutdown properly. Since we know we’re not serving any other requests, we can safely tell our
HttpServer to shut down immediately.
At a glance
Here’s our functional test at a glance:
While functional, I find the above code rather verbose. If I want to write more HTTP tests, I certainly don’t want to keep repeating myself going
httpServer.stop(0);. I especially find it tedious to have to compute the length of the HTTP response body beforehand, then send it along with our HTTP response code before writing out the actual body.
In the next part of this article, we look at using some shiny new features in JUnit 4.7 that lets us write cleaner, more reusable tests that involve pre and post test behavior. Also, I’ll exhibit a helper class that simplifies some of the effort involved in implementing an