Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion canonicalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ func TestCanonicalizeHeader(t *testing.T) {
"header component for request - invalid, serialization error": {
identifier: `"X-Foo-Bar";sf;key="foo"`,
message: &Message{Header: http.Header{"X-Foo-Bar": []string{"baz"}}, IsRequest: true},
errContains: "unable to parse",
errContains: "serialization error",
},
"header component for request - valid": {
identifier: `"X-Foo-Bar"`,
Expand Down
27 changes: 13 additions & 14 deletions serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,30 @@ type strictSerializer struct {
key string
}

func (s *strictSerializer) serialize(v []string) ([]string, error) {
// strict encoding of field
parsed, err := s.parse(v)
if err != nil {
return nil, err
}
func (s *strictSerializer) serialize(values []string) ([]string, error) {
var parsed httpsfv.StructuredFieldValue

if len(s.key) != 0 {
dict, ok := parsed.(*httpsfv.Dictionary)
if !ok {
return nil, fmt.Errorf("%w: unable to parse value as dictionary",
errSerialization)
// The 'key' parameter requires dictionary parsing, even when the
// field value could also be parsed as a list (e.g. a bare boolean member).
dict, err := httpsfv.UnmarshalDictionary(values)
if err != nil {
return nil, fmt.Errorf("%w: unable to parse value as dictionary: %w", errSerialization, err)
}

val, ok := dict.Get(s.key)
if !ok {
return nil, fmt.Errorf("%w: unable to find key '%s' in structured field", errSerialization, s.key)
}

marshalled, err := httpsfv.Marshal(val)
parsed = val
} else {
var err error

parsed, err = s.parse(values)
if err != nil {
return nil, fmt.Errorf("%w: %w", errSerialization, err)
return nil, err
}

return []string{marshalled}, nil
}

marshalled, err := httpsfv.Marshal(parsed)
Expand Down
18 changes: 10 additions & 8 deletions serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,22 @@ func TestStrictEncodingSerialization(t *testing.T) {
func TestStructuredFieldSerialization(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
for uc, tc := range map[string]struct {
input []string
key string
result []string
err bool
}{
{input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "a", result: []string{"1"}},
{input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "b", result: []string{"2;x=1;y=2"}},
{input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "c", result: []string{"(a b c)"}},
{input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "d", result: []string{"?1"}},
{input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "e", err: true},
{input: []string{"(a b c)"}, key: "a", err: true},
"integer member": {input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "a", result: []string{"1"}},
"member with parameters": {input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "b", result: []string{"2;x=1;y=2"}},
"inner list member": {input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "c", result: []string{"(a b c)"}},
"boolean member in dictionary": {input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "d", result: []string{"?1"}},
"single bare boolean member": {input: []string{"a"}, key: "a", result: []string{"?1"}},
"bare boolean with parameters": {input: []string{"a;x=1"}, key: "a", result: []string{"?1;x=1"}},
"missing dictionary member": {input: []string{" a=1, b=2;x=1;y=2, c=(a b c), d"}, key: "e", err: true},
"non-dictionary field": {input: []string{"(a b c)"}, key: "a", err: true},
} {
t.Run("key="+tc.key, func(t *testing.T) {
t.Run(uc, func(t *testing.T) {
params := httpsfv.NewParams()
params.Add("key", tc.key)

Expand Down
Loading