serve/http: compare zip test output semantically

Go 1.27 changes compress/flate output, which also changes archive/zip
byte output. The HTTP zip download tests currently compare raw zip bytes
against golden files, so they fail even though the generated zip archives
contain the expected files.

Compare zip entries and decompressed contents instead of the exact
compressed byte stream.

Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
This commit is contained in:
Mikel Olasagasti Uranga
2026-07-15 11:00:18 +01:00
committed by Nick Craig-Wood
parent c851d4dec5
commit 169f5b714c
+34
View File
@@ -1,6 +1,8 @@
package http
import (
"archive/zip"
"bytes"
"compress/gzip"
"context"
"flag"
@@ -25,6 +27,11 @@ import (
"github.com/stretchr/testify/require"
)
type zipEntry struct {
IsDir bool
Contents string
}
var (
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
)
@@ -102,12 +109,39 @@ func checkGolden(t *testing.T, fileName string, got []byte) {
} else {
want, err := os.ReadFile(fileName)
require.NoError(t, err)
if strings.HasSuffix(fileName, ".zip") {
assert.Equal(t, readZip(t, want), readZip(t, got), fileName)
return
}
wants := strings.Split(string(want), "\n")
gots := strings.Split(string(got), "\n")
assert.Equal(t, wants, gots, fileName)
}
}
func readZip(t *testing.T, data []byte) map[string]zipEntry {
t.Helper()
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
require.NoError(t, err)
entries := make(map[string]zipEntry, len(zr.File))
for _, f := range zr.File {
entry := zipEntry{
IsDir: f.FileInfo().IsDir(),
}
if !entry.IsDir {
rc, err := f.Open()
require.NoError(t, err)
contents, err := io.ReadAll(rc)
require.NoError(t, err)
require.NoError(t, rc.Close())
entry.Contents = string(contents)
}
entries[f.Name] = entry
}
return entries
}
func testGET(t *testing.T, useProxy bool) {
ctx := context.Background()
// ci := fs.GetConfig(ctx)