Giter Club home page Giter Club logo

httpfake's Introduction

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.

httpfake's People

Contributors

carlware avatar erutherford avatar maxcnunes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

httpfake's Issues

WithTesting() binds us to the testing framework

Problem description

func WithTesting(t *testing.T) ServerOption

WithTesting() takes a *testing.T parameter. In a framework such as Godog, individual scenarios should be self-contained, but the need to use a *testing.T forces us to use the same object (no thanks to the testing package, which does not let us create our own one). This means that the assertions from one test contaminate another.

Ideally, we should be able to pass in our own testing object. So, could we switch WithTesting() to take an interface instead?

Suggested solutions
Switch from struct to interface testing.TB, which is already used through the package:

func WithTesting(tb testing.TB) ServerOption

On a personal level, I would also suggest also moving from testing.TB to an interface that just contains the methods actually used. Most other assertion packages now follow this approach and it makes it much less painful to implement the interface.

I'm happy to do the work and raise a PR if this approach finds favour.

Define a subset of the testing.TB methods needed for assertions

We should determine the subset of methods from the testing.TB interface we need to provide for fully functioning assertions. We should also take into consideration which methods should or shouldn't be used across goroutine boundaries. Once we've determine the subset we should update the WithTesting method and the built in assertions to use this interface.

This change should be considered a breaking change for versioning.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.