HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
See the codeVersatile HTTP mocking made easy in Go that works with any net/http based stdlib implementation.
Heavily inspired by nock. There is also its Python port, pook.
To get started, take a look to the examples.
http.RoundTripper interface.net/http compatible client, such as gentleman.go get -u github.com/h2non/gock
See godoc reference for detailed API documentation.
http.DefaultTransport or custom http.Transport used by any http.Client.Declare your mocks before you start declaring the concrete test logic:
func TestFoo(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
gock.New("http://server.com").
Get("/bar").
Reply(200).
JSON(map[string]string{"foo": "bar"})
// Your test code starts here...
}
If you're running concurrent code, be aware that your mocks are declared first to avoid unexpected
race conditions while configuring gock or intercepting custom HTTP clients.
gock is not fully thread-safe, but sensible parts are.
Any help making gock more reliable in this sense is appreciated.
If you're mocking a bunch of mocks in the same test suite, it's recommended to define the more concrete mocks first, and then the generic ones.
This approach usually avoids matching unexpected generic mocks (e.g: specific header, body payload...) instead of the generic ones that performs less complex matches.
gock traffic interception once doneIn other to minimize potential side effects within your test code, it's a good practice
disabling gock once you are done with your HTTP testing logic.
A Go idiomatic approach for doing this can be using it in a defer statement, such as:
func TestGock (t *testing.T) {
defer gock.Off()
// ... my test code goes here
}
http.Client just onceYou don't need to intercept multiple times the same http.Client instance.
Just call gock.InterceptClient(client) once, typically at the beginning of your test scenarios.
http.Client after interceptionNOTE: this is not required is you are using http.DefaultClient or http.DefaultTransport.
As a good testing pattern, you should call gock.RestoreClient(client) after running your test scenario, typically as after clean up hook.
You can also use a defer statement for doing it, as you do with gock.Off(), such as:
func TestGock (t *testing.T) {
defer gock.Off()
defer gock.RestoreClient(client)
// ... my test code goes here
}
See examples directory for more featured use cases.
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestSimple(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
Get("/bar").
Reply(200).
JSON(map[string]string{"foo": "bar"})
res, err := http.Get("http://foo.com/bar")
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body)[:13], `{"foo":"bar"}`)
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestMatchHeaders(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
MatchHeader("Authorization", "^foo bar$").
MatchHeader("API", "1.[0-9]+").
HeaderPresent("Accept").
Reply(200).
BodyString("foo foo")
req, err := http.NewRequest("GET", "http://foo.com", nil)
req.Header.Set("Authorization", "foo bar")
req.Header.Set("API", "1.0")
req.Header.Set("Accept", "text/plain")
res, err := (&http.Client{}).Do(req)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body), "foo foo")
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestMatchParams(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
MatchParam("page", "1").
MatchParam("per_page", "10").
Reply(200).
BodyString("foo foo")
req, err := http.NewRequest("GET", "http://foo.com?page=1&per_page=10", nil)
res, err := (&http.Client{}).Do(req)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body), "foo foo")
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestMockSimple(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
Post("/bar").
MatchType("json").
JSON(map[string]string{"foo": "bar"}).
Reply(201).
JSON(map[string]string{"bar": "foo"})
body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
res, err := http.Post("http://foo.com/bar", "application/json", body)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 201)
resBody, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`)
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestClient(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
Reply(200).
BodyString("foo foo")
req, err := http.NewRequest("GET", "http://foo.com", nil)
client := &http.Client{Transport: &http.Transport{}}
gock.InterceptClient(client)
res, err := client.Do(req)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body), "foo foo")
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/h2non/gock"
)
func main() {
defer gock.Off()
defer gock.DisableNetworking()
gock.EnableNetworking()
gock.New("http://httpbin.org").
Get("/get").
Reply(201).
SetHeader("Server", "gock")
res, err := http.Get("http://httpbin.org/get")
if err != nil {
fmt.Errorf("Error: %s", err)
}
// The response status comes from the mock
fmt.Printf("Status: %d\n", res.StatusCode)
// The server header comes from mock as well
fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
// Response body is the original
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("Body: %s", string(body))
}
package main
import (
"bytes"
"net/http"
"github.com/h2non/gock"
)
func main() {
defer gock.Off()
gock.Observe(gock.DumpRequest)
gock.New("http://foo.com").
Post("/bar").
MatchType("json").
JSON(map[string]string{"foo": "bar"}).
Reply(200)
body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
http.Post("http://foo.com/bar", "application/json", body)
}
You can easily hack gock defining custom matcher functions with own matching rules.
See add matcher functions and custom matching layer examples for further details.
MIT - Tomas Aparicio
(top 24 of 30)
305 followers · starred Mar 2016
662 followers · starred Mar 2024
1,497 followers · starred Mar 2016
31 followers · starred Oct 2023
HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
See the codeVersatile HTTP mocking made easy in Go that works with any net/http based stdlib implementation.
Heavily inspired by nock. There is also its Python port, pook.
To get started, take a look to the examples.
http.RoundTripper interface.net/http compatible client, such as gentleman.go get -u github.com/h2non/gock
See godoc reference for detailed API documentation.
http.DefaultTransport or custom http.Transport used by any http.Client.Declare your mocks before you start declaring the concrete test logic:
func TestFoo(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
gock.New("http://server.com").
Get("/bar").
Reply(200).
JSON(map[string]string{"foo": "bar"})
// Your test code starts here...
}
If you're running concurrent code, be aware that your mocks are declared first to avoid unexpected
race conditions while configuring gock or intercepting custom HTTP clients.
gock is not fully thread-safe, but sensible parts are.
Any help making gock more reliable in this sense is appreciated.
If you're mocking a bunch of mocks in the same test suite, it's recommended to define the more concrete mocks first, and then the generic ones.
This approach usually avoids matching unexpected generic mocks (e.g: specific header, body payload...) instead of the generic ones that performs less complex matches.
gock traffic interception once doneIn other to minimize potential side effects within your test code, it's a good practice
disabling gock once you are done with your HTTP testing logic.
A Go idiomatic approach for doing this can be using it in a defer statement, such as:
func TestGock (t *testing.T) {
defer gock.Off()
// ... my test code goes here
}
http.Client just onceYou don't need to intercept multiple times the same http.Client instance.
Just call gock.InterceptClient(client) once, typically at the beginning of your test scenarios.
http.Client after interceptionNOTE: this is not required is you are using http.DefaultClient or http.DefaultTransport.
As a good testing pattern, you should call gock.RestoreClient(client) after running your test scenario, typically as after clean up hook.
You can also use a defer statement for doing it, as you do with gock.Off(), such as:
func TestGock (t *testing.T) {
defer gock.Off()
defer gock.RestoreClient(client)
// ... my test code goes here
}
See examples directory for more featured use cases.
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestSimple(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
Get("/bar").
Reply(200).
JSON(map[string]string{"foo": "bar"})
res, err := http.Get("http://foo.com/bar")
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body)[:13], `{"foo":"bar"}`)
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestMatchHeaders(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
MatchHeader("Authorization", "^foo bar$").
MatchHeader("API", "1.[0-9]+").
HeaderPresent("Accept").
Reply(200).
BodyString("foo foo")
req, err := http.NewRequest("GET", "http://foo.com", nil)
req.Header.Set("Authorization", "foo bar")
req.Header.Set("API", "1.0")
req.Header.Set("Accept", "text/plain")
res, err := (&http.Client{}).Do(req)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body), "foo foo")
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestMatchParams(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
MatchParam("page", "1").
MatchParam("per_page", "10").
Reply(200).
BodyString("foo foo")
req, err := http.NewRequest("GET", "http://foo.com?page=1&per_page=10", nil)
res, err := (&http.Client{}).Do(req)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body), "foo foo")
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestMockSimple(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
Post("/bar").
MatchType("json").
JSON(map[string]string{"foo": "bar"}).
Reply(201).
JSON(map[string]string{"bar": "foo"})
body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
res, err := http.Post("http://foo.com/bar", "application/json", body)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 201)
resBody, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`)
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package test
import (
"io/ioutil"
"net/http"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
func TestClient(t *testing.T) {
defer gock.Off()
gock.New("http://foo.com").
Reply(200).
BodyString("foo foo")
req, err := http.NewRequest("GET", "http://foo.com", nil)
client := &http.Client{Transport: &http.Transport{}}
gock.InterceptClient(client)
res, err := client.Do(req)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body), "foo foo")
// Verify that we don't have pending mocks
st.Expect(t, gock.IsDone(), true)
}
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/h2non/gock"
)
func main() {
defer gock.Off()
defer gock.DisableNetworking()
gock.EnableNetworking()
gock.New("http://httpbin.org").
Get("/get").
Reply(201).
SetHeader("Server", "gock")
res, err := http.Get("http://httpbin.org/get")
if err != nil {
fmt.Errorf("Error: %s", err)
}
// The response status comes from the mock
fmt.Printf("Status: %d\n", res.StatusCode)
// The server header comes from mock as well
fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
// Response body is the original
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("Body: %s", string(body))
}
package main
import (
"bytes"
"net/http"
"github.com/h2non/gock"
)
func main() {
defer gock.Off()
gock.Observe(gock.DumpRequest)
gock.New("http://foo.com").
Post("/bar").
MatchType("json").
JSON(map[string]string{"foo": "bar"}).
Reply(200)
body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
http.Post("http://foo.com/bar", "application/json", body)
}
You can easily hack gock defining custom matcher functions with own matching rules.
See add matcher functions and custom matching layer examples for further details.
MIT - Tomas Aparicio
(top 24 of 30)
305 followers · starred Mar 2016
662 followers · starred Mar 2024
1,497 followers · starred Mar 2016
31 followers · starred Oct 2023