Skip to content

Commit eb9640b

Browse files
authored
Merge pull request #7 from arpit20adlakha/fixing_albatross_client_body_nil
Fix sendWithRetry method to support body param as nil
2 parents f05d97a + aee9d4b commit eb9640b

File tree

3 files changed

+147
-4
lines changed

3 files changed

+147
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7-
7+
.idea/
88
# Test binary, built with `go test -c`
99
*.test
1010

httpclient/client.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ func (c *Client) getBackoffForRetry(count int) time.Duration {
8383

8484
func (c *Client) sendWithRetry(url string, method string, body io.Reader) (*http.Response, error) {
8585
// reqBytes is used to populate the body for the request for each retry,
86-
reqBytes, err := ioutil.ReadAll(body)
87-
if err != nil {
88-
return nil, fmt.Errorf("Error reading the request body: %s", err)
86+
var reqBytes []byte = nil
87+
88+
if body != nil {
89+
var err error = nil
90+
if reqBytes, err = ioutil.ReadAll(body); err != nil {
91+
return nil, fmt.Errorf("Error reading the request body: %s", err)
92+
}
8993
}
9094

9195
var retryError error

httpclient/client_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,145 @@ func TestHttpClientSendOnSuccess(t *testing.T) {
4949
assert.Equal(t, resp.StatusCode, 200)
5050
}
5151

52+
func TestHttpClientSendWithRetry(t *testing.T) {
53+
54+
t.Run("When body is nil and call returns with 200", func(t *testing.T) {
55+
mc := new(mockClient)
56+
response := &http.Response{
57+
Status: "200 OK",
58+
StatusCode: 200,
59+
Body: ioutil.NopCloser(bytes.NewReader([]byte("ab"))),
60+
}
61+
mc.On("Do", mock.Anything).Return(response, nil)
62+
httpClient := &Client{
63+
client: mc,
64+
retry: &config.Retry{
65+
RetryCount: 3,
66+
Backoff: 2 * time.Second,
67+
},
68+
logger: &logger.DefaultLogger{},
69+
}
70+
71+
resp, err := httpClient.sendWithRetry("http://localhost:444", "GET", nil)
72+
73+
assert.NoError(t, err)
74+
assert.Equal(t, resp, response)
75+
})
76+
77+
t.Run("When body is nil and call returns with 500", func(t *testing.T) {
78+
mc := new(mockClient)
79+
response := &http.Response{
80+
Status: "500 Internal Server Error",
81+
StatusCode: 500,
82+
Body: ioutil.NopCloser(bytes.NewReader([]byte("abcde"))),
83+
}
84+
85+
mc.On("Do", mock.Anything).Return(response, nil)
86+
client := &Client{
87+
client: mc,
88+
retry: &config.Retry{
89+
RetryCount: 3,
90+
Backoff: 2 * time.Second,
91+
},
92+
logger: &logger.DefaultLogger{},
93+
}
94+
95+
resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)
96+
97+
assert.Nil(t, err)
98+
assert.Equal(t, resp, response)
99+
})
100+
101+
t.Run("When body is nil and call returns with 400", func(t *testing.T) {
102+
mc := new(mockClient)
103+
response := &http.Response{
104+
Status: "400 Bad Request",
105+
StatusCode: 400,
106+
Body: ioutil.NopCloser(bytes.NewReader([]byte("abcde"))),
107+
}
108+
109+
mc.On("Do", mock.Anything).Return(response, nil)
110+
client := &Client{
111+
client: mc,
112+
retry: &config.Retry{
113+
RetryCount: 3,
114+
Backoff: 2 * time.Second,
115+
},
116+
logger: &logger.DefaultLogger{},
117+
}
118+
119+
resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)
120+
121+
assert.Nil(t, err)
122+
assert.Equal(t, resp, response)
123+
})
124+
125+
t.Run("When body is nil and call returns with Network Error On retries", func(t *testing.T) {
126+
mc := new(mockClient)
127+
128+
mc.On("Do", mock.Anything).Return(&http.Response{}, errors.New("Network Error"))
129+
client := &Client{
130+
client: mc,
131+
retry: &config.Retry{
132+
RetryCount: 3,
133+
Backoff: 500 * time.Millisecond,
134+
},
135+
logger: &logger.DefaultLogger{},
136+
}
137+
138+
resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)
139+
140+
assert.Error(t, err)
141+
assert.EqualError(t, err, "Max retries exceeded: Network Error")
142+
assert.Nil(t, resp)
143+
})
144+
145+
t.Run("When body is nil and call returns with Network Error With zero retries", func(t *testing.T) {
146+
mc := new(mockClient)
147+
148+
mc.On("Do", mock.Anything).Return(&http.Response{}, errors.New("Network Error"))
149+
client := &Client{
150+
client: mc,
151+
retry: &config.Retry{
152+
RetryCount: 0,
153+
},
154+
logger: &logger.DefaultLogger{},
155+
}
156+
157+
resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)
158+
159+
assert.Error(t, err)
160+
assert.EqualError(t, err, "Max retries exceeded: Network Error")
161+
assert.Nil(t, resp)
162+
})
163+
164+
t.Run("When body is nil and call recovers after retries", func(t *testing.T) {
165+
mc := new(mockClient)
166+
response := &http.Response{
167+
Status: "200 OK",
168+
StatusCode: 200,
169+
Body: ioutil.NopCloser(bytes.NewReader([]byte("abcde"))),
170+
}
171+
172+
mc.On("Do", mock.Anything).Return(&http.Response{}, &url.Error{}).Once()
173+
mc.On("Do", mock.Anything).Return(&http.Response{}, &url.Error{}).Once()
174+
mc.On("Do", mock.Anything).Return(response, nil).Once()
175+
client := &Client{
176+
client: mc,
177+
retry: &config.Retry{
178+
RetryCount: 3,
179+
Backoff: 500 * time.Millisecond,
180+
},
181+
logger: &logger.DefaultLogger{},
182+
}
183+
184+
resp, err := client.sendWithRetry("http://localhost:444", "GET", nil)
185+
186+
assert.NoError(t, err)
187+
assert.Equal(t, resp, response)
188+
})
189+
}
190+
52191
func TestHttpClientSendOnServerError(t *testing.T) {
53192
mc := new(mockClient)
54193
response := &http.Response{

0 commit comments

Comments
 (0)