added counter to verify upstream calls

This commit is contained in:
2026-05-06 15:33:17 -06:00
parent e7abb22c5e
commit b4b0aa1049
+14 -1
View File
@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
"sync/atomic"
"testing" "testing"
"github.com/ewpt3ch/pkgstash/internal/cache" "github.com/ewpt3ch/pkgstash/internal/cache"
@@ -17,6 +18,14 @@ var (
mirror404 = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) }) mirror404 = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) })
) )
func mirrorOKWithCounter() (http.HandlerFunc, *atomic.Int32) {
var calls atomic.Int32
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
fmt.Fprint(w, "fake pkg data")
}), &calls
}
const testUrlBase = "/core/os/x86_64" const testUrlBase = "/core/os/x86_64"
func newTestServer(t *testing.T, mirrorHandler http.HandlerFunc) (*httptest.Server, *Server) { func newTestServer(t *testing.T, mirrorHandler http.HandlerFunc) (*httptest.Server, *Server) {
@@ -91,7 +100,8 @@ func TestHandlerPkgsMiss(t *testing.T) {
func TestHandlerPkgsDBSig(t *testing.T) { func TestHandlerPkgsDBSig(t *testing.T) {
ts, _ := newTestServer(t, mirrorOK) handler, calls := mirrorOKWithCounter()
ts, _ := newTestServer(t, handler)
resp, err := http.Get(ts.URL + testUrlBase + "/core.db.sig") resp, err := http.Get(ts.URL + testUrlBase + "/core.db.sig")
if err != nil { if err != nil {
@@ -100,6 +110,9 @@ func TestHandlerPkgsDBSig(t *testing.T) {
if resp.StatusCode != http.StatusNotFound { if resp.StatusCode != http.StatusNotFound {
t.Errorf("expected 404 got %d", resp.StatusCode) t.Errorf("expected 404 got %d", resp.StatusCode)
} }
if calls.Load() != 0 {
t.Error("expected no upstream calls for .db.sig")
}
} }
func TestHandlerRefreshUnauthorized(t *testing.T) { func TestHandlerRefreshUnauthorized(t *testing.T) {