1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
package test
import (
"github.com/nbio/st"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"net/http"
"testing"
)
func TestMultipleMocks(t *testing.T) {
defer gock.Disable()
gock.New("http://server.com").
Get("/foo").
Reply(200).
JSON(map[string]string{"value": "foo"})
gock.New("http://server.com").
Get("/bar").
Reply(200).
JSON(map[string]string{"value": "bar"})
gock.New("http://server.com").
Get("/baz").
Reply(200).
JSON(map[string]string{"value": "baz"})
tests := []struct {
path string
}{
{"/foo"},
{"/bar"},
{"/baz"},
}
for _, test := range tests {
res, err := http.Get("http://server.com" + test.path)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
body, _ := ioutil.ReadAll(res.Body)
st.Expect(t, string(body)[:15], `{"value":"`+test.path[1:]+`"}`)
}
// Failed request after mocks expires
_, err := http.Get("http://server.com/foo")
st.Reject(t, err, nil)
}
|