httpfake provides is a simple wrapper for httptest with a handful chainable API for setting up handlers to a fake server. This package is aimed to be used in tests where the original external server must not be reached. Instead is used in its place a fake server which can be configured to handle any request as desired.
go get -u github.com/maxcnunes/httpfake
or
govendor fetch github.com/maxcnunes/httpfake
If possible give preference for using vendor. This way the version is locked up as a dependency in your project.
See Releases for detailed history changes.
See godoc reference for detailed API documentation.
For a full list of examples please check out the functional_tests folder.
// initialize the faker server
// will bring up a httptest.Server
fakeService := httpfake.New()
// bring down the server once we
// finish running our tests
defer fakeService.Server.Close()
// register a handler for our fake service
fakeService.NewHandler().
Get("/users").
Reply(200).
BodyString(`[{"username": "dreamer"}]`)
// run a real http request to that server
res, err := http.Get(fakeService.ResolveURL("/users"))
See the Contributing guide for steps on how to contribute to this project.
This package was heavily inspired by gock. Check that you if you prefer mocking your requests.