Files
2026-11-07 00:53:15 +00:00

89 lines
3.1 KiB
Go

package server
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestServeHomeFile(t *testing.T) {
root := t.TempDir()
home := filepath.Join(root, "home")
outside := filepath.Join(root, "outside.txt")
if err := os.Mkdir(home, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(home, "hello.txt"), []byte("hello file contents"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(home, "résumé"), []byte("plain"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(outside, []byte("outside"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(outside, filepath.Join(home, "escape")); err != nil {
t.Fatal(err)
}
originalHomeDirectory := operatorHomeDirectory
operatorHomeDirectory = func() (string, error) { return home, nil }
t.Cleanup(func() { operatorHomeDirectory = originalHomeDirectory })
t.Run("operator home seam", func(t *testing.T) {
recorder := httptest.NewRecorder()
serveHomeFile(recorder, httptest.NewRequest(http.MethodGet, "/files?path=~/hello.txt", nil))
if recorder.Code != http.StatusOK || recorder.Body.String() != "hello file contents" {
t.Fatalf("response = %d %q", recorder.Code, recorder.Body.String())
}
})
tests := []struct {
name string
path string
want int
}{
{"happy path", "~/hello.txt", http.StatusOK},
{"directory refused", "~", http.StatusNotFound},
{"dot dot refused", filepath.Join(home, "..", "outside.txt"), http.StatusForbidden},
{"symlink refused", "~/escape", http.StatusForbidden},
{"missing file", "~/missing", http.StatusNotFound},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/files?path="+tc.path, nil)
recorder := httptest.NewRecorder()
serveHomeFileFrom(recorder, req, home)
if recorder.Code != tc.want {
t.Fatalf("status = %d, want %d", recorder.Code, tc.want)
}
})
}
t.Run("download disposition and range", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/files?path=~/hello.txt&download=1", nil)
req.Header.Set("Range", "bytes=0-4")
recorder := httptest.NewRecorder()
serveHomeFileFrom(recorder, req, home)
if recorder.Code != http.StatusPartialContent || recorder.Body.String() != "hello" {
t.Fatalf("response = %d %q, want 206 hello", recorder.Code, recorder.Body.String())
}
if got := recorder.Header().Get("Content-Disposition"); got != `attachment; filename="hello.txt"; filename*=UTF-8''hello.txt` {
t.Fatalf("disposition = %q", got)
}
if recorder.Header().Get("Content-Type") != "text/plain; charset=utf-8" {
t.Fatalf("content type = %q", recorder.Header().Get("Content-Type"))
}
})
t.Run("non ASCII disposition", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/files?path=~/r%C3%A9sum%C3%A9", nil)
recorder := httptest.NewRecorder()
serveHomeFileFrom(recorder, req, home)
if !strings.Contains(recorder.Header().Get("Content-Disposition"), "filename*=UTF-8''r%C3%A9sum%C3%A9") {
t.Fatal(recorder.Header().Get("Content-Disposition"))
}
})
}