- Notifications
You must be signed in to change notification settings - Fork6
Httpfake – A Golang httptest wrapper for easily setting up a fake server
License
maxclaus/httpfake
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
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.
SeeReleases for detailed history changes.
Seegodoc reference for detailed API documentation.
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.
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.
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"))
See theContributing guide for steps on how to contribute to this project.
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