indexer: sort headers to have deterministic output

This commit is contained in:
Aloïs Micard 2021-01-07 20:58:33 +01:00
parent 8297dc7616
commit 69352f7237
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE
2 changed files with 11 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
@ -63,9 +64,16 @@ func formatResource(url string, body string, headers map[string]string) ([]byte,
// First URL
builder.WriteString(fmt.Sprintf("%s\n\n", url))
// Sort headers to have deterministic output
var headerNames []string
for headerName := range headers {
headerNames = append(headerNames, headerName)
}
sort.Strings(headerNames)
// Then headers
for key, value := range headers {
builder.WriteString(fmt.Sprintf("%s: %s\n", key, value))
for _, name := range headerNames {
builder.WriteString(fmt.Sprintf("%s: %s\n", name, headers[name]))
}
builder.WriteString("\n")

View File

@ -134,7 +134,7 @@ func TestFormatResource(t *testing.T) {
t.FailNow()
}
if string(res) != "https://google.com\n\nServer: Traefik\nContent-Type: text/html\n\nHello, world" {
if string(res) != "https://google.com\n\nContent-Type: text/html\nServer: Traefik\n\nHello, world" {
t.Errorf("got %s want %s", string(res), "https://google.com\n\nServer: Traefik\nContent-Type: text/html\n\nHello, world")
}
}