-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsession_test.go
123 lines (117 loc) · 2.8 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/v9/pkg/edgegrid"
"github.com/apex/log"
"github.com/apex/log/handlers/discard"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
tests := map[string]struct {
client *http.Client
log log.Interface
userAgent string
httpTracing bool
expected *session
}{
"no options provided, return default session": {
expected: &session{
client: http.DefaultClient,
signer: &edgegrid.Config{},
log: log.Log,
trace: false,
userAgent: "Akamai-Open-Edgegrid-golang/9.0.0 golang/" + strings.TrimPrefix(runtime.Version(), "go"),
},
},
"with options provided": {
client: &http.Client{
Timeout: 500,
},
log: log.Log,
userAgent: "test user agent",
httpTracing: true,
expected: &session{
client: &http.Client{
Timeout: 500,
},
signer: &edgegrid.Config{},
log: log.Log,
trace: true,
userAgent: "test user agent",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
cfg := &edgegrid.Config{}
options := []Option{WithSigner(cfg)}
if test.client != nil {
options = append(options, WithClient(test.client))
}
if test.log != nil {
options = append(options, WithLog(test.log))
}
if test.userAgent != "" {
options = append(options, WithUserAgent(test.userAgent))
}
if test.httpTracing {
options = append(options, WithHTTPTracing(test.httpTracing))
}
res, err := New(options...)
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.Logger
}{
"logger found in context, omit logger from session": {
ctx: ContextWithOptions(context.Background(), WithContextLog(&log.Logger{
Handler: discard.New(),
Level: 1,
})),
sessionLogger: &log.Logger{
Handler: discard.New(),
Level: 2,
},
expected: &log.Logger{
Handler: discard.New(),
Level: 1,
},
},
"logger not found in context, pick logger from session": {
ctx: context.Background(),
sessionLogger: &log.Logger{
Handler: discard.New(),
Level: 2,
},
expected: &log.Logger{
Handler: discard.New(),
Level: 2,
},
},
"logger not found in context or session": {
ctx: context.Background(),
sessionLogger: nil,
expected: &log.Logger{
Handler: discard.New(),
},
},
}
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)
})
}
}