BlueGit

timfa bluegit

Files

bluegit / internal / server / trailing_slash_test.go

trailing_slash_test.go

trailing_slash_test.go 1.7 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "path/filepath"
7 "testing"
8
9 "gitlab.com/Timfa/bluegit/internal/auth"
10 "gitlab.com/Timfa/bluegit/internal/config"
11 "gitlab.com/Timfa/bluegit/internal/store"
12)
13
14func TestTrailingSlashPages(t *testing.T) {
15 storage := t.TempDir()
16 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
17 if err != nil {
18 t.Fatal(err)
19 }
20 t.Cleanup(func() { _ = st.Close() })
21 cfg := config.Config{
22 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
23 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"},
24 Git: config.Git{Binary: "git", DefaultBranch: "master"},
25 }
26 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
27 ts := httptest.NewServer(srv.Handler())
28 t.Cleanup(ts.Close)
29 if _, err := st.UpsertUser(t.Context(), 2, "ada", "", false); err != nil {
30 t.Fatal(err)
31 }
32 client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
33 cases := []struct{ path, loc string }{
34 {"/ada", "/users/ada"},
35 {"/ada/", "/users/ada"},
36 {"/users/ada/", "/users/ada"},
37 {"/groups/plein/", "/groups/plein"},
38 }
39 for _, c := range cases {
40 res, err := client.Get(ts.URL + c.path)
41 if err != nil {
42 t.Fatal(err)
43 }
44 res.Body.Close()
45 if res.StatusCode/100 != 3 || res.Header.Get("Location") != c.loc {
46 t.Errorf("%s: status %d location %q, wil redirect naar %q", c.path, res.StatusCode, res.Header.Get("Location"), c.loc)
47 }
48 }
49 res, err := client.Get(ts.URL + "/niemand/")
50 if err != nil {
51 t.Fatal(err)
52 }
53 res.Body.Close()
54 if res.StatusCode != http.StatusNotFound {
55 t.Errorf("/niemand/: status %d, wil 404", res.StatusCode)
56 }
57}