package server import ( "net/http" "net/http/httptest" "strings" "testing" ) // TestTrailingSlashTolerated 反代(nginx)会把无斜杠路径当磁盘目录 301 成 // 带斜杠,gitcat 必须对尾斜杠宽容:就地剥离后正常渲染,而不是返回 Go mux // 的默认 404。这是仓库主页"点进去 404"的回归测试。 func TestTrailingSlashTolerated(t *testing.T) { srv, st, alice, _ := newTestServer(t) dir := makeRepo(t, srv, st, "slash", alice) commitFiles(t, dir, "first", map[string]string{"README.md": "# demo\n"}) cases := []struct { path, want string }{ {"/slash/", "README.md"}, // 仓库主页 {"/slash/issues/", "还没有议题"}, // 议题列表 {"/slash/commits/main/", "first"}, // 提交历史 {"/slash/blob/main/README.md/", "# demo"}, // 文件页 } for _, c := range cases { r := httptest.NewRequest(http.MethodGet, c.path, nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusOK { t.Errorf("GET %s 返回 %d,期望 200", c.path, w.Code) continue } if !strings.Contains(w.Body.String(), c.want) { t.Errorf("GET %s 页面缺少 %q", c.path, c.want) } } }