-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathretry_test.go
More file actions
103 lines (94 loc) · 2.58 KB
/
retry_test.go
File metadata and controls
103 lines (94 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package acpclient_test
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
acpclient "github.com/cloudentity/acp-client-go"
"github.com/cloudentity/acp-client-go/mocks"
"golang.org/x/oauth2/clientcredentials"
)
var ccConfig = clientcredentials.Config{
TokenURL: "http://example.com/oauth2/token",
}
func TestAuthenticatorRoundTrip(t *testing.T) {
tests := []struct {
name string
mock http.RoundTripper
requestBody string
expectedStatus int
expectedError bool
wantNilResp bool
}{
{
name: "successful request without retry",
mock: &mocks.MockAuthTransport{},
requestBody: "",
expectedStatus: http.StatusOK,
expectedError: false,
},
{
name: "successful request with body without retry",
mock: &mocks.MockAuthTransport{},
requestBody: "test body",
expectedStatus: http.StatusOK,
expectedError: false,
},
{
name: "unauthorized with invalid token triggers retry and succeeds",
mock: &mocks.MockAuthTransport{},
requestBody: "test body for retry",
expectedStatus: http.StatusOK,
expectedError: false,
},
{
name: "token renewal failure",
mock: &mocks.MockAuthTransport{FailRenewal: true},
requestBody: "test body",
expectedStatus: 0,
expectedError: true,
wantNilResp: true,
},
{
name: "transport error",
mock: &mocks.MockTransport{Response: nil, Error: errors.New("transport failure")},
requestBody: "",
expectedStatus: 0,
expectedError: true,
wantNilResp: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &http.Client{Transport: tt.mock}
authClient := acpclient.NewAuthenticator(ccConfig, client)
var reqBody io.ReadCloser
if tt.requestBody != "" {
reqBody = io.NopCloser(bytes.NewBufferString(tt.requestBody))
}
req, err := http.NewRequest("GET", "http://example.com/api/test", reqBody)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
res, err := authClient.Transport.RoundTrip(req)
if tt.expectedError {
if err == nil {
t.Errorf("Expected error, got nil")
}
if tt.wantNilResp && res != nil {
t.Errorf("Expected nil response, got %+v", res)
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if res == nil {
t.Error("Expected response, got nil")
} else if res.StatusCode != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, res.StatusCode)
}
}
})
}
}