Skip to content

Commit 9753c44

Browse files
fileserver: fix try_policy when instantiating file matcher from CEL (#6624)
Co-authored-by: Francis Lavoie <[email protected]>
1 parent b443190 commit 9753c44

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

modules/caddyhttp/fileserver/matcher.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (MatchFile) CELLibrary(ctx caddy.Context) (cel.Library, error) {
191191

192192
var try_policy string
193193
if len(values["try_policy"]) > 0 {
194-
root = values["try_policy"][0]
194+
try_policy = values["try_policy"][0]
195195
}
196196

197197
m := MatchFile{

modules/caddyhttp/fileserver/matcher_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ var expressionTests = []struct {
289289
wantErr bool
290290
wantResult bool
291291
clientCertificate []byte
292+
expectedPath string
292293
}{
293294
{
294295
name: "file error no args (MatchFile)",
@@ -354,6 +355,15 @@ var expressionTests = []struct {
354355
urlTarget: "https://example.com/nopenope.txt",
355356
wantResult: false,
356357
},
358+
{
359+
name: "file match long pattern foo.txt with try_policy (MatchFile)",
360+
expression: &caddyhttp.MatchExpression{
361+
Expr: `file({"root": "./testdata", "try_policy": "largest_size", "try_files": ["foo.txt", "large.txt"]})`,
362+
},
363+
urlTarget: "https://example.com/",
364+
wantResult: true,
365+
expectedPath: "/large.txt",
366+
},
357367
}
358368

359369
func TestMatchExpressionMatch(t *testing.T) {
@@ -382,6 +392,16 @@ func TestMatchExpressionMatch(t *testing.T) {
382392
if tc.expression.Match(req) != tc.wantResult {
383393
t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tc.wantResult, tc.expression.Expr)
384394
}
395+
396+
if tc.expectedPath != "" {
397+
path, ok := repl.Get("http.matchers.file.relative")
398+
if !ok {
399+
t.Errorf("MatchExpression.Match() expected to return path '%s', but got none", tc.expectedPath)
400+
}
401+
if path != tc.expectedPath {
402+
t.Errorf("MatchExpression.Match() expected to return path '%s', but got '%s'", tc.expectedPath, path)
403+
}
404+
}
385405
})
386406
}
387407
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a file with more content than the other files in this directory
2+
such that tests using the largest_size policy pick this file, or the
3+
smallest_size policy avoids this file.

0 commit comments

Comments
 (0)