showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
server_test.go
6.5 KB · 178 lines
1package server23import (4 "io"5 "log/slog"6 "net/http"7 "net/http/httptest"8 "os"9 "path/filepath"10 "strings"11 "testing"1213 "daum.dev/showcase/internal/config"14)1516func testServer(t *testing.T, ready bool) *Server {17 t.Helper()18 root := t.TempDir()19 files := map[string]string{20 "index.html": "home",21 "repos/index.html": "all repos",22 "404.html": "<h1>not found</h1>",23 "static/style.css": "body{}",24 "static/fonts/x.woff2": "font",25 "repo-a/index.html": "repo page",26 "repo-a/tree/docs/index.html": "docs dir",27 "repo-a/blob/main.go.html": "main.go page",28 "repo-a/raw/main.go": "package main",29 "repo-a/raw/logo.png": "\x89PNG",30 "repo-a/raw/evil.svg": "<svg onload=alert(1)>",31 "repo-a/raw/page.html": "<script>alert(1)</script>",32 "repo-a/raw/data.bin": "bin\x00ary",33 "repo-a/log/index.html": "log",34 "secret/index.html": "unlisted repo left on disk",35 }36 for name, body := range files {37 p := filepath.Join(root, name)38 os.MkdirAll(filepath.Dir(p), 0o755)39 os.WriteFile(p, []byte(body), 0o644)40 }41 cfg := &config.Config{GiteaURL: "http://127.0.0.1:1", User: "daniel", SiteURL: "https://code.danieldaum.net",42 Repos: []string{"repo-a"}, Images: []string{"repo-a", "tools/cli"}, TrustedProxies: []string{"10.0.0.1"}}43 return New(cfg, func() string { return root }, func() bool { return ready }, slog.New(slog.NewTextHandler(io.Discard, nil)))44}4546func do(s *Server, method, target string, body io.Reader) *httptest.ResponseRecorder {47 w := httptest.NewRecorder()48 req := httptest.NewRequest(method, target, body)49 s.ServeHTTP(w, req)50 return w51}5253func TestRoutes(t *testing.T) {54 s := testServer(t, true)55 cases := []struct {56 method, path string57 status int58 ctype, body string59 }{60 {"GET", "/", 200, "text/html", "home"},61 {"HEAD", "/", 200, "text/html", ""},62 {"GET", "/healthz", 200, "", "ok"},63 {"GET", "/repos/", 200, "text/html", "all repos"},64 {"GET", "/repos", 301, "", ""},65 {"GET", "/repos/x", 404, "", ""},66 {"GET", "/static/style.css", 200, "text/css", "body{}"},67 {"GET", "/static/fonts/x.woff2", 200, "font/woff2", ""},68 {"GET", "/repo-a/", 200, "text/html", "repo page"},69 {"GET", "/repo-a", 301, "", ""},70 {"GET", "/repo-a/tree/docs", 301, "", ""},71 {"GET", "/repo-a/tree/docs/", 200, "text/html", "docs dir"},72 {"GET", "/repo-a/blob/main.go", 200, "text/html", "main.go page"},73 {"GET", "/repo-a/log/", 200, "text/html", "log"},74 {"GET", "/repo-a/sub/pkg?go-get=1", 200, "text/html", "repo page"},75 // raw: images as images, everything else as text or a download76 {"GET", "/repo-a/raw/main.go", 200, "text/plain", "package main"},77 {"GET", "/repo-a/raw/logo.png", 200, "image/png", ""},78 {"GET", "/repo-a/raw/evil.svg", 200, "text/plain", ""},79 {"GET", "/repo-a/raw/page.html", 200, "text/plain", ""},80 {"GET", "/repo-a/raw/data.bin", 200, "application/octet-stream", ""},81 // default 404: Gitea's UI and API, unlisted repos, odd paths82 {"GET", "/repo-a/blob/missing", 404, "text/html", "not found"},83 {"GET", "/secret/", 404, "", ""},84 {"GET", "/api/v1/users/daniel", 404, "", ""},85 {"GET", "/user/login", 404, "", ""},86 {"GET", "/daniel/repo-a", 404, "", ""},87 {"GET", "/repo-a/raw/../../secret/index.html", 404, "", ""},88 {"GET", "/repo-a//index.html", 404, "", ""},89 {"GET", "/static/", 404, "", ""},90 {"GET", "/index.html", 404, "", ""},91 {"POST", "/", 404, "", ""},92 {"PUT", "/repo-a/", 404, "", ""},93 {"DELETE", "/repo-a/", 404, "", ""},94 }95 for _, c := range cases {96 w := do(s, c.method, c.path, nil)97 if w.Code != c.status {98 t.Errorf("%s %s: status %d, want %d", c.method, c.path, w.Code, c.status)99 continue100 }101 if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, c.ctype) {102 t.Errorf("%s %s: content type %q, want %q", c.method, c.path, ct, c.ctype)103 }104 if !strings.Contains(w.Body.String(), c.body) {105 t.Errorf("%s %s: body %q lacks %q", c.method, c.path, w.Body.String(), c.body)106 }107 if w.Header().Get("Content-Security-Policy") != csp || w.Header().Get("X-Content-Type-Options") != "nosniff" ||108 w.Header().Get("Referrer-Policy") != "no-referrer" {109 t.Errorf("%s %s: missing security headers", c.method, c.path)110 }111 }112}113114func TestBodyRejected(t *testing.T) {115 if w := do(testServer(t, true), "GET", "/", strings.NewReader("x")); w.Code != http.StatusBadRequest {116 t.Errorf("GET with body: %d", w.Code)117 }118}119120func TestNotReady(t *testing.T) {121 s := testServer(t, false)122 for _, p := range []string{"/", "/healthz", "/repo-a/"} {123 if w := do(s, "GET", p, nil); w.Code != http.StatusServiceUnavailable {124 t.Errorf("%s before first build: %d, want 503", p, w.Code)125 }126 }127}128129func TestClientIP(t *testing.T) {130 s := testServer(t, true)131 req := httptest.NewRequest("GET", "/", nil)132 req.Header.Set("X-Forwarded-For", "1.2.3.4")133 req.RemoteAddr = "192.0.2.9:5555"134 if ip := s.clientIP(req); ip != "192.0.2.9" {135 t.Errorf("untrusted peer: got %s", ip)136 }137 req.RemoteAddr = "10.0.0.1:5555"138 if ip := s.clientIP(req); ip != "1.2.3.4" {139 t.Errorf("trusted peer: got %s", ip)140 }141}142143func TestBasePath(t *testing.T) {144 s := testServer(t, true)145 s.Cfg.BasePath = "/projects"146 cases := []struct {147 path string148 status int149 body string // or the Location of a redirect150 location string151 }{152 {"/projects/", 200, "home", ""},153 {"/projects/repos/", 200, "all repos", ""},154 {"/projects/repo-a/", 200, "repo page", ""},155 {"/projects/repo-a/blob/main.go", 200, "main.go page", ""},156 {"/projects/repo-a/raw/main.go", 200, "package main", ""},157 {"/projects/static/style.css", 200, "body{}", ""},158 {"/healthz", 200, "ok", ""},159 {"/repo-a/sub/pkg?go-get=1", 200, "repo page", ""}, // module paths stay at the root160 // redirects into the base, so old links keep working161 {"/projects", 301, "", "/projects/"},162 {"/", 301, "", "/projects/"},163 {"/repo-a/log/", 301, "", "/projects/repo-a/log/"},164 {"/projects/repos", 301, "", "/projects/repos/"},165 {"/projects/repo-a", 301, "", "/projects/repo-a/"},166 {"/projects/repo-a/tree/docs", 301, "", "/projects/repo-a/tree/docs/"},167 // still 404 inside the base168 {"/projects/secret/", 404, "not found", ""},169 {"/projects/index.html", 404, "not found", ""},170 {"/projects/../secret/", 404, "not found", ""},171 }172 for _, c := range cases {173 w := do(s, "GET", c.path, nil)174 if w.Code != c.status || !strings.Contains(w.Body.String(), c.body) || w.Header().Get("Location") != c.location {175 t.Errorf("GET %s: %d %q location %q, want %d %q %q", c.path, w.Code, w.Body.String(), w.Header().Get("Location"), c.status, c.body, c.location)176 }177 }178}