From 2bf2dcd77b13f586f642f6b3c5619bea3ef19bc4 Mon Sep 17 00:00:00 2001 From: poliha Date: Thu, 2 May 2019 18:57:05 +0100 Subject: [PATCH] horizonclient: Set default HTTP client (#1228) * set default HTTP client * changes from review --- clients/horizonclient/client.go | 10 +++++++++- clients/horizonclient/main_test.go | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/clients/horizonclient/client.go b/clients/horizonclient/client.go index fa608d5b06..1f88e36c1f 100644 --- a/clients/horizonclient/client.go +++ b/clients/horizonclient/client.go @@ -53,7 +53,7 @@ func (c *Client) sendRequestURL(requestURL string, method string, a interface{}) return errors.Wrap(err, "error creating HTTP request") } c.setClientAppHeaders(req) - + c.setDefaultClient() if c.horizonTimeOut == 0 { c.horizonTimeOut = HorizonTimeOut } @@ -93,6 +93,7 @@ func (c *Client) stream( return errors.Wrap(err, "error creating HTTP request") } req.Header.Set("Accept", "text/event-stream") + c.setDefaultClient() // to do: confirm name and version c.setClientAppHeaders(req) @@ -203,6 +204,13 @@ func (c *Client) setClientAppHeaders(req *http.Request) { req.Header.Set("X-App-Version", c.AppVersion) } +// setDefaultClient sets the default HTTP client when none is provided. +func (c *Client) setDefaultClient() { + if c.HTTP == nil { + c.HTTP = http.DefaultClient + } +} + // fixHorizonURL strips all slashes(/) at the end of HorizonURL if any, then adds a single slash func (c *Client) fixHorizonURL() string { return strings.TrimRight(c.HorizonURL, "/") + "/" diff --git a/clients/horizonclient/main_test.go b/clients/horizonclient/main_test.go index a8b62d9760..4d6cd87125 100644 --- a/clients/horizonclient/main_test.go +++ b/clients/horizonclient/main_test.go @@ -2,6 +2,7 @@ package horizonclient import ( "fmt" + "net/http" "testing" "time" @@ -293,6 +294,17 @@ func ExampleClient_Fund() { fmt.Print(resp) } +func TestFixHTTP(t *testing.T) { + client := &Client{ + HorizonURL: "https://localhost/", + } + // No HTTP client is provided + assert.Nil(t, client.HTTP, "client HTTP is nil") + client.Root() + // When a request is made, default HTTP client is set + assert.IsType(t, client.HTTP, &http.Client{}) +} + func TestClientFund(t *testing.T) { hmock := httptest.NewClient() client := &Client{