HTTPClient interface (#754)
* HTTPClient interface
* Spelling and punctuation fixes
Ivan Skriabin authored 5 years ago
Peter Bourgon committed 5 years ago
11 | 11 |
"github.com/go-kit/kit/endpoint"
|
12 | 12 |
)
|
13 | 13 |
|
|
14 |
// HTTPClient is an interface that models *http.Client.
|
|
15 |
type HTTPClient interface {
|
|
16 |
Do(req *http.Request) (*http.Response, error)
|
|
17 |
}
|
|
18 |
|
14 | 19 |
// Client wraps a URL and provides a method that implements endpoint.Endpoint.
|
15 | 20 |
type Client struct {
|
16 | |
client *http.Client
|
|
21 |
client HTTPClient
|
17 | 22 |
method string
|
18 | 23 |
tgt *url.URL
|
19 | 24 |
enc EncodeRequestFunc
|
|
53 | 58 |
|
54 | 59 |
// SetClient sets the underlying HTTP client used for requests.
|
55 | 60 |
// By default, http.DefaultClient is used.
|
56 | |
func SetClient(client *http.Client) ClientOption {
|
|
61 |
func SetClient(client HTTPClient) ClientOption {
|
57 | 62 |
return func(c *Client) { c.client = client }
|
58 | 63 |
}
|
59 | 64 |
|
0 | 0 |
package http_test
|
1 | 1 |
|
2 | 2 |
import (
|
|
3 |
"bytes"
|
3 | 4 |
"context"
|
4 | 5 |
"io"
|
5 | 6 |
"io/ioutil"
|
|
251 | 252 |
}
|
252 | 253 |
}
|
253 | 254 |
|
|
255 |
func TestSetClient(t *testing.T) {
|
|
256 |
var (
|
|
257 |
encode = func(context.Context, *http.Request, interface{}) error { return nil }
|
|
258 |
decode = func(_ context.Context, r *http.Response) (interface{}, error) {
|
|
259 |
t, err := ioutil.ReadAll(r.Body)
|
|
260 |
if err != nil {
|
|
261 |
return nil, err
|
|
262 |
}
|
|
263 |
return string(t), nil
|
|
264 |
}
|
|
265 |
)
|
|
266 |
|
|
267 |
testHttpClient := httpClientFunc(func(req *http.Request) (*http.Response, error) {
|
|
268 |
return &http.Response{
|
|
269 |
StatusCode: http.StatusOK,
|
|
270 |
Request: req,
|
|
271 |
Body: ioutil.NopCloser(bytes.NewBufferString("hello, world!")),
|
|
272 |
}, nil
|
|
273 |
})
|
|
274 |
|
|
275 |
client := httptransport.NewClient(
|
|
276 |
"GET",
|
|
277 |
&url.URL{},
|
|
278 |
encode,
|
|
279 |
decode,
|
|
280 |
httptransport.SetClient(testHttpClient),
|
|
281 |
).Endpoint()
|
|
282 |
|
|
283 |
resp, err := client(context.Background(), nil)
|
|
284 |
if err != nil {
|
|
285 |
t.Fatal(err)
|
|
286 |
}
|
|
287 |
if r, ok := resp.(string); !ok || r != "hello, world!" {
|
|
288 |
t.Fatal("Expected response to be 'hello, world!' string")
|
|
289 |
}
|
|
290 |
}
|
|
291 |
|
254 | 292 |
func mustParse(s string) *url.URL {
|
255 | 293 |
u, err := url.Parse(s)
|
256 | 294 |
if err != nil {
|
|
264 | 302 |
}
|
265 | 303 |
|
266 | 304 |
func (e enhancedRequest) Headers() http.Header { return http.Header{"X-Edward": []string{"Snowden"}} }
|
|
305 |
|
|
306 |
type httpClientFunc func(req *http.Request) (*http.Response, error)
|
|
307 |
|
|
308 |
func (f httpClientFunc) Do(req *http.Request) (*http.Response, error) {
|
|
309 |
return f(req)
|
|
310 |
}
|
14 | 14 |
|
15 | 15 |
// Client wraps a JSON RPC method and provides a method that implements endpoint.Endpoint.
|
16 | 16 |
type Client struct {
|
17 | |
client *http.Client
|
|
17 |
client httptransport.HTTPClient
|
18 | 18 |
|
19 | 19 |
// JSON RPC endpoint URL
|
20 | 20 |
tgt *url.URL
|
|
85 | 85 |
|
86 | 86 |
// SetClient sets the underlying HTTP client used for requests.
|
87 | 87 |
// By default, http.DefaultClient is used.
|
88 | |
func SetClient(client *http.Client) ClientOption {
|
|
88 |
func SetClient(client httptransport.HTTPClient) ClientOption {
|
89 | 89 |
return func(c *Client) { c.client = client }
|
90 | 90 |
}
|
91 | 91 |
|