package main import ( "io/fs" "net/http" "strings" "testing" "testing/fstest" ) func TestLoadReleases(t *testing.T) { fsys := fstest.MapFS{ "README.md": {Data: []byte("# Index, not a release\n")}, "001-first/README.md": {Data: []byte("# First\n\nBranch `a` · version (none)\n\nBody one.\n")}, "002-second/README.md": {Data: []byte("# Second\r\n\r\nBranch `b` · version 0.2.0\r\n\r\n- **Version:** 0.2.0\r\n")}, "002-second/desktop.png": {Data: []byte("png")}, "003-no-title/README.md": {Data: []byte("No heading here.\n")}, "004-no-readme/x.png": {Data: []byte("png")}, "Bad_Name/README.md": {Data: []byte("# Bad\n")}, } list, byID, err := loadReleases(fsys, newMarkdown()) if err != nil { t.Fatal(err) } if len(list) != 2 || list[0].ID != "002-second" || list[1].ID != "001-first" { t.Fatalf("got %d releases, want 002 then 001: %+v", len(list), list) } r := byID["002-second"] if r.Number != "002" || r.Title != "Second" || r.Version != "0.2.0" { t.Errorf("002: %+v", r) } if byID["001-first"].Version != "" { t.Errorf("001: version = %q, want none", byID["001-first"].Version) } if strings.Contains(string(r.HTML), "
`,
``,
} {
if !strings.Contains(body, want) {
t.Errorf("release page: missing %q", want)
}
}
status, h, img := get(t, srv, "/releases/"+id+"/desktop.png")
if status != 200 || h.Get("Content-Type") != "image/png" || !strings.HasPrefix(img, "\x89PNG") {
t.Errorf("screenshot: status = %d, type = %q", status, h.Get("Content-Type"))
}
for _, path := range []string{
"/releases/nope/",
"/releases/nope",
"/releases/" + id + "/README.md",
"/releases/" + id + "/missing.png",
"/releases/" + id + "/..%2f..%2fmain.go",
"/releases/README.md",
} {
if status, _, _ := get(t, srv, path); status != 404 {
t.Errorf("%s: status = %d, want 404", path, status)
}
}
}
func TestReleaseRedirect(t *testing.T) {
srv, _ := newTestServer(t, nil)
client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
resp, err := client.Get(srv.URL + "/releases/001-initial")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != 301 || resp.Header.Get("Location") != "/releases/001-initial/" {
t.Errorf("status = %d, Location = %q", resp.StatusCode, resp.Header.Get("Location"))
}
}
func TestAboutLinksToReleases(t *testing.T) {
srv, _ := newTestServer(t, nil)
_, _, body := get(t, srv, "/about")
if !strings.Contains(body, "