Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion remote_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ func closeBody(resp *http.Response) func() {
}

func decompressResponse(bodyBytes []byte, contentLength int64, cacheLocator CacheLocator, downloadURL string) error {
zipReader, err := zip.NewReader(bytes.NewReader(bodyBytes), contentLength)
size := contentLength
// if the content length is not set (i.e. chunked encoding),
// we need to use the length of the bodyBytes otherwise
// the unzip operation will fail
if contentLength < 0 {
size = int64(len(bodyBytes))
}
zipReader, err := zip.NewReader(bytes.NewReader(bodyBytes), size)
if err != nil {
return errorFetchingPostgres(err)
}
Expand Down
47 changes: 47 additions & 0 deletions remote_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"archive/zip"
"crypto/sha256"
"encoding/hex"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -370,3 +372,48 @@ func Test_defaultRemoteFetchStrategyWithExistingDownload(t *testing.T) {
out2, err := os.ReadFile(cacheLocation)
assert.Equal(t, out1, out2)
}

func Test_defaultRemoteFetchStrategy_whenContentLengthNotSet(t *testing.T) {
jarFile, cleanUp := createTempZipArchive()
defer cleanUp()

cacheLocation := filepath.Join(filepath.Dir(jarFile), "extract_location", "cache.jar")

bytes, err := os.ReadFile(jarFile)
if err != nil {
require.NoError(t, err)
}
contentHash := sha256.Sum256(bytes)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.RequestURI, ".sha256") {
w.WriteHeader(200)
if _, err := w.Write([]byte(hex.EncodeToString(contentHash[:]))); err != nil {
panic(err)
}

return
}

f, err := os.Open(jarFile)
if err != nil {
panic(err)
}

// stream the file back so that Go uses
// chunked encoding and never sets Content-Length
_, _ = io.Copy(w, f)
}))
defer server.Close()

remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL+"/maven2",
testVersionStrategy(),
func() (s string, b bool) {
return cacheLocation, false
})

err = remoteFetchStrategy()

assert.NoError(t, err)
assert.FileExists(t, cacheLocation)
}