RESTful Go: An API Server with 90%+ Test Coverage in 260 Lines of Code
I wanted to try building a small, RESTful API for a mobile app. And, like any respectable piece of software, I wanted close to 100% test coverage.
Note: If you find this post on writing well-tested Go applications useful, consider helping me write more of these posts, by supporting me on Patreon.
The Premise: Signature Collection for Petitions
I built an API to collect signatures for online petitions. Each signature is composed of a name, age, and short message. The server responds to the following requests:
|GET||/signatures||List all signatures|
|POST||/signatures||Create a signature|
- Martini: A Go web framework, like Sinatra. Used for routing.
- mgo: Pronounced “mango”. A Go driver for MongoDB, used to persist the signatures.
- Ginkgo & Gomega: Essential for Go unit testing.
- gory: Used to easily create signatures for unit testing.
Continue reading →