Skip to content

Httpfake – A Golang httptest wrapper for easily setting up a fake server

License

Notifications You must be signed in to change notification settings

maxclaus/httpfake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e90b23b · Aug 29, 2017

History

27 Commits
Jul 18, 2017
Jul 21, 2017
Jul 17, 2017
Jul 17, 2017
Jul 21, 2017
Jul 18, 2017
Jul 17, 2017
Jul 18, 2017
Jul 21, 2017
Aug 29, 2017
Jul 18, 2017
Jul 18, 2017
Jul 18, 2017
Jul 18, 2017

Repository files navigation

httpfake

LICENSE Godocs Build Status Coverage Status Go Report Card

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.

Installation

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.

Changelog

See Releases for detailed history changes.

API

See godoc reference for detailed API documentation.

Examples

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"))

Contributing

See the Contributing guide for steps on how to contribute to this project.

Reference

This package was heavily inspired by gock. Check that you if you prefer mocking your requests.