Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
86 lines
3.0 KiB
Go
86 lines
3.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestProjectionMetadataChromium(t *testing.T) {
|
|
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
|
t.Skip("opt-in Chromium")
|
|
}
|
|
for _, mode := range []string{"pdf", "archive", "comic", "failed"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
pdfOutput := "Pages: 3\n"
|
|
if mode == "failed" {
|
|
pdfOutput = "Pages: 10001\n"
|
|
}
|
|
stubMetadataCommands(t, pdfOutput, "Path = page-1.png\nSize = 1\n\nPath = page-2.png\nSize = 1\n\nPath = <img onerror=alert(1)>.png\nSize = 1\n\n")
|
|
path, contentType := "/document.pdf", "application/pdf"
|
|
if mode == "archive" || mode == "comic" {
|
|
path, contentType = "/document.7z", "application/x-7z-compressed"
|
|
}
|
|
saveMetadataDocument(t, s, domain, path, contentType)
|
|
s.mux.HandleFunc(path, s.handleSemanticRoute)
|
|
s.mux.HandleFunc("/v/pdf-page", s.handlePDFPage)
|
|
s.mux.HandleFunc("/v/archive", s.handleArchiveEntry)
|
|
script, err := os.ReadFile("internal/server/testdata/projection-metadata.js")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Host = "localhost"
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
if r.URL.Query().Get("entry") != "" || (r.URL.Path == "/v/pdf-page" && r.URL.Query().Get("metadata") == "") {
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
_, _ = io.WriteString(w, `<svg xmlns="http://www.w3.org/2000/svg" width="400" height="600"><rect width="400" height="600" fill="#607878"/></svg>`)
|
|
return
|
|
}
|
|
if r.URL.Path == path {
|
|
recorder := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(recorder, r)
|
|
for key, values := range recorder.Header() {
|
|
w.Header()[key] = values
|
|
}
|
|
w.WriteHeader(recorder.Code)
|
|
_, _ = io.WriteString(w, strings.Replace(recorder.Body.String(), "</body>", browserFixtureScript(recorder.Header(), script)+"</body>", 1))
|
|
return
|
|
}
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
if r.URL.Query().Get("metadata") == "1" {
|
|
// The real handler returned pending; execute the claimed job in
|
|
// its scheduler goroutine, never inside the request handler.
|
|
go s.runDueEvents()
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
url := server.URL + path + "?fixture=" + mode
|
|
if mode == "comic" {
|
|
url += "&comic=1"
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
|
|
defer cancel()
|
|
screenshot := ""
|
|
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
screenshot = filepath.Join(dir, "projection-metadata-"+mode+".png")
|
|
}
|
|
command := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", url, t.TempDir(), screenshot)
|
|
if output, err := command.CombinedOutput(); err != nil {
|
|
t.Fatalf("browser: %v %s", err, output)
|
|
}
|
|
})
|
|
}
|
|
}
|