Skip to content

Commit

Permalink
horizonclient: Set default HTTP client (#1228)
Browse files Browse the repository at this point in the history
* set default HTTP client

* changes from review
  • Loading branch information
poliha committed May 3, 2019
1 parent aa3f4c6 commit 970a6df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion clients/horizonclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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, "/") + "/"
Expand Down
12 changes: 12 additions & 0 deletions clients/horizonclient/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package horizonclient

import (
"fmt"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 970a6df

Please sign in to comment.