Codebase list golang-github-go-kit-kit / 4914a69
HTTPClient interface (#754) * HTTPClient interface * Spelling and punctuation fixes Ivan Skriabin authored 5 years ago Peter Bourgon committed 5 years ago
3 changed file(s) with 53 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
1111 "github.com/go-kit/kit/endpoint"
1212 )
1313
14 // HTTPClient is an interface that models *http.Client.
15 type HTTPClient interface {
16 Do(req *http.Request) (*http.Response, error)
17 }
18
1419 // Client wraps a URL and provides a method that implements endpoint.Endpoint.
1520 type Client struct {
16 client *http.Client
21 client HTTPClient
1722 method string
1823 tgt *url.URL
1924 enc EncodeRequestFunc
5358
5459 // SetClient sets the underlying HTTP client used for requests.
5560 // By default, http.DefaultClient is used.
56 func SetClient(client *http.Client) ClientOption {
61 func SetClient(client HTTPClient) ClientOption {
5762 return func(c *Client) { c.client = client }
5863 }
5964
00 package http_test
11
22 import (
3 "bytes"
34 "context"
45 "io"
56 "io/ioutil"
251252 }
252253 }
253254
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
254292 func mustParse(s string) *url.URL {
255293 u, err := url.Parse(s)
256294 if err != nil {
264302 }
265303
266304 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 }
1414
1515 // Client wraps a JSON RPC method and provides a method that implements endpoint.Endpoint.
1616 type Client struct {
17 client *http.Client
17 client httptransport.HTTPClient
1818
1919 // JSON RPC endpoint URL
2020 tgt *url.URL
8585
8686 // SetClient sets the underlying HTTP client used for requests.
8787 // By default, http.DefaultClient is used.
88 func SetClient(client *http.Client) ClientOption {
88 func SetClient(client httptransport.HTTPClient) ClientOption {
8989 return func(c *Client) { c.client = client }
9090 }
9191