Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 10:30 UTC
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.
Will Norris committed
Sep 28, 2022 at 12:38 UTC
61822f129b9edbbc1ed2fd068d8e5ba919b5881d
2 files changed
+23
-1
modules/caddyhttp/map/map.go
+1
-1
@@ -169,7 +169,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
169
170
// fall back to default if no match or if matched nil value
171
if len(h.Defaults) > destIdx {
172
- return h.Defaults[destIdx], true
172
+ return repl.ReplaceAll(h.Defaults[destIdx], ""), true
173
}
174
175
return nil, true
modules/caddyhttp/map/map_test.go
+22
@@ -98,6 +98,28 @@ func TestHandler(t *testing.T) {
98
"output": "testing",
99
},
100
},
101
+ {
102
+ reqURI: "/foo",
103
+ handler: Handler{
104
+ Source: "{http.request.uri.path}",
105
+ Destinations: []string{"{output}"},
106
+ Defaults: []string{"default"},
107
+ },
108
+ expect: map[string]any{
109
+ "output": "default",
110
+ },
111
+ },
112
+ {
113
+ reqURI: "/foo",
114
+ handler: Handler{
115
+ Source: "{http.request.uri.path}",
116
+ Destinations: []string{"{output}"},
117
+ Defaults: []string{"{testvar}"},
118
+ },
119
+ expect: map[string]any{
120
+ "output": "testing",
121
+ },
122
+ },
123
} {
124
if err := tc.handler.Provision(caddy.Context{}); err != nil {
125
t.Fatalf("Test %d: Provisioning handler: %v", i, err)