Smock ( Previously: RequestBin ) is a golang package for testing the outgoing http requests initiated from a function. The package creates a mock server and passes the URL to the function to be tested and then collects all the requests that the server received.
The package was previously called RequestBin, but was changed as per the request of @johnsheehan, the CEO of Runscope (The company behind http://requestb.in/). Clones will still work with the old name thanks to Github, but the API will be slightly changed.
Smock is a package for testing the outgoing http requests initiated from a function. The package creates a mock server and passes the URL to the function to be tested and then collects all the requests that the server received.
func ExampleCaptureRequests() {
reqs := smock.NewMockServer(smock.MockServerConfig{RequestTimeout: 1}).CaptureRequests(func(url string) {
http.Post(url, "text/plain", strings.NewReader("Hello"))
http.Post(url, "text/plain", strings.NewReader("It's me"))
})
fmt.Println(reqs[0].Body) // Hello
fmt.Println(reqs[1].Body) // It's me
}
The server can be stopped after a certain amount of seconds, after a certain amount of requests, whenever it doesn't receive any new requests for a certain amount of time or any combination of the three.
The API is very simple and you can check it on GoDoc. Reading the tests is also a good way for understanding the API.
For a usecase you can read my blog post about this package : http://blog.mbassem.com/2016/02/09/smock/.
Your contributions and ideas are welcomed through issues and pull requests.