| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "fmt" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "regexp" |
| 15 | "strings" |
| 16 | "sync" |
| 17 | "testing" |
| 18 | "time" |
| 19 | |
| 20 | "the-valley/knot" |
| 21 | "the-valley/webui/export" |
| 22 | "the-valley/webui/export/exporttest" |
| 23 | ) |
| 24 | |
| 25 | // harness is a server over a real store that the fixture's export was |
| 26 | // uploaded to, with a clock the test sets. |
| 27 | type harness struct { |
| 28 | t *testing.T |
| 29 | f *exporttest.Fixture |
| 30 | store *export.Store |
| 31 | srv *Server |
| 32 | now time.Time |
| 33 | } |
| 34 | |
| 35 | // newHarness serves the fixture's export, two minutes after it was |
| 36 | // collected. |
| 37 | func newHarness(t *testing.T) *harness { |
| 38 | t.Helper() |
| 39 | f := exporttest.New(t) |
| 40 | store, err := export.OpenStore(t.TempDir(), func(err error) { t.Errorf("the store reported: %v", err) }) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | f.Upload(t, store) |
| 45 | h := &harness{t: t, f: f, store: store, now: exporttest.Collected.Add(2 * time.Minute)} |
| 46 | h.srv = New(Config{Store: store, Now: func() time.Time { return h.now }, |
| 47 | Log: func(err error) { t.Errorf("the server logged: %v", err) }}) |
| 48 | return h |
| 49 | } |
| 50 | |
| 51 | func (h *harness) do(method, target string) *httptest.ResponseRecorder { |
| 52 | h.t.Helper() |
| 53 | w := httptest.NewRecorder() |
| 54 | h.srv.ServeHTTP(w, httptest.NewRequest(method, target, nil)) |
| 55 | return w |
| 56 | } |
| 57 | |
| 58 | func (h *harness) get(target string) *httptest.ResponseRecorder { |
| 59 | h.t.Helper() |
| 60 | return h.do("GET", target) |
| 61 | } |
| 62 | |
| 63 | // page is the body of a GET that must answer 200. |
| 64 | func (h *harness) page(target string) string { |
| 65 | h.t.Helper() |
| 66 | w := h.get(target) |
| 67 | if w.Code != http.StatusOK { |
| 68 | h.t.Fatalf("GET %s answered %d:\n%s", target, w.Code, w.Body) |
| 69 | } |
| 70 | return w.Body.String() |
| 71 | } |
| 72 | |
| 73 | // tip is the fixture's published source tip of app. |
| 74 | func (h *harness) tip() string { return h.f.Export.Projects[0].SourceTip } |
| 75 | |
| 76 | // publish activates a new generation, signed at: the fixture's export |
| 77 | // changed by change, with the bundles given in place of the fixture's. |
| 78 | func (h *harness) publish(at time.Time, bundles map[string]string, change func(*export.Export)) export.Generation { |
| 79 | t := h.t |
| 80 | t.Helper() |
| 81 | e, err := export.Decode(h.f.ExportJSON) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | change(e) |
| 86 | data, err := e.Encode() |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | paths := map[string]string{} |
| 91 | for name, path := range h.f.Bundles { |
| 92 | paths[name] = path |
| 93 | } |
| 94 | for name, path := range bundles { |
| 95 | paths[name] = path |
| 96 | } |
| 97 | bodies := map[string]export.Body{} |
| 98 | for name, path := range paths { |
| 99 | if bodies[name], err = export.FileBody(name, path); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | } |
| 103 | m, err := export.NewManifest(exporttest.Instance, exporttest.Destination, at.UnixNano(), e, data, bodies) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | hw, err := h.store.HighWater() |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | a := h.f.Admission() |
| 112 | a.Now, a.HighWater = at.Add(time.Second), hw |
| 113 | if m, err = export.VerifyManifest(bytes.NewReader(h.f.Sign(t, m)), a); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | st, err := h.store.Stage(m) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | inv, err := h.store.Inventory(st) |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | for _, need := range inv.Needs { |
| 125 | body := data |
| 126 | if need.Name != export.ExportFile { |
| 127 | if body, err = os.ReadFile(paths[need.Name]); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | } |
| 131 | req := httptest.NewRequest("PUT", export.BodyPath(m.Sequence, need.SHA256), bytes.NewReader(body)) |
| 132 | if _, err := export.StageBody(st.Bodies(), export.Body{Name: need.Name, SHA256: need.SHA256, Size: need.Size}, 64<<20, req); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | } |
| 136 | g, err := h.store.Activate(context.Background(), st) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | return g |
| 141 | } |
| 142 | |
| 143 | // active is the generation the store serves. |
| 144 | func (h *harness) active() export.Generation { |
| 145 | h.t.Helper() |
| 146 | g, release, err := h.store.Open() |
| 147 | if err != nil { |
| 148 | h.t.Fatal(err) |
| 149 | } |
| 150 | release() |
| 151 | return g |
| 152 | } |
| 153 | |
| 154 | // git runs git on the repository at repo, with no configuration of the |
| 155 | // caller's and a fixed identity for the commits it writes. |
| 156 | func git(t *testing.T, repo string, stdin []byte, args ...string) string { |
| 157 | t.Helper() |
| 158 | cmd := exec.Command(export.GitProgram, append([]string{"--git-dir=" + repo}, args...)...) |
| 159 | for _, kv := range os.Environ() { |
| 160 | if !strings.HasPrefix(kv, "GIT_") { |
| 161 | cmd.Env = append(cmd.Env, kv) |
| 162 | } |
| 163 | } |
| 164 | cmd.Env = append(cmd.Env, "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", |
| 165 | "GIT_AUTHOR_NAME=Example Author", "GIT_AUTHOR_EMAIL=author@example.org", "GIT_AUTHOR_DATE=1791190800 +0000", |
| 166 | "GIT_COMMITTER_NAME=Example Author", "GIT_COMMITTER_EMAIL=author@example.org", "GIT_COMMITTER_DATE=1791190800 +0000") |
| 167 | if stdin != nil { |
| 168 | cmd.Stdin = bytes.NewReader(stdin) |
| 169 | } |
| 170 | var stderr bytes.Buffer |
| 171 | cmd.Stderr = &stderr |
| 172 | out, err := cmd.Output() |
| 173 | if err != nil { |
| 174 | t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, stderr.String()) |
| 175 | } |
| 176 | return strings.TrimSpace(string(out)) |
| 177 | } |
| 178 | |
| 179 | // section is the part of a page from one marker up to the next. |
| 180 | func section(t *testing.T, page, from, to string) string { |
| 181 | t.Helper() |
| 182 | i := strings.Index(page, from) |
| 183 | j := strings.Index(page[max(i, 0):], to) |
| 184 | if i < 0 || j < 0 { |
| 185 | t.Fatalf("the page has no section from %q to %q", from, to) |
| 186 | } |
| 187 | return page[i : i+j] |
| 188 | } |
| 189 | |
| 190 | // The plan page keeps the template's reading order, says what free to |
| 191 | // start means, shows the ready changes as one card in no order, and |
| 192 | // shows how old the export is. |
| 193 | func TestPlanPage(t *testing.T) { |
| 194 | h := newHarness(t) |
| 195 | page := h.page("/") |
| 196 | |
| 197 | at := -1 |
| 198 | for _, heading := range []string{">Waiting on you<", ">In flight<", ">Paths<", ">Milestones<"} { |
| 199 | i := strings.Index(page, heading) |
| 200 | if i <= at { |
| 201 | t.Fatalf("%s is not where the reading order puts it", heading) |
| 202 | } |
| 203 | at = i |
| 204 | } |
| 205 | for _, want := range []string{ |
| 206 | "free to start: an open, unclaimed, unblocked outcome or bug", |
| 207 | "Nothing here waits on you.", |
| 208 | "Ready changes — order not assessed", |
| 209 | `<span class="handle">kmno</span>`, "plan: list the milestones", "app v2", "approved · integrator: stale", |
| 210 | "Collected 2026-10-25 12:00 UTC, 2 minutes ago", |
| 211 | "claimed by agent 2 hours ago", |
| 212 | "in progress, unclaimed", |
| 213 | "Readers browse the app's plan", "1 of 5 done", |
| 214 | "Unfiled", "Render Markdown in a later slice", |
| 215 | "waits on oc-a000002", |
| 216 | } { |
| 217 | if !strings.Contains(page, want) { |
| 218 | t.Errorf("the plan page does not show %q", want) |
| 219 | } |
| 220 | } |
| 221 | if strings.Count(page, "Ready changes") != 1 { |
| 222 | t.Error("the ready changes are not one card") |
| 223 | } |
| 224 | |
| 225 | // The bug nobody claims and nothing blocks is free to start. |
| 226 | if !strings.Contains(page, `<i class="k ready" role="img" aria-label="free to start" title="free to start"></i><span class="body"><a class="id" href="/knots/app/bd-a000004">`) { |
| 227 | t.Error("bd-a000004 is not shown as free to start") |
| 228 | } |
| 229 | |
| 230 | // The milestone's path puts oc-a000003 after the knot it waits on. |
| 231 | paths := section(t, page, `id="paths-h"`, `id="ms-h"`) |
| 232 | second := strings.Index(paths, `<span class="stage-num">2</span>`) |
| 233 | if second < 0 || strings.Index(paths, "oc-a000002") > second || strings.Index(paths, "oc-a000003") < second { |
| 234 | t.Errorf("the path does not order oc-a000003 after oc-a000002:\n%s", paths) |
| 235 | } |
| 236 | |
| 237 | // Nothing of the hidden project is linked or named, and nothing is |
| 238 | // fetched from another origin. |
| 239 | for _, want := range []string{"lands in an unpublished project", "lands in a knot in an unpublished project"} { |
| 240 | if !strings.Contains(page, want) { |
| 241 | t.Errorf("the plan page does not show %q", want) |
| 242 | } |
| 243 | } |
| 244 | for _, unwanted := range append([]string{"/knots/site/", "/source/site/", "http://", "https://", `class="chip stale"`}, unpublishedText...) { |
| 245 | if strings.Contains(page, unwanted) { |
| 246 | t.Errorf("the plan page holds %q", unwanted) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | h.now = exporttest.Collected.Add(6 * time.Minute) |
| 251 | page = h.page("/") |
| 252 | if !strings.Contains(page, `Collected 2026-10-25 12:00 UTC, 6 minutes ago <span class="chip stale">stale</span>`) { |
| 253 | t.Error("an export six minutes old is not marked stale") |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // unpublishedText is what of the hidden project site the fixture's |
| 258 | // references hold: its name and its knots' IDs. No page shows any of it. |
| 259 | var unpublishedText = []string{"site:", "oc-b000001", "oc-b000002", "lands in site", ">site<", "valley:site"} |
| 260 | |
| 261 | // A knot's page shows its body and comments, and marks the edited |
| 262 | // comment. A reference into a hidden project is shown as unpublished, |
| 263 | // with nothing of its text, and a missing knot of a public project is |
| 264 | // named as missing. |
| 265 | func TestKnotPage(t *testing.T) { |
| 266 | h := newHarness(t) |
| 267 | page := h.page("/knots/app/oc-a000002") |
| 268 | for _, want := range []string{ |
| 269 | "Each milestone is listed with its progress.", |
| 270 | "Start with the milestones that have open children.", |
| 271 | "Claimed; the change is ready for approval.", |
| 272 | `<span class="chip quiet">edited</span><span>2026-10-25 10:30 UTC by agent</span>`, |
| 273 | `<a href="/knots/app/oc-a000001">Readers browse the app's plan</a>`, |
| 274 | } { |
| 275 | if !strings.Contains(page, want) { |
| 276 | t.Errorf("the knot's page does not show %q", want) |
| 277 | } |
| 278 | } |
| 279 | if n := strings.Count(page, ">edited<"); n != 1 { |
| 280 | t.Errorf("%d comments are marked edited; one was edited", n) |
| 281 | } |
| 282 | |
| 283 | page = h.page("/knots/app/oc-a000003") |
| 284 | if !strings.Contains(page, `<a href="/knots/app/oc-a000002">The plan page lists milestones</a>`) { |
| 285 | t.Error("the local blocker is not linked") |
| 286 | } |
| 287 | for _, want := range []string{ |
| 288 | `<span class="nochild"></span><span class="t"><span class="muted">a knot in an unpublished project</span></span>`, |
| 289 | "<dt>Lands in</dt><dd>an unpublished project</dd>", |
| 290 | } { |
| 291 | if !strings.Contains(page, want) { |
| 292 | t.Errorf("the knot's page does not show %q", want) |
| 293 | } |
| 294 | } |
| 295 | page += h.page("/knots/app/bd-a000004") |
| 296 | if !strings.Contains(page, "<dt>Lands in</dt><dd>a knot in an unpublished project</dd>") { |
| 297 | t.Error("a target in the hidden project is not shown as unpublished") |
| 298 | } |
| 299 | for _, unwanted := range append([]string{"/knots/site/", "/source/site/"}, unpublishedText...) { |
| 300 | if strings.Contains(page, unwanted) { |
| 301 | t.Errorf("the knot's page shows %q", unwanted) |
| 302 | } |
| 303 | } |
| 304 | if w := h.get("/knots/app/oc-fffffff"); w.Code != http.StatusNotFound { |
| 305 | t.Errorf("a missing knot answered %d", w.Code) |
| 306 | } |
| 307 | |
| 308 | // A knot of a public project the graph does not hold, and a local |
| 309 | // knot that is not there, are named as missing. A change of the |
| 310 | // hidden project is not named. |
| 311 | h.publish(exporttest.SignedAt.Add(time.Minute), nil, func(e *export.Export) { |
| 312 | for i := range e.Projects[0].Knots { |
| 313 | if k := &e.Projects[0].Knots[i]; k.ID == "oc-a000003" { |
| 314 | k.BlockedBy = append(k.BlockedBy, "app:oc-fffffff", "oc-eeeeeee") |
| 315 | k.Edges = append(k.Edges, export.Edge{Field: "blocked_by", Ref: "app:oc-fffffff", Project: "app", ID: "oc-fffffff", State: export.EdgeDangling}) |
| 316 | k.Changes = append(k.Changes, "valley:site/kmnopqrstuvwxyzkmnopqrstuvwxyzkm", "valley:app/kmnopqrstuvwxyzkmnopqrstuvwxyzkm") |
| 317 | } |
| 318 | } |
| 319 | }) |
| 320 | page = h.page("/knots/app/oc-a000003") |
| 321 | for _, want := range []string{ |
| 322 | `<span class="mono">app:oc-fffffff</span> <span class="muted">not found in its graph</span>`, |
| 323 | `<span class="mono">oc-eeeeeee</span> <span class="muted">not found</span>`, |
| 324 | `<li class="mono">a change in an unpublished project</li>`, |
| 325 | `<li class="mono">valley:app/kmnopqrstuvwxyzkmnopqrstuvwxyzkm</li>`, |
| 326 | } { |
| 327 | if !strings.Contains(page, want) { |
| 328 | t.Errorf("the knot's page does not show %q", want) |
| 329 | } |
| 330 | } |
| 331 | for _, unwanted := range unpublishedText { |
| 332 | if strings.Contains(page, unwanted) { |
| 333 | t.Errorf("the knot's page shows %q", unwanted) |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // A project the export does not hold as public answers every route |
| 339 | // exactly as a project that does not exist, even when its repository is |
| 340 | // in the generation's directory. |
| 341 | func TestHiddenProjectAnswersLikeMissing(t *testing.T) { |
| 342 | h := newHarness(t) |
| 343 | g := h.active() |
| 344 | if err := os.CopyFS(filepath.Join(g.Dir, "repos", "site.git"), os.DirFS(h.f.Repos["site"])); err != nil { |
| 345 | t.Fatal(err) |
| 346 | } |
| 347 | siteTip := git(t, h.f.Repos["site"], nil, "rev-parse", export.MainRef) |
| 348 | notFound := h.get("/knots/app/oc-fffffff") |
| 349 | for _, route := range []string{ |
| 350 | "/knots/%s/oc-b000001", |
| 351 | "/source/%s", |
| 352 | "/source/%s/", |
| 353 | "/source/%s/tree/" + siteTip + "/", |
| 354 | "/source/%s/blob/" + siteTip + "/README.md", |
| 355 | "/source/%s/log/" + siteTip, |
| 356 | "/source/%s/tree/" + siteTip + "/no/such/path/", |
| 357 | "/source/%s/anything", |
| 358 | } { |
| 359 | hidden, missing := h.get(fmt.Sprintf(route, "site")), h.get(fmt.Sprintf(route, "nosuch")) |
| 360 | if hidden.Code != http.StatusNotFound || missing.Code != http.StatusNotFound { |
| 361 | t.Errorf("%s answered %d hidden and %d missing", route, hidden.Code, missing.Code) |
| 362 | } |
| 363 | if fmt.Sprint(hidden.Header()) != fmt.Sprint(missing.Header()) { |
| 364 | t.Errorf("%s answered with other headers hidden than missing:\n%v\n%v", route, hidden.Header(), missing.Header()) |
| 365 | } |
| 366 | if !bytes.Equal(hidden.Body.Bytes(), missing.Body.Bytes()) || !bytes.Equal(hidden.Body.Bytes(), notFound.Body.Bytes()) { |
| 367 | t.Errorf("%s answered with another page hidden than missing", route) |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // A source route serves a commit only when main's published tip reaches |
| 373 | // it, and refuses every other object ID or name with the one 404. |
| 374 | func TestUnreachableCommitIsRefused(t *testing.T) { |
| 375 | h := newHarness(t) |
| 376 | repo, _ := h.active().Repo("app") |
| 377 | tip := h.tip() |
| 378 | tree := git(t, repo, nil, "rev-parse", tip+"^{tree}") |
| 379 | readme := git(t, repo, nil, "rev-parse", tip+":README.md") |
| 380 | side := git(t, repo, nil, "commit-tree", tree, "-p", tip, "-m", "a commit main does not reach") |
| 381 | parent := git(t, repo, nil, "rev-parse", tip+"~2") |
| 382 | notFound := h.get("/knots/app/oc-fffffff").Body.String() |
| 383 | |
| 384 | for _, commit := range []string{side, tree, readme, tip[:12], "main", "HEAD", strings.Repeat("0", 40), tip + "~1"} { |
| 385 | for _, route := range []string{"/source/app/tree/%s/", "/source/app/blob/%s/README.md", "/source/app/log/%s"} { |
| 386 | target := fmt.Sprintf(route, commit) |
| 387 | w := h.get(target) |
| 388 | if w.Code != http.StatusNotFound || w.Body.String() != notFound { |
| 389 | t.Errorf("GET %s answered %d, not the one 404", target, w.Code) |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // A commit the tip reaches is served, as that commit had it. |
| 395 | page := h.page("/source/app/blob/" + parent + "/README.md") |
| 396 | if !strings.Contains(page, "A synthetic project for the web UI's tests.") || strings.Contains(page, "readme") { |
| 397 | t.Errorf("the first README is not served as the first commit had it:\n%s", page) |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // The source browser lists a directory and its README, shows a file and |
| 402 | // main's log, and every source link names its project, a full commit and |
| 403 | // a path. |
| 404 | func TestSourceBrowser(t *testing.T) { |
| 405 | h := newHarness(t) |
| 406 | tip := h.tip() |
| 407 | |
| 408 | w := h.get("/source/app/") |
| 409 | if w.Code != http.StatusFound || w.Header().Get("Location") != "/source/app/tree/"+tip+"/" { |
| 410 | t.Fatalf("the project's source answered %d to %q", w.Code, w.Header().Get("Location")) |
| 411 | } |
| 412 | |
| 413 | root := h.page("/source/app/tree/" + tip + "/") |
| 414 | docs, src, readme := strings.Index(root, ">docs/<"), strings.Index(root, ">src/<"), strings.Index(root, ">README.md<") |
| 415 | if docs < 0 || src < 0 || readme < 0 || readme < src || src < docs { |
| 416 | t.Errorf("the root does not list docs/ and src/ before README.md:\n%s", root) |
| 417 | } |
| 418 | if !strings.Contains(root, `<pre class="text"># app`) { |
| 419 | t.Error("the root does not show its README") |
| 420 | } |
| 421 | if dir := h.page("/source/app/tree/" + tip + "/docs/"); !strings.Contains(dir, ">guide.md<") { |
| 422 | t.Error("docs/ does not list guide.md") |
| 423 | } |
| 424 | file := h.page("/source/app/blob/" + tip + "/docs/guide.md") |
| 425 | if !strings.Contains(file, `<tr id="L1"><td class="ln"><a href="#L1">1</a></td><td class="src"># Guide</td></tr>`) { |
| 426 | t.Errorf("the file is not shown line by line:\n%s", file) |
| 427 | } |
| 428 | log := h.page("/source/app/log/" + tip) |
| 429 | first, last := strings.Index(log, "docs: a guide, and the README names it"), strings.Index(log, "app: the first README") |
| 430 | if first < 0 || last < first { |
| 431 | t.Errorf("the log does not list main's commits newest first:\n%s", log) |
| 432 | } |
| 433 | |
| 434 | links := regexp.MustCompile(`href="(/source/[^"]*)"`) |
| 435 | named := regexp.MustCompile(`^/source/app/(tree|blob|log)/[0-9a-f]{40}(/|$)`) |
| 436 | for _, page := range []string{root, file, log} { |
| 437 | for _, m := range links.FindAllStringSubmatch(page, -1) { |
| 438 | if !named.MatchString(m[1]) { |
| 439 | t.Errorf("the link %s names no commit", m[1]) |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | for _, target := range []string{ |
| 445 | "/source/app/tree/" + tip + "/README.md/", |
| 446 | "/source/app/blob/" + tip + "/docs", |
| 447 | "/source/app/blob/" + tip + "/docs/guide.md/", |
| 448 | "/source/app/blob/" + tip + "/docs/../README.md", |
| 449 | "/source/app/blob/" + tip + "/docs%2Fguide.md", |
| 450 | "/source/app/tree/" + tip + "//", |
| 451 | "/source/app/log/" + tip + "?skip=-1", |
| 452 | } { |
| 453 | if w := h.get(target); w.Code != http.StatusNotFound { |
| 454 | t.Errorf("GET %s answered %d", target, w.Code) |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // file is one file a test commits: its mode, path and content. |
| 460 | type file struct{ mode, path, content string } |
| 461 | |
| 462 | // commitMain writes a commit onto the fixture's app main, with message, |
| 463 | // that holds the files given beside what main holds. It returns the new |
| 464 | // tip and a bundle of it. Each content is written once, however many |
| 465 | // files hold it. |
| 466 | func (h *harness) commitMain(message string, files ...file) (tip, bundle string) { |
| 467 | t := h.t |
| 468 | t.Helper() |
| 469 | repo := h.f.Repos["app"] |
| 470 | var b bytes.Buffer |
| 471 | data := func(s string) { fmt.Fprintf(&b, "data %d\n%s\n", len(s), s) } |
| 472 | marks := map[string]int{} |
| 473 | for _, f := range files { |
| 474 | if _, ok := marks[f.content]; !ok { |
| 475 | marks[f.content] = len(marks) + 1 |
| 476 | fmt.Fprintf(&b, "blob\nmark :%d\n", marks[f.content]) |
| 477 | data(f.content) |
| 478 | } |
| 479 | } |
| 480 | who := "Example Author <author@example.org> 1791190800 +0000" |
| 481 | fmt.Fprintf(&b, "commit refs/heads/main\nauthor %s\ncommitter %s\n", who, who) |
| 482 | data(message) |
| 483 | fmt.Fprintf(&b, "from %s\n", git(t, repo, nil, "rev-parse", export.MainRef)) |
| 484 | for _, f := range files { |
| 485 | fmt.Fprintf(&b, "M %s :%d %s\n", f.mode, marks[f.content], f.path) |
| 486 | } |
| 487 | git(t, repo, b.Bytes(), "fast-import", "--quiet") |
| 488 | tip = git(t, repo, nil, "rev-parse", export.MainRef) |
| 489 | bundle = filepath.Join(t.TempDir(), "app.bundle") |
| 490 | git(t, repo, nil, "bundle", "create", "--quiet", bundle, "main") |
| 491 | return tip, bundle |
| 492 | } |
| 493 | |
| 494 | // publishMain activates a generation whose app main is tip, carried by |
| 495 | // bundle. |
| 496 | func (h *harness) publishMain(at time.Time, tip, bundle string) { |
| 497 | h.t.Helper() |
| 498 | h.publish(at, map[string]string{"app": bundle}, func(e *export.Export) { e.Projects[0].SourceTip = tip }) |
| 499 | } |
| 500 | |
| 501 | // hostileMain writes a commit onto the fixture's app main that holds a |
| 502 | // file of HTML, a file larger than a page shows, a binary file, a file |
| 503 | // whose name is HTML, and a symbolic link out of the repository. It |
| 504 | // returns the new tip and a bundle of it. |
| 505 | func (h *harness) hostileMain() (tip, bundle string) { |
| 506 | return h.commitMain("hostile: <script>alert(\"subject\")</script>\n", |
| 507 | file{"100644", "page.html", "<html><script>alert(\"file\")</script><img src=x onerror=alert(2)></html>\n"}, |
| 508 | file{"100644", "big.txt", strings.Repeat("big\n", (MaxFile/4)+1)}, |
| 509 | file{"100644", "binary.bin", "\x7fELF\x00\x01\x02<script>alert(\"binary\")</script>"}, |
| 510 | file{"100644", "<img src=x onerror=alert(3)>.txt", "named\n"}, |
| 511 | file{"120000", "link", "../../etc/passwd"}, |
| 512 | ) |
| 513 | } |
| 514 | |
| 515 | // HTML, scripts, large files and binary files from a repository or the |
| 516 | // plan render inert: escaped as text, or not shown. |
| 517 | func TestHostileContentRendersInert(t *testing.T) { |
| 518 | h := newHarness(t) |
| 519 | tip, bundle := h.hostileMain() |
| 520 | h.publish(exporttest.SignedAt.Add(time.Minute), map[string]string{"app": bundle}, func(e *export.Export) { |
| 521 | e.Projects[0].SourceTip = tip |
| 522 | }) |
| 523 | |
| 524 | inert := func(target, page string, wants ...string) { |
| 525 | t.Helper() |
| 526 | // Escaped, a tag is text; only a tag the page did not escape, or |
| 527 | // an attribute it let a value break out into, is active. |
| 528 | for _, active := range []string{"<script", "<img", `" onerror`, "\x00"} { |
| 529 | if strings.Contains(page, active) { |
| 530 | t.Errorf("GET %s holds %q", target, active) |
| 531 | } |
| 532 | } |
| 533 | for _, want := range wants { |
| 534 | if !strings.Contains(page, want) { |
| 535 | t.Errorf("GET %s does not show %q", target, want) |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | at := "/source/app/tree/" + tip + "/" |
| 540 | inert(at, h.page(at), |
| 541 | `<script>alert("readme")</script>`, |
| 542 | `<img src=x onerror=alert(3)>.txt`, |
| 543 | `href="/source/app/blob/`+tip+`/%3Cimg%20src=x%20onerror=alert%283%29%3E.txt"`) |
| 544 | at = "/source/app/blob/" + tip + "/%3Cimg%20src=x%20onerror=alert%283%29%3E.txt" |
| 545 | inert(at, h.page(at), ">named<") |
| 546 | at = "/source/app/blob/" + tip + "/page.html" |
| 547 | inert(at, h.page(at), `<html><script>alert("file")</script><img src=x onerror=alert(2)></html>`) |
| 548 | at = "/source/app/blob/" + tip + "/big.txt" |
| 549 | page := h.page(at) |
| 550 | inert(at, page, "This file is 1.0 MiB, larger than the 1.0 MiB a page shows.") |
| 551 | if len(page) > 64<<10 { |
| 552 | t.Errorf("the large file's page is %d bytes", len(page)) |
| 553 | } |
| 554 | at = "/source/app/blob/" + tip + "/binary.bin" |
| 555 | inert(at, h.page(at), "This file is binary, 39 bytes, and is not shown.") |
| 556 | at = "/source/app/blob/" + tip + "/link" |
| 557 | inert(at, h.page(at), "A symbolic link to ../../etc/passwd. It is not followed.") |
| 558 | at = "/source/app/log/" + tip |
| 559 | inert(at, h.page(at), "hostile: <script>alert("subject")</script>") |
| 560 | |
| 561 | at = "/knots/app/bd-a000004" |
| 562 | inert(at, h.page(at), |
| 563 | `A README holding <script>alert("knot")</script> must show as text.`, |
| 564 | `Also <img src=x onerror=alert(1)> in a comment.`) |
| 565 | } |
| 566 | |
| 567 | // A directory larger than a page reads is listed in part, and a commit |
| 568 | // message larger than a page reads ends its page of history early. git |
| 569 | // is stopped once its listing passes MaxRead, and nothing past a record's |
| 570 | // bound is kept. |
| 571 | func TestOversizedTreeAndHistory(t *testing.T) { |
| 572 | h := newHarness(t) |
| 573 | // Each entry of huge/ takes 308 bytes of git ls-tree's output, so |
| 574 | // 15,000 of them pass MaxRead at about entry 13,600. |
| 575 | longest := "!" + strings.Repeat("l", 4095) |
| 576 | files := []file{{"100644", "huge/" + longest, "x\n"}} |
| 577 | name := func(i int) string { return fmt.Sprintf("%05d-%s", i, strings.Repeat("n", 240)) } |
| 578 | for i := range 15_000 { |
| 579 | files = append(files, file{"100644", "huge/" + name(i), "x\n"}) |
| 580 | } |
| 581 | if n := 15_000 * (6 + 1 + 4 + 1 + 40 + 1 + 7 + 1 + 246 + 1); n <= MaxRead { |
| 582 | t.Fatalf("the listing of huge/ is %d bytes, within MaxRead", n) |
| 583 | } |
| 584 | h.commitMain("app: a directory larger than a page reads\n", files...) |
| 585 | h.commitMain("long: " + strings.Repeat("a long subject ", (100<<10)/15) + "\n") |
| 586 | tip, bundle := h.commitMain("longer: " + strings.Repeat("a subject past the bound ", (5<<20)/25) + "\n") |
| 587 | h.publishMain(exporttest.SignedAt.Add(time.Minute), tip, bundle) |
| 588 | |
| 589 | at := "/source/app/tree/" + tip + "/huge/" |
| 590 | page := h.page(at) |
| 591 | if !strings.Contains(page, "This directory is larger than a page reads, so only part of it is listed.") { |
| 592 | t.Error("huge/ is not marked as listed in part") |
| 593 | } |
| 594 | if n := strings.Count(page, `<tr><td class="name">`); n != MaxEntries { |
| 595 | t.Errorf("huge/ lists %d entries, not %d", n, MaxEntries) |
| 596 | } |
| 597 | if !strings.Contains(page, `">`+longest+`</a>`) { |
| 598 | t.Error("the entry with the longest name git accepts is not listed whole") |
| 599 | } |
| 600 | if len(page) > 1<<20 { |
| 601 | t.Errorf("huge/'s page is %d bytes", len(page)) |
| 602 | } |
| 603 | if w := h.get("/source/app/blob/" + tip + "/huge/" + name(0)); w.Code != http.StatusOK { |
| 604 | t.Errorf("an entry read before MaxRead answered %d", w.Code) |
| 605 | } |
| 606 | if w := h.get("/source/app/blob/" + tip + "/huge/" + name(14_999)); w.Code != http.StatusNotFound { |
| 607 | t.Errorf("an entry past MaxRead answered %d", w.Code) |
| 608 | } |
| 609 | |
| 610 | at = "/source/app/log/" + tip |
| 611 | page = h.page(at) |
| 612 | if !strings.Contains(page, "This page ends early: its commits are larger than a page reads.") { |
| 613 | t.Error("the page of history holding a 5 MiB message does not end early") |
| 614 | } |
| 615 | if n := strings.Count(page, `<tr><td class="name">`); n != 1 { |
| 616 | t.Errorf("the page that ends early lists %d commits, not the one it cut", n) |
| 617 | } |
| 618 | if m := regexp.MustCompile(`<td>(longer: a subject past the bound[^<]*)…</td>`).FindStringSubmatch(page); m == nil || len(m[1]) > maxLogText { |
| 619 | t.Error("the cut commit's subject is not shown cut to maxLogText") |
| 620 | } |
| 621 | if !strings.Contains(page, `href="/source/app/log/`+tip+`?skip=1"`) { |
| 622 | t.Error("the page that ends early does not link the commits after the one it cut") |
| 623 | } |
| 624 | page = h.page(at + "?skip=1") |
| 625 | if strings.Contains(page, "ends early") || !strings.Contains(page, "long: a long subject") || !strings.Contains(page, "app: the first README") { |
| 626 | t.Errorf("the next page of history is not whole:\n%s", page) |
| 627 | } |
| 628 | if strings.Contains(page, strings.Repeat("a long subject ", (maxLogText/15)+1)) { |
| 629 | t.Error("a subject longer than a page shows is not cut") |
| 630 | } |
| 631 | for _, p := range []string{at, at + "?skip=1"} { |
| 632 | if n := len(h.page(p)); n > 64<<10 { |
| 633 | t.Errorf("GET %s is %d bytes", p, n) |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // A file of exactly MaxFile bytes of newlines is shown to MaxLines lines, |
| 639 | // and its page says where it stops. A README longer than a page shows is |
| 640 | // shown in part. |
| 641 | func TestLongFileIsShownInPart(t *testing.T) { |
| 642 | h := newHarness(t) |
| 643 | tip, bundle := h.commitMain("app: long files\n", |
| 644 | file{"100644", "newlines.txt", strings.Repeat("\n", MaxFile)}, |
| 645 | file{"100644", "long/README.md", strings.Repeat("a line of the README\n", 2*MaxLines)}, |
| 646 | file{"100644", "wide.txt", strings.Repeat("<", MaxFile)}, |
| 647 | ) |
| 648 | h.publishMain(exporttest.SignedAt.Add(time.Minute), tip, bundle) |
| 649 | |
| 650 | page := h.page("/source/app/blob/" + tip + "/newlines.txt") |
| 651 | last := fmt.Sprintf(`id="L%d"`, MaxLines) |
| 652 | if !strings.Contains(page, last) || strings.Contains(page, fmt.Sprintf(`id="L%d"`, MaxLines+1)) { |
| 653 | t.Errorf("the file of newlines is not shown to line %d", MaxLines) |
| 654 | } |
| 655 | if want := fmt.Sprintf("The page stops at line %d of %d.", MaxLines, MaxFile); !strings.Contains(page, want) { |
| 656 | t.Errorf("the page does not say %q", want) |
| 657 | } |
| 658 | if len(page) > MaxShown { |
| 659 | t.Errorf("the page of a file of newlines is %d bytes, past MaxShown", len(page)) |
| 660 | } |
| 661 | |
| 662 | // One line of MaxFile characters that each escape to four bytes is |
| 663 | // cut where MaxShown is reached. |
| 664 | page = h.page("/source/app/blob/" + tip + "/wide.txt") |
| 665 | if !strings.Contains(page, "The page stops at line 1 of 1.") || len(page) > MaxShown+64<<10 { |
| 666 | t.Errorf("the page of one wide line is %d bytes", len(page)) |
| 667 | } |
| 668 | |
| 669 | page = h.page("/source/app/tree/" + tip + "/long/") |
| 670 | if !strings.Contains(page, "This README is longer than a page shows, and only its start is shown.") { |
| 671 | t.Error("the long README is not marked as shown in part") |
| 672 | } |
| 673 | if n := strings.Count(page, "a line of the README"); n != MaxLines { |
| 674 | t.Errorf("the long README shows %d lines, not %d", n, MaxLines) |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // A chain of knots as deep as the graph's knot limit allows renders its |
| 679 | // milestone to MaxPlanDepth levels, counts every knot under it, and |
| 680 | // names MaxPlanDepth parents in the deepest knot's trail. The plan page's |
| 681 | // structure is built once for the generation. Siblings that block one |
| 682 | // another in a cycle share the last stage of their path. |
| 683 | func TestDeepPlan(t *testing.T) { |
| 684 | h := newHarness(t) |
| 685 | var chain []string |
| 686 | h.publish(exporttest.SignedAt.Add(time.Minute), nil, func(e *export.Export) { |
| 687 | p := &e.Projects[0] |
| 688 | for i := range knot.MaxKnots - len(p.Knots) - 3 { |
| 689 | id := fmt.Sprintf("oc-c%06x", i) |
| 690 | k := export.Knot{ID: id, Type: "outcome", Status: "open", Active: true, State: "ready", |
| 691 | Title: fmt.Sprintf("Level %d of the chain", i), Created: "2026-10-01", CreatedBy: "operator"} |
| 692 | if i == 0 { |
| 693 | p.Roots = append(p.Roots, id) |
| 694 | } else { |
| 695 | k.Parent = chain[i-1] |
| 696 | p.Knots[len(p.Knots)-1].Children = []string{id} |
| 697 | } |
| 698 | chain = append(chain, id) |
| 699 | p.Knots = append(p.Knots, k) |
| 700 | } |
| 701 | // Under the first link, a path whose two last siblings wait on |
| 702 | // each other. |
| 703 | free, a, b := "oc-d000001", "oc-d000002", "oc-d000003" |
| 704 | for _, k := range []export.Knot{ |
| 705 | {ID: free, BlockedBy: []string{}}, |
| 706 | {ID: a, BlockedBy: []string{free, b}}, |
| 707 | {ID: b, BlockedBy: []string{a}}, |
| 708 | } { |
| 709 | k.Type, k.Status, k.State, k.Title, k.Parent = "outcome", "open", "blocked", "Sibling "+k.ID, chain[0] |
| 710 | p.Knots = append(p.Knots, k) |
| 711 | } |
| 712 | for i := range p.Knots { |
| 713 | if p.Knots[i].ID == chain[0] { |
| 714 | p.Knots[i].Children = append(p.Knots[i].Children, free, a, b) |
| 715 | } |
| 716 | } |
| 717 | }) |
| 718 | |
| 719 | page := h.page("/") |
| 720 | if want := fmt.Sprintf("%d of %d done", 0, len(chain)-1+3); !strings.Contains(page, want) { |
| 721 | t.Errorf("the chain's milestone does not count every knot under it as %q", want) |
| 722 | } |
| 723 | if !strings.Contains(page, `href="/knots/app/`+chain[MaxPlanDepth]+`"`) || strings.Contains(page, `href="/knots/app/`+chain[MaxPlanDepth+1]+`"`) { |
| 724 | t.Errorf("the chain's tree is not shown to %d levels", MaxPlanDepth) |
| 725 | } |
| 726 | if n := strings.Count(page, ">more on its page<"); n != 1 { |
| 727 | t.Errorf("%d rows say there is more on their page; the chain's deepest shown row does", n) |
| 728 | } |
| 729 | if len(page) > 256<<10 { |
| 730 | t.Errorf("the plan page is %d bytes", len(page)) |
| 731 | } |
| 732 | paths := section(t, page, `id="paths-h"`, `id="ms-h"`) |
| 733 | stage := regexp.MustCompile(`<span class="stage-num">(\d+)</span><ul class="plain">(.*?)</ul>`) |
| 734 | var stages []string |
| 735 | for _, m := range stage.FindAllStringSubmatch(paths, -1) { |
| 736 | if strings.Contains(m[2], "Sibling") { |
| 737 | var ids []string |
| 738 | for _, id := range regexp.MustCompile(`"mono muted">(oc-d00000\d)<`).FindAllStringSubmatch(m[2], -1) { |
| 739 | ids = append(ids, id[1]) |
| 740 | } |
| 741 | stages = append(stages, m[1]+":"+strings.Join(ids, ",")) |
| 742 | } |
| 743 | } |
| 744 | if got := strings.Join(stages, " "); got != "1:oc-d000001 2:oc-d000002,oc-d000003" { |
| 745 | t.Errorf("the siblings' path has stages %q", got) |
| 746 | } |
| 747 | |
| 748 | h.srv.mu.Lock() |
| 749 | plan := h.srv.cached.idx.plan |
| 750 | h.srv.mu.Unlock() |
| 751 | h.page("/") |
| 752 | h.srv.mu.Lock() |
| 753 | again := h.srv.cached.idx.plan |
| 754 | h.srv.mu.Unlock() |
| 755 | if plan != again { |
| 756 | t.Error("the plan page's structure was built again for the same generation") |
| 757 | } |
| 758 | |
| 759 | deepest := chain[len(chain)-1] |
| 760 | page = h.page("/knots/app/" + deepest) |
| 761 | trail := section(t, page, `<nav class="trail"`, `</nav>`) |
| 762 | if n := strings.Count(trail, `<a href="/knots/`); n != MaxPlanDepth || !strings.Contains(trail, "<span>…</span>") { |
| 763 | t.Errorf("the deepest knot's trail names %d parents, or does not mark those above them", n) |
| 764 | } |
| 765 | if !strings.Contains(trail, chain[len(chain)-2]) || strings.Contains(trail, `"/knots/app/`+chain[len(chain)-2-MaxPlanDepth]+`"`) { |
| 766 | t.Error("the trail does not name the nearest parents alone") |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // Every response carries the strict policy, and the policy admits the |
| 771 | // page's one inline stylesheet and one inline script by their hashes and |
| 772 | // nothing from another origin. |
| 773 | func TestEveryResponseCarriesTheCSP(t *testing.T) { |
| 774 | h := newHarness(t) |
| 775 | for _, c := range []struct{ method, target string }{ |
| 776 | {"GET", "/"}, |
| 777 | {"GET", "/knots/app/oc-a000002"}, |
| 778 | {"GET", "/source/app/"}, |
| 779 | {"GET", "/source/app/tree/" + h.tip() + "/"}, |
| 780 | {"GET", "/source/app/blob/" + h.tip() + "/README.md"}, |
| 781 | {"GET", "/no/such/page"}, |
| 782 | {"POST", "/"}, |
| 783 | {"HEAD", "/"}, |
| 784 | } { |
| 785 | w := h.do(c.method, c.target) |
| 786 | got := w.Header().Get("Content-Security-Policy") |
| 787 | if got != csp || w.Header().Get("X-Content-Type-Options") != "nosniff" { |
| 788 | t.Errorf("%s %s answered with policy %q", c.method, c.target, got) |
| 789 | } |
| 790 | } |
| 791 | for _, directive := range []string{"default-src 'self'", "object-src 'none'", "base-uri 'none'", "frame-ancestors 'none'"} { |
| 792 | if !strings.Contains(csp, directive) { |
| 793 | t.Errorf("the policy lacks %s", directive) |
| 794 | } |
| 795 | } |
| 796 | for _, loose := range []string{"unsafe-inline", "unsafe-eval", "http", "*", "data:"} { |
| 797 | if strings.Contains(csp, loose) { |
| 798 | t.Errorf("the policy holds %q", loose) |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | page := h.page("/") |
| 803 | inline := func(tag string) string { |
| 804 | t.Helper() |
| 805 | m := regexp.MustCompile(`(?s)<`+tag+`>(.*?)</`+tag+`>`).FindAllStringSubmatch(page, -1) |
| 806 | if len(m) != 1 || strings.Count(page, "<"+tag) != 1 { |
| 807 | t.Fatalf("the plan page has %d %s elements; it has one", len(m), tag) |
| 808 | } |
| 809 | sum := sha256.Sum256([]byte(m[0][1])) |
| 810 | return "'sha256-" + base64.StdEncoding.EncodeToString(sum[:]) + "'" |
| 811 | } |
| 812 | if hash := inline("script"); !strings.Contains(csp, "script-src "+hash+";") { |
| 813 | t.Errorf("the policy does not admit the page's script, %s", hash) |
| 814 | } |
| 815 | if hash := inline("style"); !strings.Contains(csp, "style-src "+hash+";") { |
| 816 | t.Errorf("the policy does not admit the page's stylesheet, %s", hash) |
| 817 | } |
| 818 | for _, external := range []string{"<link", " src=", "@import", "url("} { |
| 819 | if strings.Contains(page, external) { |
| 820 | t.Errorf("the plan page holds %q", external) |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // A request serves everything from the generation it opened, though |
| 826 | // another activates while it runs, and that generation outlives the |
| 827 | // activation until the request releases it. |
| 828 | func TestARequestKeepsItsGeneration(t *testing.T) { |
| 829 | h := newHarness(t) |
| 830 | paused, resume := make(chan export.Generation), make(chan struct{}) |
| 831 | var once sync.Once |
| 832 | h.srv.opened = func(g export.Generation) { once.Do(func() { paused <- g; <-resume }) } |
| 833 | served := make(chan *httptest.ResponseRecorder) |
| 834 | go func() { |
| 835 | w := httptest.NewRecorder() |
| 836 | h.srv.ServeHTTP(w, httptest.NewRequest("GET", "/knots/app/oc-a000001", nil)) |
| 837 | served <- w |
| 838 | }() |
| 839 | old := <-paused |
| 840 | |
| 841 | renamed := func(title string) func(*export.Export) { |
| 842 | return func(e *export.Export) { e.Projects[0].Knots[0].Title = title } |
| 843 | } |
| 844 | next := h.publish(exporttest.SignedAt.Add(time.Minute), nil, renamed("Readers browse the second plan")) |
| 845 | if next.Sequence == old.Sequence { |
| 846 | t.Fatal("the publish did not activate a new generation") |
| 847 | } |
| 848 | if _, err := os.Stat(old.Dir); err != nil { |
| 849 | t.Fatalf("the generation a request holds was deleted: %v", err) |
| 850 | } |
| 851 | close(resume) |
| 852 | w := <-served |
| 853 | if body := w.Body.String(); w.Code != http.StatusOK || !strings.Contains(body, "Readers browse the app's plan") || strings.Contains(body, "second plan") { |
| 854 | t.Errorf("the request did not serve the generation it opened: %d\n%s", w.Code, body) |
| 855 | } |
| 856 | if !strings.Contains(h.page("/knots/app/oc-a000001"), "Readers browse the second plan") { |
| 857 | t.Error("the next request does not serve the new generation") |
| 858 | } |
| 859 | |
| 860 | // Released, the old generation goes with the next activation. |
| 861 | h.publish(exporttest.SignedAt.Add(2*time.Minute), nil, renamed("Readers browse the third plan")) |
| 862 | if _, err := os.Stat(old.Dir); !os.IsNotExist(err) { |
| 863 | t.Errorf("the released generation is still on disk: %v", err) |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // Before the first activation the server says nothing is published, and |
| 868 | // it answers GET and HEAD alone. |
| 869 | func TestNothingPublishedAndMethods(t *testing.T) { |
| 870 | store, err := export.OpenStore(t.TempDir(), nil) |
| 871 | if err != nil { |
| 872 | t.Fatal(err) |
| 873 | } |
| 874 | srv := New(Config{Store: store, Log: func(err error) { t.Errorf("the server logged: %v", err) }}) |
| 875 | w := httptest.NewRecorder() |
| 876 | srv.ServeHTTP(w, httptest.NewRequest("GET", "/", nil)) |
| 877 | if w.Code != http.StatusServiceUnavailable || !strings.Contains(w.Body.String(), "Nothing published yet") { |
| 878 | t.Errorf("an empty store answered %d:\n%s", w.Code, w.Body) |
| 879 | } |
| 880 | |
| 881 | h := newHarness(t) |
| 882 | for _, method := range []string{"POST", "PUT", "DELETE", "OPTIONS"} { |
| 883 | w := h.do(method, "/") |
| 884 | if w.Code != http.StatusMethodNotAllowed || w.Header().Get("Allow") != "GET, HEAD" { |
| 885 | t.Errorf("%s answered %d, allowing %q", method, w.Code, w.Header().Get("Allow")) |
| 886 | } |
| 887 | } |
| 888 | } |