package xfer
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// A repository the forge serves but has not registered yet is starting,
// not missing. Telling a sync client 404 during a deploy makes it
// conclude the repository is gone; 503 with Retry-After makes it wait.
func TestSyncSaysStartingRatherThanMissing(t *testing.T) {
f := &Fronter{repos: map[string]*backend{}}
f.SetKnown(func(name string) bool { return name == "quarry" })
for _, tc := range []struct {
name string
repo string
want int
retry bool
reason string
}{
{"served but not yet registered", "quarry", http.StatusServiceUnavailable, true,
"a store the forge serves is starting, not gone"},
{"genuinely absent", "nosuchrepo", http.StatusNotFound, false,
"a name the forge does not serve is still not found"},
} {
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/r/"+tc.repo+"/xfer", strings.NewReader(""))
req.SetPathValue("repo", tc.repo)
f.ServeHTTP(w, req)
if w.Code != tc.want {
t.Errorf("FAIL: %s: status %d, want %d — %s", tc.name, w.Code, tc.want, tc.reason)
continue
}
if got := w.Header().Get("Retry-After"); (got != "") != tc.retry {
t.Errorf("FAIL: %s: Retry-After %q, want present=%v", tc.name, got, tc.retry)
continue
}
t.Logf("ok: %s answers %d", tc.name, w.Code)
}
}
// Without the seam wired, behaviour is exactly as before: a missing
// backend is a missing repository.
func TestSyncWithoutTheKnownSeamStillSaysNotFound(t *testing.T) {
f := &Fronter{repos: map[string]*backend{}}
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/r/quarry/xfer", strings.NewReader(""))
req.SetPathValue("repo", "quarry")
f.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("FAIL: status %d, want 404 when nothing can answer whether the name is served", w.Code)
}
t.Log("ok: an unwired fronter is unchanged")
}