• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

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

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

More Repositories

1

waitforit

Wait until an address become available.
Go
172
star
2

gaper

Builds and restarts a Go project when it crashes or some watched file changes
Go
81
star
3

elixir-phoenix-rest-api

A simple example of a rest API with elixir and phoenix
Elixir
75
star
4

go-uptime-api

Uptime monitor API written in Golang
Go
33
star
5

mongo-to-knex

Applies a query like mongo standards to knex query builder
JavaScript
17
star
6

docker-unrar

11
star
7

solr-query-builder

A query builder for Solr in node js
JavaScript
8
star
8

remove-reactid-attributes

Clean up HTML from a generated page with React
HTML
6
star
9

go-uptime-app

Uptime monitor APP using React js
JavaScript
6
star
10

i-sweated-yesterday

A small application to count the days I did any exercise (sport)
Python
6
star
11

docker-url-redirect

Lightweight docker image (2 MB) to redirect URLs.
Go
5
star
12

SignalR-Auction

Example using SignalR to build a simple Auction App
JavaScript
5
star
13

dotfiles

Lua
5
star
14

git-web-hook

JavaScript
4
star
15

docker-electron-distribution

A image based in debian:jessie with necessary dependencies to build/pack and create installers for an electron application from Linux to any other OS
Shell
4
star
16

flaskgaedemo

Flask and Google App Engine
Python
3
star
17

ranking

JavaScript
3
star
18

ngs

n git satus - a tool to check the status of multiple git repositories
Go
3
star
19

factory-mysql-fixtures

Factory to load fixtures on mysql database.
JavaScript
3
star
20

slack-files

Easy control over the Slack files clean up
Go
3
star
21

rancheros-guest

Ruby
2
star
22

tokenizer

Generates oauth2 tokens
Go
2
star
23

GoogleMapsAPI3

Funcionalidades bΓ‘sicas do Google Maps API 3: Mapa, rotas, marcadores, bounds...
JavaScript
2
star
24

SOSPosto

Estudo de caso prΓ‘tico em WCF+SOA+MySql da minha monografia "Modelagem UML para SOA"
C#
1
star
25

learning-assembly-arm64-apple

Assembly
1
star
26

maxcnunes.github.io

My Blog
HTML
1
star
27

poll-service-go-gorilla

Go
1
star
28

go-queue

A modest implementation of queue in go
Go
1
star
29

flask_bravi

Short demo of an apresentention about Flask
Python
1
star
30

chakra-app-colorMode-undefined

TypeScript
1
star
31

playwright-electron-xvfb

1
star
32

winston-slackihook

JavaScript
1
star
33

Perfin

Personal Finance Control - Easy and Simple
JavaScript
1
star