-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvary_test.go
More file actions
99 lines (96 loc) · 2.4 KB
/
vary_test.go
File metadata and controls
99 lines (96 loc) · 2.4 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
package ghtransport
import (
"net/http"
"reflect"
"slices"
"testing"
)
func Test_identicalVary(t *testing.T) {
tests := map[string]struct {
Request *http.Request
Cached *http.Response
Expected bool
}{
"true": {
Request: &http.Request{
Header: http.Header{
"Accept": []string{"application/json"},
},
},
Cached: &http.Response{
Header: http.Header{
VaryPrefix + "Accept": []string{"application/json"},
"Vary": []string{"Accept"},
},
},
Expected: true,
},
"false": {
Request: &http.Request{
Header: http.Header{
"Accept": []string{"application/json"},
},
},
Cached: &http.Response{
Header: http.Header{
VaryPrefix + "Accept": []string{"application/xml"},
"Vary": []string{"Accept"},
},
},
Expected: false,
},
"hashed": {
Request: &http.Request{
Header: http.Header{
"Accept": []string{"application/json"},
"Authorization": []string{"Bearer hunter2"},
},
},
Cached: &http.Response{
Header: http.Header{
VaryPrefix + "Accept": []string{"application/json"},
VaryPrefix + "Authorization": []string{"9S+9MrKzuG/4jvbEkGKChfSCrxXdyylUH5S89Saj9sc="},
"Vary": []string{"Authorization, Accept"},
},
},
Expected: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if got := identicalVary(test.Request, test.Cached); got != test.Expected {
t.Errorf("identicalVary(%v, %v) = %v, want %v", test.Request, test.Cached, got, test.Expected)
}
})
}
}
func Test_parseVary(t *testing.T) {
tests := map[string]struct {
Headers http.Header
Expected []string
}{
"empty": {
Headers: http.Header{},
Expected: nil,
},
"single": {
Headers: http.Header{"Vary": []string{"Accept"}},
Expected: []string{"Accept"},
},
"comma": {
Headers: http.Header{"Vary": []string{"Accept, Authorization"}},
Expected: []string{"Accept", "Authorization"},
},
"multiple": {
Headers: http.Header{"Vary": []string{"Accept, Authorization", "Cookie"}},
Expected: []string{"Accept", "Authorization", "Cookie"},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if got := slices.Collect(parseVary(test.Headers)); !reflect.DeepEqual(got, test.Expected) {
t.Errorf("ParseVary(%v) = %v, want %v", test.Headers, got, test.Expected)
}
})
}
}