Popularity
0.2
Stable
Activity
0.6
-
1
0
0

Description

This package creates a HTTP server, it stubs requests. Inspired by bblimke/webmock. It's useful when writing integration tests while code reply on external requests. With webmock, just point the endpoint/host to mock server and stub the requests.

Webmock takes a different approach compared with gock or httpmock, it doesn't intercept http Client and replace http Transport. It runs a web server, whenever comes a request, it tries to match the request and return stub response.

Programming language: Go
Tags: Testing     API     Mock     Vcr    

webmock alternatives and similar packages

Based on the "Mock" category.
Alternatively, view webmock alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of webmock or a related project?

Add another 'Mock' Package

README

Webmock

This package creates a HTTP server, it stubs requests. Inspired by bblimke/webmock. It's useful when writing integration tests while code reply on external requests. With webmock, just point the endpoint/host to mock server and stub the requests.

Webmock takes a different approach compared with gock or httpmock, it doesn't intercept http Client and replace http Transport. It runs a web server, whenever comes a request, it tries to match the request and return stub response.

Examples

Start mock server

server := webmock.New()
baseURL := server.URL()

server.Start()

Stubbed request based on method, url

server.Stub("GET", "/hello", "ok")
server.Stub("POST", "/hello", "ok-post")

// curl http://server/hello
// curl -XPOST http://server/hello

Stubbed request based on method, url and headers

server.Stub(
    "GET",
    "/hello",
    "ok",
    webmock.WithHeaders("Content-Type: application/json"),
)

// curl -H "Content-Type: application/json" http://server/hello

Stubbing with custom response

server.Stub(
  "GET",
  "/abc?foo=bar",
  "",
  webmock.WithHeaders("Accept: application/json"),
  webmock.WithResponse(500, "Ah oh", map[string]string{
    "Access-Control-Allow-Origin": "*",
    "Content-Type":                "application/xml",
  }),
)

// curl -i -H "Content-Type: application/json" http://server/abc?foo=bar

Load cassettes

// Load a directory
server.LoadCassette("./fixtures")

// Load a yaml file
server.LoadCassette("./fixtures/sample_cassette.yml")