feat(06-03): add raw groups, house middleware, and route:list
- GroupRaw plus sticky raw inheritance and registration-time house-envelope refusal via pact.HasHouseMiddleware - Recover on raw routes writes a bare 500; non-raw keeps the house JSON body - Router.Routes() and surf.RouteListCommand; generated main registers route:list
This commit is contained in:
@@ -7,7 +7,9 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/backpack"
|
||||
"git.golem15.com/golem15/summercms/pact"
|
||||
"git.golem15.com/golem15/summercms/party"
|
||||
)
|
||||
|
||||
type routePlugin struct {
|
||||
@@ -46,6 +48,72 @@ func TestMissingMiddlewareNamesPluginAndName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRawGroupPanicBare500(t *testing.T) {
|
||||
r := New(nil)
|
||||
r.GroupRaw("/oauth", nil, func(g pact.Router) {
|
||||
g.Get("/panic", func(http.ResponseWriter, *http.Request) {
|
||||
panic("secret internals")
|
||||
})
|
||||
})
|
||||
r.Get("/panic", func(http.ResponseWriter, *http.Request) {
|
||||
panic("secret internals")
|
||||
})
|
||||
h, err := r.compile()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/oauth/panic", nil))
|
||||
if rec.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("raw status = %d", rec.Code)
|
||||
}
|
||||
if rec.Body.Len() != 0 {
|
||||
t.Fatalf("raw body = %q", rec.Body.String())
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "" {
|
||||
t.Fatalf("raw Content-Type = %q", ct)
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/panic", nil))
|
||||
if rec.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("house status = %d", rec.Code)
|
||||
}
|
||||
if rec.Header().Get("Content-Type") != "application/json" {
|
||||
t.Fatalf("house Content-Type = %q", rec.Header().Get("Content-Type"))
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload["error"] != true || payload["message"] != "Internal server error" {
|
||||
t.Fatalf("payload = %v", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHouseMiddlewareDuplicateNameFailsBoot(t *testing.T) {
|
||||
identity := func(next http.Handler) http.Handler { return next }
|
||||
named := assemblePlugin{
|
||||
id: "golem15.one",
|
||||
mw: map[string]pact.Middleware{"shared.mw": identity},
|
||||
use: []string{},
|
||||
}
|
||||
house := houseRoutePlugin{
|
||||
id: "golem15.two",
|
||||
house: map[string]pact.Middleware{
|
||||
"shared.mw": identity,
|
||||
},
|
||||
}
|
||||
_, err := BuildRouter(backpack.New(nil), []party.Plugin{named, house})
|
||||
if err == nil {
|
||||
t.Fatal("want duplicate-name error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "shared.mw") || !strings.Contains(err.Error(), "golem15.one") {
|
||||
t.Fatalf("want existing duplicate-name error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverReturnsOpaqueJSON500(t *testing.T) {
|
||||
r := New(nil)
|
||||
r.Get("/panic", func(http.ResponseWriter, *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user