Skip to content

Commit ea5ef85

Browse files
committed
fix: Ensuring component names are lowercase
1 parent a29c153 commit ea5ef85

2 files changed

Lines changed: 47 additions & 45 deletions

File tree

‎component_identifier.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ func newComponentIdentifier(item httpsfv.Item) (*componentIdentifier, error) {
8484
return nil, fmt.Errorf("%w: component name must be a string", ErrInvalidComponentIdentifier)
8585
}
8686

87-
canonicalizer, err := canonicalizerFor(strings.ToLower(name))
87+
if name != strings.ToLower(name) {
88+
return nil, fmt.Errorf("%w: component name %q must be lowercase", ErrInvalidComponentIdentifier, name)
89+
}
90+
91+
canonicalizer, err := canonicalizerFor(name)
8892
if err != nil {
8993
return nil, err
9094
}

‎component_identifier_test.go‎

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ import (
1111
func TestQuoteIdentifierName(t *testing.T) {
1212
t.Parallel()
1313

14-
for _, tc := range []struct {
15-
uc string
14+
for uc, tc := range map[string]struct {
1615
given string
1716
expect string
1817
}{
19-
{uc: "name already quoted", given: `"@method";req,key=val`, expect: `"@method";req,key=val`},
20-
{uc: "name not quoted, without parameter", given: `@method`, expect: `"@method"`},
21-
{uc: "name not quoted, with parameter", given: `@method;req,key=val`, expect: `"@method";req,key=val`},
18+
"name already quoted": {given: `"@method";req,key=val`, expect: `"@method";req,key=val`},
19+
"name not quoted, without parameter": {given: `@method`, expect: `"@method"`},
20+
"name not quoted, with parameter": {given: `@method;req,key=val`, expect: `"@method";req,key=val`},
2221
} {
23-
t.Run(tc.uc, func(t *testing.T) {
22+
t.Run(uc, func(t *testing.T) {
2423
res := quoteIdentifierName(tc.given)
2524
assert.Equal(t, tc.expect, res)
2625
})
@@ -30,18 +29,15 @@ func TestQuoteIdentifierName(t *testing.T) {
3029
func TestNormalizeParams(t *testing.T) {
3130
t.Parallel()
3231

33-
for _, tc := range []struct {
34-
uc string
32+
for uc, tc := range map[string]struct {
3533
given *httpsfv.Params
3634
expect *httpsfv.Params
3735
}{
38-
{
39-
uc: "empty params",
36+
"empty params": {
4037
given: httpsfv.NewParams(),
4138
expect: httpsfv.NewParams(),
4239
},
43-
{
44-
uc: "byte array params",
40+
"byte array params": {
4541
given: func() *httpsfv.Params {
4642
params := httpsfv.NewParams()
4743
params.Add("test", []byte("test"))
@@ -55,8 +51,7 @@ func TestNormalizeParams(t *testing.T) {
5551
return params
5652
}(),
5753
},
58-
{
59-
uc: "token params",
54+
"token params": {
6055
given: func() *httpsfv.Params {
6156
params := httpsfv.NewParams()
6257
params.Add("test", httpsfv.Token("token"))
@@ -70,8 +65,7 @@ func TestNormalizeParams(t *testing.T) {
7065
return params
7166
}(),
7267
},
73-
{
74-
uc: "bare item params",
68+
"bare item params": {
7569
given: func() *httpsfv.Params {
7670
params := httpsfv.NewParams()
7771
params.Add("test", 1)
@@ -86,7 +80,7 @@ func TestNormalizeParams(t *testing.T) {
8680
}(),
8781
},
8882
} {
89-
t.Run(tc.uc, func(t *testing.T) {
83+
t.Run(uc, func(t *testing.T) {
9084
res := normaliseParams(tc.given)
9185

9286
assert.Equal(t, tc.given.Names(), res.Names())
@@ -107,13 +101,11 @@ func TestNormalizeParams(t *testing.T) {
107101
func TestToComponentIdentifiers(t *testing.T) {
108102
t.Parallel()
109103

110-
for _, tc := range []struct {
111-
uc string
104+
for uc, tc := range map[string]struct {
112105
identifiers []string
113106
assert func(t *testing.T, ci []*componentIdentifier, err error)
114107
}{
115-
{
116-
uc: "invalid identifier definition",
108+
"invalid identifier definition": {
117109
identifiers: []string{"foo;#"},
118110
assert: func(t *testing.T, _ []*componentIdentifier, err error) {
119111
t.Helper()
@@ -122,8 +114,7 @@ func TestToComponentIdentifiers(t *testing.T) {
122114
require.ErrorIs(t, err, ErrInvalidComponentIdentifier)
123115
},
124116
},
125-
{
126-
uc: "unknown identifier",
117+
"unknown identifier": {
127118
identifiers: []string{"@foo"},
128119
assert: func(t *testing.T, _ []*componentIdentifier, err error) {
129120
t.Helper()
@@ -132,14 +123,22 @@ func TestToComponentIdentifiers(t *testing.T) {
132123
require.ErrorIs(t, err, ErrUnsupportedComponentIdentifier)
133124
},
134125
},
135-
{
136-
uc: "valid",
137-
identifiers: []string{"@method;foo=bar", "@status"},
126+
"content identifier must be lowercase": {
127+
identifiers: []string{"Content-Digest"},
128+
assert: func(t *testing.T, ci []*componentIdentifier, err error) {
129+
t.Helper()
130+
131+
require.ErrorIs(t, err, ErrInvalidComponentIdentifier)
132+
assert.Nil(t, ci)
133+
},
134+
},
135+
"valid": {
136+
identifiers: []string{"@method;foo=bar", "@status", `@query-param;name="Pet"`},
138137
assert: func(t *testing.T, ci []*componentIdentifier, err error) {
139138
t.Helper()
140139

141140
require.NoError(t, err)
142-
require.Len(t, ci, 2)
141+
require.Len(t, ci, 3)
143142

144143
assert.Equal(t, "@method", ci[0].Value.(string))
145144
res, ok := ci[0].Params.Get("foo")
@@ -150,10 +149,16 @@ func TestToComponentIdentifiers(t *testing.T) {
150149
assert.Equal(t, "@status", ci[1].Value.(string))
151150
assert.Empty(t, ci[1].Params.Names())
152151
assert.NotNil(t, ci[1].c)
152+
153+
assert.Equal(t, "@query-param", ci[2].Value.(string))
154+
res, ok = ci[2].Params.Get("name")
155+
require.True(t, ok)
156+
require.Equal(t, "Pet", res.(string))
157+
assert.NotNil(t, ci[2].c)
153158
},
154159
},
155160
} {
156-
t.Run(tc.uc, func(t *testing.T) {
161+
t.Run(uc, func(t *testing.T) {
157162
res, err := toComponentIdentifiers(tc.identifiers)
158163

159164
tc.assert(t, res, err)
@@ -164,13 +169,11 @@ func TestToComponentIdentifiers(t *testing.T) {
164169
func TestNewComponentIdentifier(t *testing.T) {
165170
t.Parallel()
166171

167-
for _, tc := range []struct {
168-
uc string
172+
for uc, tc := range map[string]struct {
169173
item httpsfv.Item
170174
assert func(t *testing.T, ci *componentIdentifier, err error)
171175
}{
172-
{
173-
uc: "non-string component name",
176+
"non-string component name": {
174177
item: httpsfv.NewItem(int64(42)),
175178
assert: func(t *testing.T, ci *componentIdentifier, err error) {
176179
t.Helper()
@@ -179,8 +182,7 @@ func TestNewComponentIdentifier(t *testing.T) {
179182
assert.Nil(t, ci)
180183
},
181184
},
182-
{
183-
uc: "invalid",
185+
"invalid": {
184186
item: httpsfv.NewItem("@foo"),
185187
assert: func(t *testing.T, _ *componentIdentifier, err error) {
186188
t.Helper()
@@ -189,8 +191,7 @@ func TestNewComponentIdentifier(t *testing.T) {
189191
require.ErrorIs(t, err, ErrUnsupportedComponentIdentifier)
190192
},
191193
},
192-
{
193-
uc: "valid",
194+
"valid": {
194195
item: func() httpsfv.Item {
195196
item := httpsfv.NewItem("@method")
196197
item.Params.Add("foo", []byte("bar"))
@@ -209,7 +210,7 @@ func TestNewComponentIdentifier(t *testing.T) {
209210
},
210211
},
211212
} {
212-
t.Run(tc.uc, func(t *testing.T) {
213+
t.Run(uc, func(t *testing.T) {
213214
ci, err := newComponentIdentifier(tc.item)
214215

215216
tc.assert(t, ci, err)
@@ -220,14 +221,12 @@ func TestNewComponentIdentifier(t *testing.T) {
220221
func TestComponentIdentifierCreateComponent(t *testing.T) {
221222
t.Parallel()
222223

223-
for _, tc := range []struct {
224-
uc string
224+
for uc, tc := range map[string]struct {
225225
item httpsfv.Item
226226
msg *Message
227227
err error
228228
}{
229-
{
230-
uc: "with error during canonicalization",
229+
"with error during canonicalization": {
231230
item: func() httpsfv.Item {
232231
item := httpsfv.NewItem("@method")
233232
item.Params.Add("req", true)
@@ -237,13 +236,12 @@ func TestComponentIdentifierCreateComponent(t *testing.T) {
237236
msg: &Message{Method: "GET", IsRequest: true},
238237
err: ErrCanonicalization,
239238
},
240-
{
241-
uc: "with error during canonicalization",
239+
"successful": {
242240
item: httpsfv.NewItem("@method"),
243241
msg: &Message{Method: "POST", IsRequest: true},
244242
},
245243
} {
246-
t.Run(tc.uc, func(t *testing.T) {
244+
t.Run(uc, func(t *testing.T) {
247245
ci, err := newComponentIdentifier(tc.item)
248246
require.NoError(t, err)
249247

0 commit comments

Comments
 (0)