Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ca15cc8

Browse files
committedMay 20, 2024··
fix: normalize headers with multiple vals
1 parent 2a8614f commit ca15cc8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
 

‎tooling/test/validate.go

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"strings"
89
"testing"
910

1011
"github.com/ipfs/gateway-conformance/tooling/check"
@@ -42,8 +43,30 @@ func validateResponse(
4243

4344
for _, header := range expected.Headers_ {
4445
testName := fmt.Sprintf("Header %s", header.Key_)
46+
4547
actual := res.Header.Values(header.Key_)
4648

49+
// HTTP Headers can have multiple values, and that can be represented by comman separated value,
50+
// or sending the same header more than once. The `res.Header.Get` only returns the value
51+
// from the first header, so we use Values here.
52+
// At the same time, we don't want to have two separate checks everywhere, so we normalize
53+
// multiple instances of the same header by converting it into a single one, with comma-separated
54+
// values.
55+
if len(actual) > 1 {
56+
var result []string
57+
all := strings.Join(actual, ",")
58+
split := strings.Split(all, ",")
59+
for _, s := range split {
60+
value := strings.TrimSpace(s)
61+
if value != "" {
62+
result = append(result, strings.TrimSpace(s))
63+
}
64+
}
65+
// Normalize values from all instances of the header into a single one and comma-separated notation
66+
joined := strings.Join(result, ", ")
67+
actual = []string{joined}
68+
}
69+
4770
c := header.Check_
4871
if header.Not_ {
4972
c = check.Not(c)

0 commit comments

Comments
 (0)
Please sign in to comment.