仰望星辰工作室

gitcat

gitcat/ internal/server/slash_test.go 1.2 KB · 38 行 原始文件
1package server
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8)
9
10// TestTrailingSlashTolerated 反代(nginx)会把无斜杠路径当磁盘目录 301 成
11// 带斜杠,gitcat 必须对尾斜杠宽容:就地剥离后正常渲染,而不是返回 Go mux
12// 的默认 404。这是仓库主页"点进去 404"的回归测试。
13func TestTrailingSlashTolerated(t *testing.T) {
14 srv, st, alice, _ := newTestServer(t)
15 dir := makeRepo(t, srv, st, "slash", alice)
16 commitFiles(t, dir, "first", map[string]string{"README.md": "# demo\n"})
17
18 cases := []struct {
19 path, want string
20 }{
21 {"/slash/", "README.md"}, // 仓库主页
22 {"/slash/issues/", "还没有议题"}, // 议题列表
23 {"/slash/commits/main/", "first"}, // 提交历史
24 {"/slash/blob/main/README.md/", "# demo"}, // 文件页
25 }
26 for _, c := range cases {
27 r := httptest.NewRequest(http.MethodGet, c.path, nil)
28 w := httptest.NewRecorder()
29 srv.Handler().ServeHTTP(w, r)
30 if w.Code != http.StatusOK {
31 t.Errorf("GET %s 返回 %d,期望 200", c.path, w.Code)
32 continue
33 }
34 if !strings.Contains(w.Body.String(), c.want) {
35 t.Errorf("GET %s 页面缺少 %q", c.path, c.want)
36 }
37 }
38}