2026-10-14 08:39:12 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/git-pkgs/registries/fetch"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-19 12:28:55 +01:00
|
|
|
func artifactSizeFetcher(t *testing.T) (*fetch.Fetcher, string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
|
_, _ = io.WriteString(w, "0123456789")
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(upstream.Close)
|
|
|
|
|
f := fetch.NewFetcher(fetch.WithHTTPClient(upstream.Client()), fetch.WithMaxArtifactSize(4))
|
|
|
|
|
t.Cleanup(func() { _ = f.Close() })
|
|
|
|
|
return f, upstream.URL
|
2026-10-14 08:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestOversizedArtifactIsNotCached(t *testing.T) {
|
2026-10-19 12:28:55 +01:00
|
|
|
proxy, db, store, _ := setupTestProxy(t)
|
|
|
|
|
limited, upstream := artifactSizeFetcher(t)
|
|
|
|
|
proxy.Fetcher = limited
|
2026-10-14 08:39:12 +02:00
|
|
|
|
2026-10-19 12:28:55 +01:00
|
|
|
_, err := proxy.GetOrFetchArtifactFromURL(context.Background(), "npm", "big", "1.0.0", "big-1.0.0.tgz", upstream+"/big-1.0.0.tgz")
|
|
|
|
|
if !errors.Is(err, fetch.ErrArtifactTooLarge) {
|
|
|
|
|
t.Fatalf("GetOrFetchArtifactFromURL error = %v, want fetch.ErrArtifactTooLarge", err)
|
2026-10-14 08:39:12 +02:00
|
|
|
}
|
|
|
|
|
if len(store.files) != 0 {
|
|
|
|
|
t.Errorf("storage holds %d objects after an oversized fetch, want 0", len(store.files))
|
|
|
|
|
}
|
2026-10-19 12:28:55 +01:00
|
|
|
fetchErr := err
|
2026-10-14 08:39:12 +02:00
|
|
|
record, err := db.GetArtifact("pkg:npm/big@1.0.0", "big-1.0.0.tgz")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetArtifact failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if record != nil {
|
|
|
|
|
t.Error("oversized artifact was recorded in the cache database")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2026-10-19 12:28:55 +01:00
|
|
|
proxy.serveArtifactError(rec, fetchErr, "failed to fetch package")
|
2026-10-14 08:39:12 +02:00
|
|
|
if rec.Code != http.StatusBadGateway {
|
|
|
|
|
t.Errorf("status = %d, want %d", rec.Code, http.StatusBadGateway)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestOversizedStreamedArtifactFailsTheStream(t *testing.T) {
|
2026-10-19 12:28:55 +01:00
|
|
|
proxy, _, _, _ := setupTestProxy(t)
|
2026-10-14 08:39:12 +02:00
|
|
|
proxy.StreamArtifacts = true
|
2026-10-19 12:28:55 +01:00
|
|
|
limited, upstream := artifactSizeFetcher(t)
|
|
|
|
|
proxy.Fetcher = limited
|
2026-10-14 08:39:12 +02:00
|
|
|
|
2026-10-19 12:28:55 +01:00
|
|
|
result, err := proxy.GetOrFetchArtifactFromURL(context.Background(), "npm", "big", "1.0.0", "big-1.0.0.tgz", upstream+"/big-1.0.0.tgz")
|
2026-10-14 08:39:12 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetOrFetchArtifactFromURL failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer func() { _ = result.Reader.Close() }()
|
2026-10-19 12:28:55 +01:00
|
|
|
if _, err := io.ReadAll(result.Reader); !errors.Is(err, fetch.ErrArtifactTooLarge) {
|
|
|
|
|
t.Errorf("streamed body error = %v, want fetch.ErrArtifactTooLarge", err)
|
2026-10-14 08:39:12 +02:00
|
|
|
}
|
|
|
|
|
}
|