package server import ( "net/http" "net/http/httptest" "path/filepath" "testing" "gitlab.com/Timfa/bluegit/internal/auth" "gitlab.com/Timfa/bluegit/internal/config" "gitlab.com/Timfa/bluegit/internal/store" ) func TestTrailingSlashPages(t *testing.T) { storage := t.TempDir() st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = st.Close() }) cfg := config.Config{ BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"}, Git: config.Git{Binary: "git", DefaultBranch: "master"}, } srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} ts := httptest.NewServer(srv.Handler()) t.Cleanup(ts.Close) if _, err := st.UpsertUser(t.Context(), 2, "ada", "", false); err != nil { t.Fatal(err) } client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} cases := []struct{ path, loc string }{ {"/ada", "/users/ada"}, {"/ada/", "/users/ada"}, {"/users/ada/", "/users/ada"}, {"/groups/plein/", "/groups/plein"}, } for _, c := range cases { res, err := client.Get(ts.URL + c.path) if err != nil { t.Fatal(err) } res.Body.Close() if res.StatusCode/100 != 3 || res.Header.Get("Location") != c.loc { t.Errorf("%s: status %d location %q, wil redirect naar %q", c.path, res.StatusCode, res.Header.Get("Location"), c.loc) } } res, err := client.Get(ts.URL + "/niemand/") if err != nil { t.Fatal(err) } res.Body.Close() if res.StatusCode != http.StatusNotFound { t.Errorf("/niemand/: status %d, wil 404", res.StatusCode) } }