|
5 | 5 | "fmt"
|
6 | 6 | "io"
|
7 | 7 | "net/http"
|
| 8 | + "strings" |
8 | 9 | "testing"
|
9 | 10 |
|
10 | 11 | "github.com/ipfs/gateway-conformance/tooling/check"
|
@@ -42,8 +43,30 @@ func validateResponse(
|
42 | 43 |
|
43 | 44 | for _, header := range expected.Headers_ {
|
44 | 45 | testName := fmt.Sprintf("Header %s", header.Key_)
|
| 46 | + |
45 | 47 | actual := res.Header.Values(header.Key_)
|
46 | 48 |
|
| 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 | + |
47 | 70 | c := header.Check_
|
48 | 71 | if header.Not_ {
|
49 | 72 | c = check.Not(c)
|
|
0 commit comments