Skip to content

Commit

Permalink
Merge pull request gojek#13 from kgrz/pass-duration-to-client
Browse files Browse the repository at this point in the history
Pass time.Duration to client init, instead of int
  • Loading branch information
rShetty authored Feb 20, 2018
2 parents 42bffd6 + db2e979 commit 4f21053
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion examples/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/gojektech/heimdall"
"github.com/pkg/errors"
Expand All @@ -14,7 +15,7 @@ const (
)

func httpClientUsage() error {
timeoutInMillis := 100
timeoutInMillis := 100 * time.Millisecond
httpClient := heimdall.NewHTTPClient(timeoutInMillis)
headers := http.Header{}
headers.Set("Content-Type", "application/json")
Expand Down
5 changes: 2 additions & 3 deletions http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ type httpClient struct {
}

// NewHTTPClient returns a new instance of HTTPClient
func NewHTTPClient(timeoutInMilliseconds int) Client {
httpTimeout := time.Duration(timeoutInMilliseconds) * time.Millisecond
func NewHTTPClient(timeout time.Duration) Client {
return &httpClient{
client: &http.Client{
Timeout: httpTimeout,
Timeout: timeout,
},

retryCount: defaultRetryCount,
Expand Down
23 changes: 12 additions & 11 deletions http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHTTPClientDoSuccess(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestHTTPClientDoSuccess(t *testing.T) {
}

func TestHTTPClientGetSuccess(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
Expand All @@ -67,7 +68,7 @@ func TestHTTPClientGetSuccess(t *testing.T) {
}

func TestHTTPClientPostSuccess(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

requestBodyString := `{ "name": "heimdall" }`

Expand Down Expand Up @@ -102,7 +103,7 @@ func TestHTTPClientPostSuccess(t *testing.T) {
}

func TestHTTPClientDeleteSuccess(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method)
Expand All @@ -128,7 +129,7 @@ func TestHTTPClientDeleteSuccess(t *testing.T) {
}

func TestHTTPClientPutSuccess(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

requestBodyString := `{ "name": "heimdall" }`

Expand Down Expand Up @@ -163,7 +164,7 @@ func TestHTTPClientPutSuccess(t *testing.T) {
}

func TestHTTPClientPatchSuccess(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

requestBodyString := `{ "name": "heimdall" }`

Expand Down Expand Up @@ -198,7 +199,7 @@ func TestHTTPClientPatchSuccess(t *testing.T) {
}

func TestHTTPClientGetRetriesOnFailure(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

count := 0

Expand Down Expand Up @@ -227,7 +228,7 @@ func TestHTTPClientGetRetriesOnFailure(t *testing.T) {
}

func TestHTTPClientGetReturnsAllErrorsIfRetriesFail(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

count := 0

Expand Down Expand Up @@ -255,7 +256,7 @@ func TestHTTPClientGetReturnsAllErrorsIfRetriesFail(t *testing.T) {
}

func TestHTTPClientGetReturnsNoErrorsIfRetrySucceeds(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

count := 0
countWhenCallSucceeds := 2
Expand Down Expand Up @@ -285,7 +286,7 @@ func TestHTTPClientGetReturnsNoErrorsIfRetrySucceeds(t *testing.T) {
}

func TestHTTPClientGetReturnsErrorOnClientCallFailure(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand All @@ -304,7 +305,7 @@ func TestHTTPClientGetReturnsErrorOnClientCallFailure(t *testing.T) {
}

func TestHTTPClientGetReturnsErrorOn5xxFailure(t *testing.T) {
client := NewHTTPClient(10)
client := NewHTTPClient(10 * time.Millisecond)

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
Expand Down

0 comments on commit 4f21053

Please sign in to comment.