Skip to content
quarry Artifact [7df1bb8854]
Login

Artifact [7df1bb8854]

Artifact 7df1bb88548f7c3badc50a035f87bdc8d723602647da43e29e9317d8a6a6193f:


package web

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

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

type wikiGetIn struct {
	Repo string `json:"repo"`
	Name string `json:"name"`
	Rev  string `json:"rev"`
}
type wikiGetOut struct {
	Title string `json:"title"`
	HTML  string `json:"html"`
}

// fossil's addresses of one wiki page reach that page: /wiki?name=X,
// and a path with + for a space when no page carries the + itself.
func TestFossilWikiAddressesReachThePage(t *testing.T) {
	r := ops.NewRegistry()
	if err := ops.Register(r, ops.Op[wikiGetIn, wikiGetOut]{
		Name: "wiki.get", Kind: ops.Query, Auth: ops.Anonymous, Route: "GET /api/v1/r/{repo}/wiki/{name}", Doc: "test double",
		Run: func(_ context.Context, in wikiGetIn) (wikiGetOut, error) {
			if in.Name == "Brief Generation" || in.Name == "C++ Notes" {
				return wikiGetOut{Title: in.Name, HTML: "<p>x</p>"}, nil
			}
			return wikiGetOut{}, fmt.Errorf("no page %q: %w", in.Name, ops.ErrNotFound)
		},
	}); err != nil {
		t.Fatal(err)
	}
	h := HandlerWith(r, nil, nil, nil)
	get := func(path string) (int, string) {
		w := httptest.NewRecorder()
		h.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
		return w.Code, w.Header().Get("Location")
	}
	if code, loc := get("/r/kb/wiki?name=Brief+Generation"); code != http.StatusMovedPermanently || loc != "/r/kb/wiki/Brief%20Generation" {
		t.Errorf("FAIL: /wiki?name= answered %d -> %q", code, loc)
	}
	if code, loc := get("/r/kb/wiki/Brief+Generation"); code != http.StatusMovedPermanently || loc != "/r/kb/wiki/Brief%20Generation" {
		t.Errorf("FAIL: a + path answered %d -> %q", code, loc)
	}
	if code, loc := get("/r/kb/wiki/C++%20Notes"); code == http.StatusMovedPermanently {
		t.Errorf("FAIL: a page with + in its own name was redirected to %q", loc)
	}
	if code, loc := get("/r/kb/wiki/No+Such+Page"); code == http.StatusMovedPermanently {
		t.Errorf("FAIL: a missing page was redirected to %q", loc)
	}
}