Skip to content

Commit 55abbbd

Browse files
committed
Only retry on error
1 parent 040d54a commit 55abbbd

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

oauth2/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (o *Transport) RoundTrip(httpRequest *http.Request) (*http.Response, error)
7474
requestFreshToken = true
7575
continue
7676
}
77+
break
7778
}
7879
return httpResponse, err
7980
}

oauth2/client_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func TestClient_RoundTrip(t *testing.T) {
1515
t.Run("Resource Server requires authentication", func(t *testing.T) {
1616
t.Run("GET request", func(t *testing.T) {
1717
mux := http.NewServeMux()
18+
invocations := 0
1819
mux.HandleFunc("GET /resource", func(w http.ResponseWriter, r *http.Request) {
20+
invocations++
1921
if r.Header.Get("Authorization") != "Bearer token" {
2022
w.WriteHeader(http.StatusUnauthorized)
2123
_, _ = w.Write([]byte("Unauthorized"))
@@ -36,12 +38,15 @@ func TestClient_RoundTrip(t *testing.T) {
3638

3739
require.NoError(t, err)
3840
require.Equal(t, http.StatusOK, httpResponse.StatusCode)
41+
require.Equal(t, 1, invocations)
3942
})
4043
t.Run("401 Unauthorized retries HTTP request with fresh token", func(t *testing.T) {
4144
t.Run("GET request", func(t *testing.T) {
4245
mux := http.NewServeMux()
4346
var capturedBody []byte
47+
invocations := 0
4448
mux.HandleFunc("GET /resource", func(w http.ResponseWriter, r *http.Request) {
49+
invocations++
4550
// Reject the first token
4651
if r.Header.Get("Authorization") != "Bearer token2" {
4752
w.WriteHeader(http.StatusUnauthorized)
@@ -67,6 +72,7 @@ func TestClient_RoundTrip(t *testing.T) {
6772
require.NoError(t, err)
6873
require.Equal(t, http.StatusOK, httpResponse.StatusCode)
6974
require.Empty(t, string(capturedBody))
75+
require.Equal(t, 2, invocations)
7076
})
7177
t.Run("max. number of retries reached", func(t *testing.T) {
7278
mux := http.NewServeMux()

0 commit comments

Comments
 (0)