httpcaddyfile: add Helper.IsNamedRoute (#8354)
parent
b5d67d6540
commit
cf1b1121e7
|
|
@ -228,6 +228,14 @@ func (h Helper) Option(name string) any {
|
|||
return h.options[name]
|
||||
}
|
||||
|
||||
// IsNamedRoute returns true if the directive is in a named route.
|
||||
// Named routes are adapted before the site blocks, with their own
|
||||
// State, so values a directive keeps in State aren't shared with
|
||||
// the site blocks.
|
||||
func (h Helper) IsNamedRoute() bool {
|
||||
return h.parentBlock.IsNamedRoute
|
||||
}
|
||||
|
||||
// Caddyfiles returns the list of config files from
|
||||
// which tokens in the current server block were loaded.
|
||||
func (h Helper) Caddyfiles() []string {
|
||||
|
|
|
|||
|
|
@ -111,6 +111,36 @@ func init() {
|
|||
return caddyhttp.StaticError{StatusCode: "500"}, nil
|
||||
})
|
||||
RegisterDirectiveOrder("test_ordered_plugin", Before, "respond")
|
||||
|
||||
RegisterHandlerDirective("test_named_route", func(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
h.Next()
|
||||
body := "site"
|
||||
if h.IsNamedRoute() {
|
||||
body = "named"
|
||||
}
|
||||
return caddyhttp.StaticResponse{Body: body}, nil
|
||||
})
|
||||
RegisterDirectiveOrder("test_named_route", Before, "respond")
|
||||
}
|
||||
|
||||
func TestHelperIsNamedRoute(t *testing.T) {
|
||||
adapted := adaptCaddyfile(t, `&(shared) {
|
||||
handle /nested {
|
||||
test_named_route
|
||||
}
|
||||
}
|
||||
|
||||
:8080 {
|
||||
test_named_route
|
||||
invoke shared
|
||||
}
|
||||
`)
|
||||
|
||||
for _, body := range []string{`"body":"named"`, `"body":"site"`} {
|
||||
if !strings.Contains(adapted, body) {
|
||||
t.Errorf("expected %s in the adapted config: %s", body, adapted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// caddyfiles shared by the directive order tests below. the handle block in
|
||||
|
|
|
|||
Loading…
Reference in New Issue