95 lines
3.0 KiB
Go
95 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
func musicServer(t *testing.T) (*httptest.Server, string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
write := func(name, content string) {
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write("b_second.MP3", "not really audio, but not empty")
|
|
write("a_first.mp3", "0123456789")
|
|
write("placeholder.mp3", "") // the empty placeholder must not be offered
|
|
write("notes.txt", "hello")
|
|
write(".hidden.mp3", "secret")
|
|
if err := os.Mkdir(filepath.Join(dir, "sub"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
site := fstest.MapFS{"index.html": {Data: []byte("<h1>game</h1>")}}
|
|
return httptest.NewServer(newMux(site, dir)), dir
|
|
}
|
|
|
|
func get(t *testing.T, url string, header ...string) (*http.Response, string) {
|
|
t.Helper()
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
for i := 0; i+1 < len(header); i += 2 {
|
|
req.Header.Set(header[i], header[i+1])
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
buf := make([]byte, 4096)
|
|
n, _ := resp.Body.Read(buf)
|
|
return resp, string(buf[:n])
|
|
}
|
|
|
|
func TestMusicFolder(t *testing.T) {
|
|
srv, _ := musicServer(t)
|
|
defer srv.Close()
|
|
|
|
// the index offers only real, non-empty mp3 files, sorted
|
|
resp, body := get(t, srv.URL+"/music/index.json")
|
|
var idx struct{ Tracks []string }
|
|
if err := json.Unmarshal([]byte(body), &idx); err != nil {
|
|
t.Fatalf("index.json: %v (%s)", err, body)
|
|
}
|
|
if want := []string{"a_first.mp3", "b_second.MP3"}; !reflect.DeepEqual(idx.Tracks, want) {
|
|
t.Errorf("tracks = %v, want %v", idx.Tracks, want)
|
|
}
|
|
if resp.Header.Get("Cache-Control") != "no-store" {
|
|
t.Error("the index must not be cached: tracks are added without a rebuild")
|
|
}
|
|
|
|
// a track is served, and byte ranges work (the browser needs them to seek)
|
|
if r, b := get(t, srv.URL+"/music/a_first.mp3"); r.StatusCode != 200 || b != "0123456789" {
|
|
t.Errorf("track: %d %q", r.StatusCode, b)
|
|
}
|
|
if r, b := get(t, srv.URL+"/music/a_first.mp3", "Range", "bytes=2-4"); r.StatusCode != 206 || b != "234" {
|
|
t.Errorf("range: %d %q", r.StatusCode, b)
|
|
}
|
|
|
|
// nothing else leaks: no listing, no dot files, no sub folders, no way out of the folder
|
|
for _, path := range []string{"/music/", "/music/.hidden.mp3", "/music/sub", "/music/..%2Fmain.go", "/music/%2e%2e/main.go"} {
|
|
if r, _ := get(t, srv.URL+path); r.StatusCode == 200 {
|
|
t.Errorf("%s must not be served", path)
|
|
}
|
|
}
|
|
// the game itself is still served
|
|
if r, b := get(t, srv.URL+"/"); r.StatusCode != 200 || b != "<h1>game</h1>" {
|
|
t.Errorf("site: %d %q", r.StatusCode, b)
|
|
}
|
|
}
|
|
|
|
func TestMusicFolderMissing(t *testing.T) {
|
|
srv := httptest.NewServer(newMux(fstest.MapFS{}, filepath.Join(t.TempDir(), "nope")))
|
|
defer srv.Close()
|
|
resp, body := get(t, srv.URL+"/music/index.json")
|
|
if resp.StatusCode != 200 || body != "{\"tracks\":[]}\n" {
|
|
t.Errorf("a missing music folder should give an empty list, got %d %q", resp.StatusCode, body)
|
|
}
|
|
}
|