Files
Docwell/Server/internal/httpapi/router_test.go
T
tajniak81andClaude Opus 5.5 5e9c61460b Fix databases and live editing in the Android app
- Server: accept the mobile editor's https://app.docwell.docwell_mobile
  origin on /collab (refused since the page moved off file:).
- Android: allow mixed content when signed in to an http:// server, so
  the ws:// collab socket is not blocked from the https page.
- Databases: a touch tap on a cell edits it, since a phone has no
  keyboard to type into a selected cell.
- Back button closes an open record, picker or menu first: the bridge
  reports open overlays and takes a dismiss message.
- Guide: how databases work in the mobile app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:28:30 +01:00

88 lines
2.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package httpapi
import (
"context"
"errors"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type fakeDB struct{ err error }
func (f fakeDB) Health(context.Context) error { return f.err }
func newTestRouter(db Pinger) http.Handler {
return NewRouter(Deps{
Version: "test",
CORSOrigins: []string{"https://app.docwell.test"},
DB: db,
Log: slog.New(slog.NewTextHandler(io.Discard, nil)),
})
}
func do(h http.Handler, method, path string, header map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, nil)
for k, v := range header {
req.Header.Set(k, v)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
func TestHealthAndVersion(t *testing.T) {
h := newTestRouter(fakeDB{})
if rec := do(h, "GET", "/healthz", nil); rec.Code != http.StatusOK {
t.Errorf("/healthz = %d", rec.Code)
}
rec := do(h, "GET", "/api/v1/version", nil)
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"version":"test"`) {
t.Errorf("/api/v1/version = %d %s", rec.Code, rec.Body)
}
if rec := do(h, "GET", "/api/v1/nope", nil); rec.Code != http.StatusNotFound {
t.Errorf("unknown api route = %d", rec.Code)
}
}
func TestReadiness(t *testing.T) {
if rec := do(newTestRouter(fakeDB{}), "GET", "/readyz", nil); rec.Code != http.StatusOK {
t.Errorf("ready = %d", rec.Code)
}
if rec := do(newTestRouter(fakeDB{err: errors.New("down")}), "GET", "/readyz", nil); rec.Code != http.StatusServiceUnavailable {
t.Errorf("db down = %d", rec.Code)
}
}
func TestCORS(t *testing.T) {
h := newTestRouter(fakeDB{})
rec := do(h, "OPTIONS", "/api/v1/version", map[string]string{"Origin": "https://app.docwell.test"})
if rec.Code != http.StatusNoContent || rec.Header().Get("Access-Control-Allow-Origin") != "https://app.docwell.test" {
t.Errorf("preflight = %d %v", rec.Code, rec.Header())
}
rec = do(h, "GET", "/api/v1/version", map[string]string{"Origin": "https://evil.test"})
if rec.Header().Get("Access-Control-Allow-Origin") != "" {
t.Error("unknown origin must not be allowed")
}
}
func TestAppOrigin(t *testing.T) {
for _, o := range []string{"null", "https://app.docwell.docwell_mobile", "wails://wails.localhost", "http://wails.localhost:34115"} {
if !appOrigin(o) {
t.Errorf("%q refused", o)
}
}
for _, o := range []string{"", "https://evil.example", "http://wails.localhost.evil.example", "https://app.docwell.docwell_mobile.evil.example"} {
if appOrigin(o) {
t.Errorf("%q accepted", o)
}
}
}