-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomponent_identifier_test.go
253 lines (217 loc) · 5.41 KB
/
component_identifier_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package httpsig
import (
"testing"
"github.com/dunglas/httpsfv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestQuoteIdentifierName(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
uc string
given string
expect string
}{
{uc: "name already quoted", given: `"@method";req,key=val`, expect: `"@method";req,key=val`},
{uc: "name not quoted, without parameter", given: `@method`, expect: `"@method"`},
{uc: "name not quoted, with parameter", given: `@method;req,key=val`, expect: `"@method";req,key=val`},
} {
t.Run(tc.uc, func(t *testing.T) {
res := quoteIdentifierName(tc.given)
assert.Equal(t, tc.expect, res)
})
}
}
func TestNormalizeParams(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
uc string
given *httpsfv.Params
expect *httpsfv.Params
}{
{
uc: "empty params",
given: httpsfv.NewParams(),
expect: httpsfv.NewParams(),
},
{
uc: "byte array params",
given: func() *httpsfv.Params {
params := httpsfv.NewParams()
params.Add("test", []byte("test"))
return params
}(),
expect: func() *httpsfv.Params {
params := httpsfv.NewParams()
params.Add("test", "dGVzdA==")
return params
}(),
},
{
uc: "token params",
given: func() *httpsfv.Params {
params := httpsfv.NewParams()
params.Add("test", httpsfv.Token("token"))
return params
}(),
expect: func() *httpsfv.Params {
params := httpsfv.NewParams()
params.Add("test", "token")
return params
}(),
},
{
uc: "bare item params",
given: func() *httpsfv.Params {
params := httpsfv.NewParams()
params.Add("test", 1)
return params
}(),
expect: func() *httpsfv.Params {
params := httpsfv.NewParams()
params.Add("test", 1)
return params
}(),
},
} {
t.Run(tc.uc, func(t *testing.T) {
res := normaliseParams(tc.given)
assert.Equal(t, tc.given.Names(), res.Names())
for _, name := range res.Names() {
resVal, ok := res.Get(name)
assert.True(t, ok)
expVal, ok := tc.expect.Get(name)
assert.True(t, ok)
assert.Equal(t, expVal, resVal)
}
})
}
}
func TestToComponentIdentifiers(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
uc string
identifiers []string
assert func(t *testing.T, ci []*componentIdentifier, err error)
}{
{
uc: "invalid identifier definition",
identifiers: []string{"foo;#"},
assert: func(t *testing.T, _ []*componentIdentifier, err error) {
t.Helper()
require.Error(t, err)
require.ErrorIs(t, err, ErrInvalidComponentIdentifier)
},
},
{
uc: "unknown identifier",
identifiers: []string{"@foo"},
assert: func(t *testing.T, _ []*componentIdentifier, err error) {
t.Helper()
require.Error(t, err)
require.ErrorIs(t, err, ErrUnsupportedComponentIdentifier)
},
},
{
uc: "valid",
identifiers: []string{"@method;foo=bar", "@status"},
assert: func(t *testing.T, ci []*componentIdentifier, err error) {
t.Helper()
require.NoError(t, err)
require.Len(t, ci, 2)
assert.Equal(t, "@method", ci[0].Value.(string))
res, ok := ci[0].Params.Get("foo")
require.True(t, ok)
require.Equal(t, "bar", res.(string))
assert.NotNil(t, ci[0].c)
assert.Equal(t, "@status", ci[1].Value.(string))
assert.Empty(t, ci[1].Params.Names())
assert.NotNil(t, ci[1].c)
},
},
} {
t.Run(tc.uc, func(t *testing.T) {
res, err := toComponentIdentifiers(tc.identifiers)
tc.assert(t, res, err)
})
}
}
func TestNewComponentIdentifier(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
uc string
item httpsfv.Item
assert func(t *testing.T, ci *componentIdentifier, err error)
}{
{
uc: "invalid",
item: httpsfv.NewItem("@foo"),
assert: func(t *testing.T, _ *componentIdentifier, err error) {
t.Helper()
require.Error(t, err)
require.ErrorIs(t, err, ErrUnsupportedComponentIdentifier)
},
},
{
uc: "valid",
item: func() httpsfv.Item {
item := httpsfv.NewItem("@method")
item.Params.Add("foo", []byte("bar"))
return item
}(),
assert: func(t *testing.T, ci *componentIdentifier, err error) {
t.Helper()
require.NoError(t, err)
assert.NotNil(t, ci.c)
val, ok := ci.Params.Get("foo")
require.True(t, ok)
assert.Equal(t, "YmFy", val)
},
},
} {
t.Run(tc.uc, func(t *testing.T) {
ci, err := newComponentIdentifier(tc.item)
tc.assert(t, ci, err)
})
}
}
func TestComponentIdentifierCreateComponent(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
uc string
item httpsfv.Item
msg *Message
err error
}{
{
uc: "with error during canonicalization",
item: func() httpsfv.Item {
item := httpsfv.NewItem("@method")
item.Params.Add("req", true)
return item
}(),
msg: &Message{Method: "GET", IsRequest: true},
err: ErrCanonicalization,
},
{
uc: "with error during canonicalization",
item: httpsfv.NewItem("@method"),
msg: &Message{Method: "POST", IsRequest: true},
},
} {
t.Run(tc.uc, func(t *testing.T) {
ci, err := newComponentIdentifier(tc.item)
require.NoError(t, err)
c, err := ci.createComponent(tc.msg)
if tc.err != nil {
require.Error(t, err)
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
require.NotNil(t, c)
assert.Equal(t, ci, c.key)
assert.NotEmpty(t, c.value)
}
})
}
}