-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_test.go
171 lines (159 loc) · 3.53 KB
/
option_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package gracefulhttp
import (
"crypto/tls"
"reflect"
"testing"
"time"
)
func TestWithCloudflareTimeouts(t *testing.T) {
type timeouts struct {
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
}
tests := []struct {
name string
want timeouts
}{
{
name: "set cloudflare timeouts",
want: timeouts{
ReadTimeout: defaultReadTimeout,
ReadHeaderTimeout: defaultReadHeaderTimeout,
WriteTimeout: defaultWriteTimeout,
IdleTimeout: defaultIdleTimeout,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := GracefulServer{}
opt := WithCloudflareTimeouts()
opt(&s)
got := timeouts{
ReadTimeout: s.ReadTimeout,
ReadHeaderTimeout: s.ReadHeaderTimeout,
WriteTimeout: s.WriteTimeout,
IdleTimeout: s.IdleTimeout,
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithCloudflareTimeouts() = %v, want %v", got, tt.want)
}
})
}
}
func TestWithShutdownTimeout(t *testing.T) {
type args struct {
duration time.Duration
}
tests := []struct {
name string
args args
want time.Duration
}{
{
name: "set timeout to a positive value",
args: args{
duration: 7 * time.Second,
},
want: 7 * time.Second,
},
{
name: "set timeout to zero",
args: args{
duration: 0 * time.Second,
},
want: defaultGracefulTimeout,
},
{
name: "set a negative timeout",
args: args{
duration: -5 * time.Second,
},
want: defaultGracefulTimeout,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := GracefulServer{}
opt := WithShutdownTimeout(tt.args.duration)
opt(&s)
if got := s.gracefulTimeout; !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithShutdownTimeout() = %v, want %v", got, tt.want)
}
})
}
}
func TestWithCloudflareTLSConfig(t *testing.T) {
type config struct {
MinVersion uint16
CurvePreferences []tls.CurveID
CipherSuites []uint16
}
tests := []struct {
name string
want config
}{
{
name: "set cloudflare tls config",
want: config{
MinVersion: defaultTLSMinVersion,
CurvePreferences: defaultTLSCurvePreferences,
CipherSuites: defaultTLSCipherSuites,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := GracefulServer{}
opt := WithCloudflareTLSConfig()
opt(&s)
got := config{
MinVersion: s.TLSConfig.MinVersion,
CurvePreferences: s.TLSConfig.CurvePreferences,
CipherSuites: s.TLSConfig.CipherSuites,
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithCloudflareTimeouts() = %v, want %v", got, tt.want)
}
})
}
}
func TestWithTLSConfig(t *testing.T) {
type args struct {
config *tls.Config
}
tests := []struct {
name string
args args
want *tls.Config
}{
{
name: "pass tls config",
args: args{
config: &tls.Config{
MinVersion: defaultTLSMinVersion,
CurvePreferences: defaultTLSCurvePreferences,
CipherSuites: defaultTLSCipherSuites,
},
},
want: &tls.Config{
MinVersion: defaultTLSMinVersion,
CurvePreferences: defaultTLSCurvePreferences,
CipherSuites: defaultTLSCipherSuites,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := GracefulServer{}
opt := WithTLSConfig(tt.args.config)
opt(&s)
got := s.TLSConfig
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithCloudflareTimeouts() = %v, want %v", got, tt.want)
}
})
}
}