-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsession_test.go
123 lines (117 loc) · 3.38 KB
/
session_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package session
import (
"context"
"net/http"
"runtime"
"strings"
"testing"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v10/pkg/edgegrid"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v10/pkg/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
tests := map[string]struct {
options []Option
expected *session
err string
}{
"no options provided, return default session": {
options: []Option{WithSigner(&edgegrid.Config{})},
expected: &session{
client: http.DefaultClient,
signer: &edgegrid.Config{},
log: log.Default(),
trace: false,
userAgent: "Akamai-Open-Edgegrid-golang/10.0.0 golang/" + strings.TrimPrefix(runtime.Version(), "go"),
},
},
"nil client provided, return error": {
options: []Option{WithClient(nil)},
err: "client should not be nil",
},
"nil log provided, return error": {
options: []Option{WithLog(nil)},
err: "logger should not be nil",
},
"empty user agent provided, return error": {
options: []Option{WithUserAgent("")},
err: "user agent should not be empty",
},
"nil signer provided, return error": {
options: []Option{WithSigner(nil)},
err: "signer should not be nil",
},
"invalid retries provided, return error": {
options: []Option{WithRetries(RetryConfig{
RetryMax: -1,
RetryWaitMin: -1,
RetryWaitMax: -2,
ExcludedEndpoints: []string{"f:o#[]o"},
})},
err: "retry configuration failed: maximum number of retries cannot be negative\n" +
"minimum retry wait time cannot be negative\n" +
"maximum retry wait time cannot be negative\n" +
"maximum retry wait time cannot be shorter than minimum retry wait time\n" +
"malformed exclude endpoint pattern: syntax error in pattern: f:o#[]o",
},
"with options provided": {
options: []Option{
WithSigner(&edgegrid.Config{}),
WithClient(&http.Client{Timeout: 500}),
WithLog(log.NOPLogger()),
WithUserAgent("test user agent"),
WithHTTPTracing(true)},
expected: &session{
client: &http.Client{
Timeout: 500,
},
signer: &edgegrid.Config{},
log: log.NOPLogger(),
trace: true,
userAgent: "test user agent",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res, err := New(test.options...)
if test.err != "" {
assert.EqualError(t, err, test.err)
} else {
require.NoError(t, err)
assert.Equal(t, test.expected, res)
}
})
}
}
func TestSession_Log(t *testing.T) {
tests := map[string]struct {
ctx context.Context
sessionLogger log.Interface
expected log.Interface
}{
"logger found in context, omit logger from session": {
ctx: ContextWithOptions(context.Background(), WithContextLog(log.NOPLogger())),
sessionLogger: log.Default(),
expected: log.NOPLogger(),
},
"logger not found in context, pick logger from session": {
ctx: context.Background(),
sessionLogger: log.NOPLogger(),
expected: log.NOPLogger(),
},
"logger not found in context or session": {
ctx: context.Background(),
sessionLogger: nil,
expected: log.Default(),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
s := session{log: test.sessionLogger}
res := s.Log(test.ctx)
assert.Equal(t, test.expected, res)
})
}
}