Skip to content

Instantly share code, notes, and snippets.

@cep21
Last active June 13, 2023 04:54
Show Gist options
  • Save cep21/1f2a5c61a2186db8d040b6ec20dd5db1 to your computer and use it in GitHub Desktop.
Save cep21/1f2a5c61a2186db8d040b6ec20dd5db1 to your computer and use it in GitHub Desktop.
Stub out RoundTripper to test HTTP client
type roundTripFunc func (r *http.Request) (*http.Response, error)
func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return s(r)
}
func TestSword(t *testing.T) {
var c Client
c.Client.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
assert.Equal(t, r.URL.Path, "/v1/item/sword")
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"type":"sword"}`)),
}, nil
})
item, err := c.GetItem(ctx, "sword")
assert.Nil(t, err)
assert.Equal(t, item.Type, "sword")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment