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.
server := webmock.New()
baseURL := server.URL()
server.Start()
server.Stub("GET", "/hello", "ok")
server.Stub("POST", "/hello", "ok-post")
// curl http://server/hello
// curl -XPOST http://server/hello
server.Stub(
"GET",
"/hello",
"ok",
webmock.WithHeaders("Content-Type: application/json"),
)
// curl -H "Content-Type: application/json" http://server/hello
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 a directory
server.LoadCassette("./fixtures")
// Load a yaml file
server.LoadCassette("./fixtures/sample_cassette.yml")