Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: 13 days ago
JSON representation

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

Awesome Lists containing this project

README

        

httpfake
========

[![LICENSE](https://img.shields.io/badge/license-MIT-orange.svg)](LICENSE)
[![Godocs](https://img.shields.io/badge/golang-documentation-blue.svg)](https://godoc.org/github.com/maxcnunes/httpfake)
[![Build Status](https://travis-ci.org/maxcnunes/httpfake.svg?branch=master)](https://travis-ci.org/maxcnunes/httpfake)
[![Coverage Status](https://coveralls.io/repos/github/maxcnunes/httpfake/badge.svg?branch=master)](https://coveralls.io/github/maxcnunes/httpfake?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/maxcnunes/httpfake)](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.