Skip to content
quarry Artifact [e4e298c2c7]
Login

Artifact [e4e298c2c7]

Artifact e4e298c2c74750e13969be7bb72ad72bf282c6a1bbc15b268b490a80ec48413d:


package api

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

	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

// The web's /r/{owner}/{repo} spelling, carried into the API, answers a
// 404 that names the bare-name path to try. The catch-all used to say
// only "no such endpoint": the hint repoFor gives fired for %2F, which
// routes, and never for the slash, which does not (G-1, jva #1068).
func TestAnOwnerPrefixedAPIPathIsToldTheBarePath(t *testing.T) {
	r := ops.NewRegistry()
	h, err := RouterWithCaps(r, "test", func(_, _ string) CapSet { return oltp.CapSet("s") }, nil)
	if err != nil {
		t.Fatal(err)
	}
	for _, c := range []struct{ path, want string }{
		{"/api/v1/r/jva/dantest-sandbox/info", "/api/v1/r/dantest-sandbox/info"},
		{"/api/v1/r/jva/dantest-sandbox/file/trunk/README.md", "/api/v1/r/dantest-sandbox/file/trunk/README.md"},
	} {
		w := httptest.NewRecorder()
		h.ServeHTTP(w, httptest.NewRequest("GET", c.path, nil))
		if w.Code != http.StatusNotFound || !strings.Contains(w.Body.String(), c.want) || !strings.Contains(w.Body.String(), "jva") {
			t.Fatalf("FAIL: %s -> %d %s", c.path, w.Code, w.Body.String())
		}
		t.Logf("ok: %s -> %s", c.path, strings.TrimSpace(w.Body.String()))
	}
	// Two segments is an unknown op, not an owner prefix: no hint.
	w := httptest.NewRecorder()
	h.ServeHTTP(w, httptest.NewRequest("GET", "/api/v1/r/quarry/nosuchop", nil))
	if w.Code != 404 || strings.Contains(w.Body.String(), "bare name") {
		t.Fatalf("FAIL: an unknown op got the owner hint: %s", w.Body.String())
	}
	t.Log("ok: an unknown op is a plain 404")
}