Skip to content

Commit

Permalink
Merge pull request #7 from arpit20adlakha/fixing_albatross_client_bod…
Browse files Browse the repository at this point in the history
…y_nil

Fix sendWithRetry method to support body param as nil
  • Loading branch information
buildAI authored Sep 16, 2021
2 parents f05d97a + aee9d4b commit eb9640b
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*.dll
*.so
*.dylib

.idea/
# Test binary, built with `go test -c`
*.test

Expand Down
10 changes: 7 additions & 3 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ func (c *Client) getBackoffForRetry(count int) time.Duration {

func (c *Client) sendWithRetry(url string, method string, body io.Reader) (*http.Response, error) {
// reqBytes is used to populate the body for the request for each retry,
reqBytes, err := ioutil.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("Error reading the request body: %s", err)
var reqBytes []byte = nil

if body != nil {
var err error = nil
if reqBytes, err = ioutil.ReadAll(body); err != nil {
return nil, fmt.Errorf("Error reading the request body: %s", err)
}
}

var retryError error
Expand Down
139 changes: 139 additions & 0 deletions httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,145 @@ func TestHttpClientSendOnSuccess(t *testing.T) {
assert.Equal(t, resp.StatusCode, 200)
}

func TestHttpClientSendWithRetry(t *testing.T) {

t.Run("When body is nil and call returns with 200", func(t *testing.T) {
mc := new(mockClient)
response := &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte("ab"))),
}
mc.On("Do", mock.Anything).Return(response, nil)
httpClient := &Client{
client: mc,
retry: &config.Retry{
RetryCount: 3,
Backoff: 2 * time.Second,
},
logger: &logger.DefaultLogger{},
}

resp, err := httpClient.sendWithRetry("http://localhost:444", "GET", nil)

assert.NoError(t, err)
assert.Equal(t, resp, response)
})

t.Run("When body is nil and call returns with 500", func(t *testing.T) {
mc := new(mockClient)
response := &http.Response{
Status: "500 Internal Server Error",
StatusCode: 500,
Body: ioutil.NopCloser(bytes.NewReader([]byte("abcde"))),
}

mc.On("Do", mock.Anything).Return(response, nil)
client := &Client{
client: mc,
retry: &config.Retry{
RetryCount: 3,
Backoff: 2 * time.Second,
},
logger: &logger.DefaultLogger{},
}

resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)

assert.Nil(t, err)
assert.Equal(t, resp, response)
})

t.Run("When body is nil and call returns with 400", func(t *testing.T) {
mc := new(mockClient)
response := &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewReader([]byte("abcde"))),
}

mc.On("Do", mock.Anything).Return(response, nil)
client := &Client{
client: mc,
retry: &config.Retry{
RetryCount: 3,
Backoff: 2 * time.Second,
},
logger: &logger.DefaultLogger{},
}

resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)

assert.Nil(t, err)
assert.Equal(t, resp, response)
})

t.Run("When body is nil and call returns with Network Error On retries", func(t *testing.T) {
mc := new(mockClient)

mc.On("Do", mock.Anything).Return(&http.Response{}, errors.New("Network Error"))
client := &Client{
client: mc,
retry: &config.Retry{
RetryCount: 3,
Backoff: 500 * time.Millisecond,
},
logger: &logger.DefaultLogger{},
}

resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)

assert.Error(t, err)
assert.EqualError(t, err, "Max retries exceeded: Network Error")
assert.Nil(t, resp)
})

t.Run("When body is nil and call returns with Network Error With zero retries", func(t *testing.T) {
mc := new(mockClient)

mc.On("Do", mock.Anything).Return(&http.Response{}, errors.New("Network Error"))
client := &Client{
client: mc,
retry: &config.Retry{
RetryCount: 0,
},
logger: &logger.DefaultLogger{},
}

resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)

assert.Error(t, err)
assert.EqualError(t, err, "Max retries exceeded: Network Error")
assert.Nil(t, resp)
})

t.Run("When body is nil and call recovers after retries", func(t *testing.T) {
mc := new(mockClient)
response := &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte("abcde"))),
}

mc.On("Do", mock.Anything).Return(&http.Response{}, &url.Error{}).Once()
mc.On("Do", mock.Anything).Return(&http.Response{}, &url.Error{}).Once()
mc.On("Do", mock.Anything).Return(response, nil).Once()
client := &Client{
client: mc,
retry: &config.Retry{
RetryCount: 3,
Backoff: 500 * time.Millisecond,
},
logger: &logger.DefaultLogger{},
}

resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)

assert.NoError(t, err)
assert.Equal(t, resp, response)
})
}

func TestHttpClientSendOnServerError(t *testing.T) {
mc := new(mockClient)
response := &http.Response{
Expand Down

0 comments on commit eb9640b

Please sign in to comment.