Skip to content

Commit af0a5a3

Browse files
committed
fixing existing tests
1 parent ff6d014 commit af0a5a3

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

client.go

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ var (
6969
// scheme specified in the URL is invalid. This error isn't typed
7070
// specifically so we resort to matching on the error string.
7171
schemeErrorRe = regexp.MustCompile(`unsupported protocol scheme`)
72+
73+
// A regular expression to match the error returned by net/http when the
74+
// TLS certificate is not trusted. This error isn't typed
75+
// specifically so we resort to matching on the error string.
76+
notTrustedErrorRe = regexp.MustCompile(`certificate is not trusted`)
7277
)
7378

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

447452
// Don't retry if the error was due to TLS cert verification failure.
453+
if notTrustedErrorRe.MatchString(v.Error()) {
454+
return false, v
455+
}
448456
if _, ok := v.Err.(x509.UnknownAuthorityError); ok {
449457
return false, v
450458
}

client_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ func testClientDo(t *testing.T, body interface{}) {
167167
// Send the request
168168
var resp *http.Response
169169
doneCh := make(chan struct{})
170+
errCh := make(chan error, 1)
170171
go func() {
171172
defer close(doneCh)
173+
defer close(errCh)
172174
var err error
173175
resp, err = client.Do(req)
174-
if err != nil {
175-
t.Fatalf("err: %v", err)
176-
}
176+
errCh <- err
177177
}()
178178

179179
select {
@@ -247,6 +247,11 @@ func testClientDo(t *testing.T, body interface{}) {
247247
if retryCount < 0 {
248248
t.Fatal("request log hook was not called")
249249
}
250+
251+
err = <-errCh
252+
if err != nil {
253+
t.Fatalf("err: %v", err)
254+
}
250255
}
251256

252257
func TestClient_Do_fails(t *testing.T) {
@@ -598,7 +603,7 @@ func TestClient_DefaultRetryPolicy_TLS(t *testing.T) {
598603

599604
func TestClient_DefaultRetryPolicy_redirects(t *testing.T) {
600605
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
601-
http.Redirect(w, r, "/", 302)
606+
http.Redirect(w, r, "/", http.StatusFound)
602607
}))
603608
defer ts.Close()
604609

roundtripper_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ func TestRoundTripper_TransportFailureErrorHandling(t *testing.T) {
107107

108108
expectedError := &url.Error{
109109
Op: "Get",
110-
URL: "http://this-url-does-not-exist-ed2fb.com/",
110+
URL: "http://asdfsa.com/",
111111
Err: &net.OpError{
112112
Op: "dial",
113113
Net: "tcp",
114114
Err: &net.DNSError{
115-
Name: "this-url-does-not-exist-ed2fb.com",
115+
Name: "asdfsa.com",
116116
Err: "no such host",
117117
IsNotFound: true,
118118
},
@@ -121,10 +121,10 @@ func TestRoundTripper_TransportFailureErrorHandling(t *testing.T) {
121121

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

126126
// assert expectations
127-
if !reflect.DeepEqual(normalizeError(err), expectedError) {
127+
if !reflect.DeepEqual(expectedError, normalizeError(err)) {
128128
t.Fatalf("expected %q, got %q", expectedError, err)
129129
}
130130
}

0 commit comments

Comments
 (0)