项目作者: maxcnunes

项目描述 :
Httpfake – A Golang httptest wrapper for easily setting up a fake server
高级语言: Go
项目地址: git://github.com/maxcnunes/httpfake.git
创建时间: 2017-07-17T02:44:55Z
项目社区:https://github.com/maxcnunes/httpfake

开源协议:MIT License

下载


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

  1. go get -u github.com/maxcnunes/httpfake

or

  1. 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.

Assertions

There are built-in methods you can use to make assertions about requests to your HTTP handlers. The currently
supported assertions are:

  • Presence of query parameters
  • Query parameter and its expected value
  • Presence of HTTP headers
  • HTTP header and its expected value
  • The expected body of your request

WithTesting must be provided as a server
option when creating the test server if you intend to set request assertions. Failing to set the option
when using request assertions will result in a panic.

Custom Assertions

You can also provide your own assertions by creating a type that implements the
Assertor interface or utilizing the
CustomAssertor function type. The Assertor.Log method will be
called for each assertion before it’s processed. The Assertor.Error method will only be called if the
Assertor.Assert method returns an error.

Examples

For a full list of examples please check out the functional_tests folder.

  1. // initialize the faker server
  2. // will bring up a httptest.Server
  3. fakeService := httpfake.New()
  4. // bring down the server once we
  5. // finish running our tests
  6. defer fakeService.Close()
  7. // register a handler for our fake service
  8. fakeService.NewHandler().
  9. Get("/users").
  10. Reply(200).
  11. BodyString(`[{"username": "dreamer"}]`)
  12. // run a real http request to that server
  13. 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 on gock. Check that you if you prefer mocking your requests.