1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

fileserver: fix try_policy when instantiating file matcher from CEL (#6624)

Co-authored-by: Francis Lavoie <lavofr@gmail.com>
This commit is contained in:
Logan Fleur 2024-10-20 16:08:30 +02:00 committed by GitHub
parent b443190b66
commit 9753c44510
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View file

@ -191,7 +191,7 @@ func (MatchFile) CELLibrary(ctx caddy.Context) (cel.Library, error) {
var try_policy string var try_policy string
if len(values["try_policy"]) > 0 { if len(values["try_policy"]) > 0 {
root = values["try_policy"][0] try_policy = values["try_policy"][0]
} }
m := MatchFile{ m := MatchFile{

View file

@ -289,6 +289,7 @@ var expressionTests = []struct {
wantErr bool wantErr bool
wantResult bool wantResult bool
clientCertificate []byte clientCertificate []byte
expectedPath string
}{ }{
{ {
name: "file error no args (MatchFile)", name: "file error no args (MatchFile)",
@ -354,6 +355,15 @@ var expressionTests = []struct {
urlTarget: "https://example.com/nopenope.txt", urlTarget: "https://example.com/nopenope.txt",
wantResult: false, wantResult: false,
}, },
{
name: "file match long pattern foo.txt with try_policy (MatchFile)",
expression: &caddyhttp.MatchExpression{
Expr: `file({"root": "./testdata", "try_policy": "largest_size", "try_files": ["foo.txt", "large.txt"]})`,
},
urlTarget: "https://example.com/",
wantResult: true,
expectedPath: "/large.txt",
},
} }
func TestMatchExpressionMatch(t *testing.T) { func TestMatchExpressionMatch(t *testing.T) {
@ -382,6 +392,16 @@ func TestMatchExpressionMatch(t *testing.T) {
if tc.expression.Match(req) != tc.wantResult { if tc.expression.Match(req) != tc.wantResult {
t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tc.wantResult, tc.expression.Expr) t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tc.wantResult, tc.expression.Expr)
} }
if tc.expectedPath != "" {
path, ok := repl.Get("http.matchers.file.relative")
if !ok {
t.Errorf("MatchExpression.Match() expected to return path '%s', but got none", tc.expectedPath)
}
if path != tc.expectedPath {
t.Errorf("MatchExpression.Match() expected to return path '%s', but got '%s'", tc.expectedPath, path)
}
}
}) })
} }
} }

View file

@ -0,0 +1,3 @@
This is a file with more content than the other files in this directory
such that tests using the largest_size policy pick this file, or the
smallest_size policy avoids this file.