test(06-14): regression coverage for surf and bouncer gap closure

This commit is contained in:
Jakub Zych
2026-09-21 19:47:16 +02:00
parent 1d2e00cf59
commit f1218f2c84
5 changed files with 292 additions and 0 deletions

View File

@@ -480,3 +480,55 @@ func TestDuplicateMiddlewareFactoryNamesPluginAndName(t *testing.T) {
t.Fatalf("want plugin and factory name in error, got %v", err)
}
}
func TestBuildRouterFailsOnMissingBodyConfig(t *testing.T) {
bad := map[string]string{
"missing default": "body_limits:\n upload_bytes: 8\n",
"missing upload": "body_limits:\n default_bytes: 8\n",
"zero": "body_limits:\n default_bytes: 0\n upload_bytes: 8\n",
"negative": "body_limits:\n default_bytes: 4\n upload_bytes: -1\n",
"non-numeric": "body_limits:\n default_bytes: lots\n upload_bytes: 8\n",
}
for name, y := range bad {
t.Run(name, func(t *testing.T) {
if _, err := BuildRouter(backpack.New(writeHTTPConfig(t, y)), nil); err == nil {
t.Fatal("expected error")
}
})
}
if _, err := BuildRouter(backpack.New(writeHTTPConfig(t, "body_limits:\n default_bytes: 4\n upload_bytes: 8\n")), nil); err != nil {
t.Fatal(err)
}
}
type factoryCountPlugin struct{ calls *map[string]int }
func (p factoryCountPlugin) ID() string { return "golem15.count" }
func (p factoryCountPlugin) Requires() []string { return nil }
func (p factoryCountPlugin) Register(*backpack.App) error { return nil }
func (p factoryCountPlugin) Boot(*backpack.App) error { return nil }
func (p factoryCountPlugin) MiddlewareFactories() map[string]func(string) pact.Middleware {
return map[string]func(string) pact.Middleware{"count": func(param string) pact.Middleware {
(*p.calls)[param]++
return func(next http.Handler) http.Handler { return next }
}}
}
func (p factoryCountPlugin) Routes(r pact.Router) error {
r.Group("/", Use("count:a"), func(g pact.Router) {
g.Get("/one", func(http.ResponseWriter, *http.Request) {})
g.Get("/two", func(http.ResponseWriter, *http.Request) {})
g.Get("/three", func(http.ResponseWriter, *http.Request) {}, "count:b")
})
return nil
}
func TestFactoriesBuiltOncePerName(t *testing.T) {
calls := map[string]int{}
cfg := writeHTTPConfig(t, "body_limits:\n default_bytes: 4\n upload_bytes: 8\n")
if _, err := Assemble(backpack.New(cfg), []party.Plugin{factoryCountPlugin{calls: &calls}}); err != nil {
t.Fatal(err)
}
if calls["a"] != 1 || calls["b"] != 1 {
t.Fatalf("factory invocations = %v, want each name:param built once", calls)
}
}