From 69352f7237750d8cd3f49aecb42e14a8a944eda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alo=C3=AFs=20Micard?= Date: Thu, 7 Jan 2021 20:58:33 +0100 Subject: [PATCH] indexer: sort headers to have deterministic output --- internal/indexer/index/local.go | 12 ++++++++++-- internal/indexer/index/local_test.go | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/indexer/index/local.go b/internal/indexer/index/local.go index 9b84f92..8d4c8af 100644 --- a/internal/indexer/index/local.go +++ b/internal/indexer/index/local.go @@ -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") diff --git a/internal/indexer/index/local_test.go b/internal/indexer/index/local_test.go index 00bca29..b349e8e 100644 --- a/internal/indexer/index/local_test.go +++ b/internal/indexer/index/local_test.go @@ -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") } }