Files
pages/internal/server/browser_upload_test.go
T
Anya Tokugawa b90aef79d0
Build and publish container images / images (push) Successful in 11m37s
Release container images / release (push) Successful in 22s
feat: add browser upload queue and trusted sessions with bounded projections
2026-11-08 20:27:04 +09:00

89 lines
2.7 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestBrowserUploadQueueChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
for _, width := range []string{"1280", "390"} {
t.Run(width, func(t *testing.T) { testBrowserUploadQueueChromium(t, width) })
}
}
func testBrowserUploadQueueChromium(t *testing.T, width string) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/v/upload", s.handleUpload)
object, err := s.store.CreatePageAtPathInDomain(domain, "/drop/existing.txt")
if err != nil {
t.Fatal(err)
}
if _, err = s.store.SaveRevisionContent(object, []byte("original"), "", "und", "text/plain", "existing.txt"); err != nil {
t.Fatal(err)
}
if _, err = s.store.CreatePageAtPathInDomain(domain, "/drop/sub/seed"); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/browser-upload.js")
if err != nil {
t.Fatal(err)
}
var active, peak atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
if r.Method == "POST" && r.URL.Path == "/v/upload" {
n := active.Add(1)
defer active.Add(-1)
if n > peak.Load() {
peak.Store(n)
}
time.Sleep(100 * time.Millisecond)
}
if r.URL.Path == "/v/browser/drop" && r.URL.Query().Has("fixture") {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for key, values := range rec.Header() {
w.Header()[key] = values
}
w.WriteHeader(rec.Code)
w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/browser/drop?fixture=1", t.TempDir(), "/tmp/pageswiki-browser-upload-"+width+".png", width, "850").CombinedOutput()
if err != nil {
t.Fatalf("browser: %v %s", err, out)
}
if peak.Load() != 1 {
t.Fatalf("parallel uploads: %d", peak.Load())
}
for _, path := range []string{"/drop/日本語 #1.txt", "/drop/sub/nested.txt"} {
if _, err = s.store.ResolvePathInDomain(domain, path); err != nil {
t.Fatal(path, err)
}
}
if _, err = s.store.ResolvePathInDomain(domain, "/drop/cancel.txt"); err == nil {
t.Fatal("cancelled file uploaded")
}
revs, err := s.store.ListRevisions(object)
if err != nil || len(revs) != 1 {
t.Fatal("existing file overwritten")
}
}