Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

maxclaus/httpfake

Repository files navigation

LICENSEGodocsBuild StatusCoverage StatusGo Report Card

httpfake provides is a simple wrapper forhttptest 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

SeeReleases for detailed history changes.

API

Seegodoc reference for detailed API documentation.

Assertions

There are built-in methods you can use to make assertions about requests to your HTTP handlers. The currentlysupported 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

WithTestingmust be provided as a serveroption when creating the test server if you intend to set request assertions. Failing to set the optionwhen using request assertions will result in a panic.

Custom Assertions

You can also provide your own assertions by creating a type that implements theAssertor interface or utilizing theCustomAssertor function type. TheAssertor.Log method will becalled for each assertion before it's processed. TheAssertor.Error method will only be called if theAssertor.Assert method returns an error.

Examples

For a full list of examples please check out thefunctional_tests folder.

// initialize the faker server// will bring up a httptest.ServerfakeService:=httpfake.New()// bring down the server once we// finish running our testsdeferfakeService.Close()// register a handler for our fake servicefakeService.NewHandler().Get("/users").Reply(200).BodyString(`[{"username": "dreamer"}]`)// run a real http request to that serverres,err:=http.Get(fakeService.ResolveURL("/users"))

Contributing

See theContributing guide for steps on how to contribute to this project.

Reference

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp