-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ok to pass nil headers, test, don't retry 404 (#5)
- Loading branch information
1 parent
0ff2e47
commit 1e7d293
Showing
4 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package shared | ||
|
||
import ( | ||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func Test_httpClientWrapper_ExecuteRequest_failsTooManyRetries(t *testing.T) { | ||
const baseURL = "http://test.com" | ||
hcw, _ := NewHTTPClientWrapper(baseURL, "", 1, nil, true) | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
path := "/api/vehicle/v2/makes" | ||
httpmock.RegisterResponder(http.MethodGet, baseURL+path, httpmock.NewStringResponder(409, "error: too many requests")) | ||
response, err := hcw.ExecuteRequest(path, "GET", nil) | ||
countInfo := httpmock.GetCallCountInfo() | ||
c := countInfo["GET "+baseURL+path] | ||
|
||
assert.Equal(t, 5, c, "expected five retries") | ||
assert.Error(t, err, "expected error") | ||
assert.Contains(t, err.Error(), "All attempts fail") | ||
assert.Equal(t, 409, response.StatusCode) | ||
} | ||
|
||
func Test_httpClientWrapper_ExecuteRequest_doesNotRetryCertainStatusCodes(t *testing.T) { | ||
const baseURL = "http://test.com" | ||
hcw, _ := NewHTTPClientWrapper(baseURL, "", 1, nil, true) | ||
|
||
tests := []struct { | ||
name string | ||
statusCode int | ||
}{ | ||
{ | ||
name: "don't retry 400", | ||
statusCode: 400, | ||
}, | ||
{ | ||
name: "don't retry 401", | ||
statusCode: 401, | ||
}, | ||
{ | ||
name: "don't retry 404", | ||
statusCode: 404, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
path := "/api/vehicle/v2/makes" | ||
httpmock.RegisterResponder(http.MethodGet, baseURL+path, httpmock.NewStringResponder(test.statusCode, "error: whatever")) | ||
response, err := hcw.ExecuteRequest(path, "GET", nil) | ||
countInfo := httpmock.GetCallCountInfo() | ||
c := countInfo["GET "+baseURL+path] | ||
|
||
assert.Equal(t, 1, c, "expected single call") | ||
assert.Equal(t, test.statusCode, response.StatusCode) | ||
assert.Error(t, err, "expected error") | ||
}) | ||
} | ||
} | ||
|
||
func Test(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
}{ | ||
// TODO: test cases | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
||
}) | ||
} | ||
} | ||
|