Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

horizonclient: Set default HTTP client #1228

Merged
merged 2 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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