https://github.com/maxcnunes/httpfake
Httpfake – A Golang httptest wrapper for easily setting up a fake server
https://github.com/maxcnunes/httpfake
fake-server fake-services go http httptest
Last synced: 6 months ago
JSON representation
Httpfake – A Golang httptest wrapper for easily setting up a fake server
- Host: GitHub
- URL: https://github.com/maxcnunes/httpfake
- Owner: maxcnunes
- License: mit
- Archived: true
- Fork: true (maxclaus/httpfake)
- Created: 2025-01-11T19:07:22.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2025-01-11T19:12:58.000Z (6 months ago)
- Last Synced: 2025-01-13T04:55:52.749Z (6 months ago)
- Language: Go
- Homepage:
- Size: 102 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
Awesome Lists containing this project
README
httpfake
========
[](LICENSE)
[](https://godoc.org/github.com/maxcnunes/httpfake)
[](https://travis-ci.org/maxcnunes/httpfake)
[](https://coveralls.io/github/maxcnunes/httpfake?branch=master)
[](https://goreportcard.com/report/github.com/maxcnunes/httpfake)httpfake provides is a simple wrapper for [httptest](https://golang.org/pkg/net/http/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](https://github.com/maxcnunes/httpfake/releases) for detailed history changes.
## API
See [godoc reference](https://godoc.org/github.com/maxcnunes/httpfake) 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](https://godoc.org/github.com/maxcnunes/httpfake#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](https://godoc.org/github.com/maxcnunes/httpfake#Assertor) or utilizing the
[CustomAssertor function type](https://pkg.go.dev/github.com/maxcnunes/httpfake#CustomAssertor). 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](/functional_tests).
```go
// 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.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](/CONTRIBUTING.md) for steps on how to contribute to this project.
## Reference
This package was heavily inspired on [gock](https://github.com/h2non/gock). Check that you if you prefer mocking your requests.