Skip to content
quarry Artifact [48383a62a1]
Login

Artifact [48383a62a1]

Artifact 48383a62a16574f29305322a90614d84ba41939dd25482f2b6c488cd31fb3993:


package web

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

// A private repository the viewer cannot see and a name that was never
// registered answer every path the same way, signed in or out, so
// neither a page, a fragment, the card nor a stream can tell them apart
// (jva, ^3800). A public repository passes through.
func TestAPrivateRepoAnswersLikeANameThatIsNotThere(t *testing.T) {
	r := viewRegistry(t, nil)
	reached := ""
	next := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		reached = req.URL.Path
		w.WriteHeader(http.StatusTeapot)
	})
	visible := func(repo string, _ *http.Request) bool { return repo != "orchard" }
	known := func(repo string) bool { return repo == "orchard" || repo == "open" }
	h := unreadableNotFound(r, visible, known, next)
	answer := func(path, cookie string) (int, string) {
		req := httptest.NewRequest("GET", path, nil)
		if cookie != "" {
			req.Header.Set("Cookie", cookie)
		}
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, req)
		return rec.Code, rec.Body.String()
	}
	for _, sub := range []string{"", "/nav.frag", "/og.png", "/ci.sse", "/people.frag", "/merge"} {
		for _, cookie := range []string{"", "session=x"} {
			pc, pb := answer("/r/orchard"+sub, cookie)
			uc, ub := answer("/r/zzqqnon"+sub, cookie)
			// Same-length names: the page echoes the name it was asked for.
			if pc != http.StatusNotFound || pc != uc || pb != strings.ReplaceAll(ub, "zzqqnon", "orchard") {
				t.Errorf("FAIL: %s (cookie %q): private %d/%dB, unknown %d/%dB", sub, cookie, pc, len(pb), uc, len(ub))
			}
		}
	}
	if reached != "" {
		t.Errorf("FAIL: a hidden path reached its handler: %s", reached)
	}
	if code, _ := answer("/r/open/nav.frag", ""); code != http.StatusTeapot {
		t.Errorf("FAIL: a public repository did not pass: %d", code)
	}
	t.Log("ok")
}