caddyhttp: replace placeholders in map defaults (#5081)

This updates the map directive to replace placeholders in default values
in the same way as matched values.
This commit is contained in:
Will Norris 2022-09-28 12:38:20 -07:00 committed by GitHub
parent e3e8aabbcf
commit 61822f129b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View file

@ -169,7 +169,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
// fall back to default if no match or if matched nil value
if len(h.Defaults) > destIdx {
return h.Defaults[destIdx], true
return repl.ReplaceAll(h.Defaults[destIdx], ""), true
}
return nil, true

View file

@ -98,6 +98,28 @@ func TestHandler(t *testing.T) {
"output": "testing",
},
},
{
reqURI: "/foo",
handler: Handler{
Source: "{http.request.uri.path}",
Destinations: []string{"{output}"},
Defaults: []string{"default"},
},
expect: map[string]any{
"output": "default",
},
},
{
reqURI: "/foo",
handler: Handler{
Source: "{http.request.uri.path}",
Destinations: []string{"{output}"},
Defaults: []string{"{testvar}"},
},
expect: map[string]any{
"output": "testing",
},
},
} {
if err := tc.handler.Provision(caddy.Context{}); err != nil {
t.Fatalf("Test %d: Provisioning handler: %v", i, err)