package web import ( "net/http" "strings" "testing" ) func noFollowClient() *http.Client { return &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} } func TestTreeView(t *testing.T) { httpServer, _, dataDir := newTestServer(t) createRepo(t, newLoggedInClient(t, httpServer), httpServer, "pub", "public") seedFiles(t, dataDir, "pub") // The ref view is the repo home layout at that ref: recursive file tree // plus the rendered README. resp, err := (&http.Client{}).Get(httpServer.URL + "/josie/pub/tree/main") if err != nil { t.Fatalf("GET tree root: %v", err) } body := readAll(t, resp) if resp.StatusCode != http.StatusOK { t.Fatalf("tree status = %d: %q", resp.StatusCode, body) } for _, want := range []string{ "/josie/pub/blob/main/hello.c", "/josie/pub/blob/main/sub/note.txt", "readme", `class="file-tree`, } { if !strings.Contains(body, want) { t.Errorf("ref view lacks %q", want) } } // Subdirectory pages are gone; the recursive tree links files directly. resp, err = (&http.Client{}).Get(httpServer.URL + "/josie/pub/tree/main/sub/") if err != nil { t.Fatalf("GET tree sub: %v", err) } readAll(t, resp) if resp.StatusCode != http.StatusNotFound { t.Errorf("subdirectory tree status = %d, want 404", resp.StatusCode) } resp, err = (&http.Client{}).Get(httpServer.URL + "/josie/pub/tree/nosuchref") if err != nil { t.Fatalf("GET bad ref: %v", err) } readAll(t, resp) if resp.StatusCode != http.StatusNotFound { t.Errorf("bad ref status = %d, want 404", resp.StatusCode) } } // TestSlashBranchViews checks refs containing "/" (feature/greeting) resolve // on the tree, blob, and commits views. func TestSlashBranchViews(t *testing.T) { httpServer, _, _ := newTestServer(t) owner := noFollow(newLoggedInClient(t, httpServer)) createRepo(t, owner, httpServer, "pub", "public") work := cloneRepo(t, httpServer, "pub") writeWork(t, work, "hello.c", "int main(void) { return 0; }\n") runGit(t, work, "add", ".") runGit(t, work, "commit", "-qm", "a") runGit(t, work, "-c", "credential.helper=", "push", "-q", "origin", "HEAD:refs/heads/main") runGit(t, work, "checkout", "-qb", "feature/greeting") writeWork(t, work, "hello.c", "int main(void) { return 1; }\n") runGit(t, work, "commit", "-qam", "b") runGit(t, work, "-c", "credential.helper=", "push", "-q", "origin", "feature/greeting") for _, path := range []string{ "/josie/pub/tree/feature/greeting", "/josie/pub/commits/feature/greeting", } { resp, err := (&http.Client{}).Get(httpServer.URL + path) if err != nil { t.Fatalf("GET %s: %v", path, err) } body := readAll(t, resp) if resp.StatusCode != http.StatusOK { t.Errorf("GET %s = %d, want 200: %q", path, resp.StatusCode, body) } } resp, err := (&http.Client{}).Get(httpServer.URL + "/josie/pub/blob/feature/greeting/hello.c") if err != nil { t.Fatalf("GET slash-branch blob: %v", err) } body := readAll(t, resp) if resp.StatusCode != http.StatusOK || !strings.Contains(body, "feature/greeting / hello.c") { t.Errorf("slash-branch blob = %d, want 200 resolved to the branch: %q", resp.StatusCode, body) } } func TestBlobViews(t *testing.T) { httpServer, _, dataDir := newTestServer(t) createRepo(t, newLoggedInClient(t, httpServer), httpServer, "pub", "public") seedFiles(t, dataDir, "pub") resp, err := (&http.Client{}).Get(httpServer.URL + "/josie/pub/blob/main/hello.c") if err != nil { t.Fatalf("GET blob c: %v", err) } body := readAll(t, resp) if resp.StatusCode != http.StatusOK { t.Fatalf("blob status = %d: %q", resp.StatusCode, body) } if !strings.Contains(body, "readme") { t.Errorf("markdown blob not rendered: %q", body) } resp, err = (&http.Client{}).Get(httpServer.URL + "/josie/pub/blob/main/sub/note.txt") if err != nil { t.Fatalf("GET blob txt: %v", err) } body = readAll(t, resp) if !strings.Contains(body, "