diff --git a/cmd/server/tests/device_pairing_test.go b/cmd/server/tests/device_pairing_test.go index bfa1afb3..384af391 100644 --- a/cmd/server/tests/device_pairing_test.go +++ b/cmd/server/tests/device_pairing_test.go @@ -19,6 +19,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "strings" "testing" "github.com/google/uuid" @@ -91,6 +92,55 @@ func wrongPairingCode(code string) string { return string(rotated) } +// pairingFormRequest POSTs urlencoded form data — the Content-Type the +// browser's htmx forms actually send — with a Bearer token. +func pairingFormRequest(t *testing.T, setup *TestServerSetup, path, token, body string) (int, map[string]interface{}) { + t.Helper() + req := httptest.NewRequest("POST", path, strings.NewReader(body)) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) + } + rec := httptest.NewRecorder() + setup.Server.Config.Handler.ServeHTTP(rec, req) + var resp map[string]interface{} + json.Unmarshal(rec.Body.Bytes(), &resp) + return rec.Code, resp +} + +// TestPairingClaimAcceptsBrowserFormEncoding pins the htmx path: the +// confirm card claims via a urlencoded