For our application logic tests we’ve primarily been using ginkgo and gomega, and we love taking advantage of the built-in Go benchmarking feature. Where we need to mock interfaces to test calls on other packages, we use gomock. Unfortunately those sites didn't have any clear documentation on how to write router tests, so I started trawling the web looking for more descriptive guides.

The First Pass

The first helpful article I found was Kyle Truscoff’s blog post, Mocking HTTP Responses in Golang. While his application didn’t exactly mirror ours, it clued me in on how a lot of the httptest package works in conjunction with the http package to allow for testing response objects. It was also very helpful as a guide for how to test handlers.

I took a basic stab at implementing what I had learned through the post and uncovered an issue in the way that we had written our own application routing code!

Our HTTP router is initialized within a versioned package for our HTTP APIs. This package describes an HttpAPI object which contains the route and handler (we use gorilla) definitions for our server (which we serve using grace). When we call New() in this package, we create a new HttpAPI object, assign handlers to routes, and generate a server object (for passing to grace). All of this information is then attached to the HttpAPI struct which was being generated.