Skip to content

Commit

Permalink
fixing existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gavriel-hc committed Apr 11, 2022
1 parent ff6d014 commit af0a5a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ var (
// scheme specified in the URL is invalid. This error isn't typed
// specifically so we resort to matching on the error string.
schemeErrorRe = regexp.MustCompile(`unsupported protocol scheme`)

// A regular expression to match the error returned by net/http when the
// TLS certificate is not trusted. This error isn't typed
// specifically so we resort to matching on the error string.
notTrustedErrorRe = regexp.MustCompile(`certificate is not trusted`)
)

// ReaderFunc is the type of function that can be given natively to NewRequest
Expand Down Expand Up @@ -445,6 +450,9 @@ func baseRetryPolicy(resp *http.Response, err error) (bool, error) {
}

// Don't retry if the error was due to TLS cert verification failure.
if notTrustedErrorRe.MatchString(v.Error()) {
return false, v
}
if _, ok := v.Err.(x509.UnknownAuthorityError); ok {
return false, v
}
Expand Down
13 changes: 9 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ func testClientDo(t *testing.T, body interface{}) {
// Send the request
var resp *http.Response
doneCh := make(chan struct{})
errCh := make(chan error, 1)
go func() {
defer close(doneCh)
defer close(errCh)
var err error
resp, err = client.Do(req)
if err != nil {
t.Fatalf("err: %v", err)
}
errCh <- err
}()

select {
Expand Down Expand Up @@ -247,6 +247,11 @@ func testClientDo(t *testing.T, body interface{}) {
if retryCount < 0 {
t.Fatal("request log hook was not called")
}

err = <-errCh
if err != nil {
t.Fatalf("err: %v", err)
}
}

func TestClient_Do_fails(t *testing.T) {
Expand Down Expand Up @@ -598,7 +603,7 @@ func TestClient_DefaultRetryPolicy_TLS(t *testing.T) {

func TestClient_DefaultRetryPolicy_redirects(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
}))
defer ts.Close()

Expand Down
8 changes: 4 additions & 4 deletions roundtripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func TestRoundTripper_TransportFailureErrorHandling(t *testing.T) {

expectedError := &url.Error{
Op: "Get",
URL: "http://this-url-does-not-exist-ed2fb.com/",
URL: "http://asdfsa.com/",
Err: &net.OpError{
Op: "dial",
Net: "tcp",
Err: &net.DNSError{
Name: "this-url-does-not-exist-ed2fb.com",
Name: "asdfsa.com",
Err: "no such host",
IsNotFound: true,
},
Expand All @@ -121,10 +121,10 @@ func TestRoundTripper_TransportFailureErrorHandling(t *testing.T) {

// Get the standard client and execute the request.
client := retryClient.StandardClient()
_, err := client.Get("http://this-url-does-not-exist-ed2fb.com/")
_, err := client.Get("http://asdfsa.com/")

// assert expectations
if !reflect.DeepEqual(normalizeError(err), expectedError) {
if !reflect.DeepEqual(expectedError, normalizeError(err)) {
t.Fatalf("expected %q, got %q", expectedError, err)
}
}
Expand Down

0 comments on commit af0a5a3

Please sign in to comment.