Files
flow/core/cmd/flowweb/webui_test.go
T
zeekay 5421a384b5 Fold the v2 API into /v1 and 404 /api
The upstream v2 routers mount under /v1 without collisions. flowweb
answers /api with 404 instead of the SPA shell, and a CI test fails
on any first-party /api/ or /v2/ path in src/.

Claude-Session: https://claude.ai/code/session_011i2AG9nwpiKUCVxAdDzYuU
2026-09-29 18:34:03 -07:00

61 lines
1.9 KiB
Go

package main
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHandlerRoutes(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{"path":"`+r.URL.Path+`"}`)
}))
defer backend.Close()
h, err := newHandler(backend.URL)
if err != nil {
t.Fatal(err)
}
cases := []struct {
method, path string
backend bool
status int
}{
{http.MethodGet, "/v1/version", true, http.StatusOK},
{http.MethodPost, "/v1/login", true, http.StatusOK},
{http.MethodGet, "/v1", true, http.StatusOK},
{http.MethodGet, "/v1/files", true, http.StatusOK},
{http.MethodGet, "/api/v1/version", false, http.StatusNotFound},
{http.MethodPost, "/api/v1/login", false, http.StatusNotFound},
{http.MethodGet, "/api", false, http.StatusNotFound},
{http.MethodGet, "/v2/files", false, http.StatusOK},
{http.MethodGet, "/apis", false, http.StatusOK},
{http.MethodGet, "/health_check", true, http.StatusOK},
{http.MethodGet, "/logs-stream", true, http.StatusOK},
{http.MethodGet, "/", false, http.StatusOK},
{http.MethodGet, "/flows", false, http.StatusOK},
{http.MethodGet, "/v10", false, http.StatusOK},
{http.MethodPost, "/flows", false, http.StatusMethodNotAllowed},
}
for _, c := range cases {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(c.method, c.path, nil))
if rec.Code != c.status {
t.Errorf("%s %s: status %d, want %d", c.method, c.path, rec.Code, c.status)
continue
}
body := rec.Body.String()
proxied := strings.Contains(body, `{"path":"`+c.path+`"}`)
if proxied != c.backend {
t.Errorf("%s %s: proxied=%v, want %v (body %.60q)", c.method, c.path, proxied, c.backend, body)
}
if !c.backend && c.status == http.StatusOK && !strings.Contains(body, "<html") {
t.Errorf("%s %s: want the SPA shell, got %.60q", c.method, c.path, body)
}
}
}