|
4 | 4 | "archive/zip"
|
5 | 5 | "crypto/sha256"
|
6 | 6 | "encoding/hex"
|
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "io" |
7 | 9 | "net/http"
|
8 | 10 | "net/http/httptest"
|
9 | 11 | "os"
|
@@ -370,3 +372,48 @@ func Test_defaultRemoteFetchStrategyWithExistingDownload(t *testing.T) {
|
370 | 372 | out2, err := os.ReadFile(cacheLocation)
|
371 | 373 | assert.Equal(t, out1, out2)
|
372 | 374 | }
|
| 375 | + |
| 376 | +func Test_defaultRemoteFetchStrategy_whenContentLengthNotSet(t *testing.T) { |
| 377 | + jarFile, cleanUp := createTempZipArchive() |
| 378 | + defer cleanUp() |
| 379 | + |
| 380 | + cacheLocation := filepath.Join(filepath.Dir(jarFile), "extract_location", "cache.jar") |
| 381 | + |
| 382 | + bytes, err := os.ReadFile(jarFile) |
| 383 | + if err != nil { |
| 384 | + require.NoError(t, err) |
| 385 | + } |
| 386 | + contentHash := sha256.Sum256(bytes) |
| 387 | + |
| 388 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 389 | + if strings.HasSuffix(r.RequestURI, ".sha256") { |
| 390 | + w.WriteHeader(200) |
| 391 | + if _, err := w.Write([]byte(hex.EncodeToString(contentHash[:]))); err != nil { |
| 392 | + panic(err) |
| 393 | + } |
| 394 | + |
| 395 | + return |
| 396 | + } |
| 397 | + |
| 398 | + f, err := os.Open(jarFile) |
| 399 | + if err != nil { |
| 400 | + panic(err) |
| 401 | + } |
| 402 | + |
| 403 | + // stream the file back so that Go uses |
| 404 | + // chunked encoding and never sets Content-Length |
| 405 | + _, _ = io.Copy(w, f) |
| 406 | + })) |
| 407 | + defer server.Close() |
| 408 | + |
| 409 | + remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL+"/maven2", |
| 410 | + testVersionStrategy(), |
| 411 | + func() (s string, b bool) { |
| 412 | + return cacheLocation, false |
| 413 | + }) |
| 414 | + |
| 415 | + err = remoteFetchStrategy() |
| 416 | + |
| 417 | + assert.NoError(t, err) |
| 418 | + assert.FileExists(t, cacheLocation) |
| 419 | +} |
0 commit comments