diff --git a/examples/go.sum b/examples/go.sum index 029e80d..cf45c6d 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -81,5 +81,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA= diff --git a/go.mod b/go.mod index 737d281..f2a74b3 100644 --- a/go.mod +++ b/go.mod @@ -12,5 +12,5 @@ require ( require ( github.com/davecgh/go-spew v1.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.0 // indirect ) diff --git a/go.sum b/go.sum index 4722b00..fa38a0b 100644 --- a/go.sum +++ b/go.sum @@ -47,5 +47,6 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/prepare_database.go b/prepare_database.go index 494be90..0e8fa65 100644 --- a/prepare_database.go +++ b/prepare_database.go @@ -82,7 +82,7 @@ func defaultCreateDatabase(port uint32, username, password, database string) (er err = connectionClose(db, err) }() - if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", database)); err != nil { + if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE \"%s\"", database)); err != nil { return errorCustomDatabase(database, err) } diff --git a/prepare_database_test.go b/prepare_database_test.go index f29845b..cad9873 100644 --- a/prepare_database_test.go +++ b/prepare_database_test.go @@ -116,6 +116,20 @@ func Test_defaultCreateDatabase_ErrorWhenSQLOpenError(t *testing.T) { assert.EqualError(t, err, "unable to connect to create database with custom name database with the following error: client_encoding must be absent or 'UTF8'") } +func Test_defaultCreateDatabase_DashesInName(t *testing.T) { + database := NewDatabase(DefaultConfig(). + Port(9832). + Database("my-cool-database")) + + if err := database.Start(); err != nil { + t.Fatal(err) + } + + if err := database.Stop(); err != nil { + t.Fatal(err) + } +} + func Test_defaultCreateDatabase_ErrorWhenQueryError(t *testing.T) { database := NewDatabase(DefaultConfig(). Port(9831). diff --git a/remote_fetch.go b/remote_fetch.go index dceae84..1aa1e84 100644 --- a/remote_fetch.go +++ b/remote_fetch.go @@ -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) } diff --git a/remote_fetch_test.go b/remote_fetch_test.go index c884e5c..9e6dcfb 100644 --- a/remote_fetch_test.go +++ b/remote_fetch_test.go @@ -4,6 +4,8 @@ import ( "archive/zip" "crypto/sha256" "encoding/hex" + "github.com/stretchr/testify/require" + "io" "net/http" "net/http/httptest" "os" @@ -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) +}