Last active
June 13, 2023 04:54
-
-
Save cep21/1f2a5c61a2186db8d040b6ec20dd5db1 to your computer and use it in GitHub Desktop.
Stub out RoundTripper to test HTTP client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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