aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLogan Fleur <[email protected]>2024-10-20 16:08:30 +0200
committerGitHub <[email protected]>2024-10-20 14:08:30 +0000
commit9753c4451077d4459ec10cb3df27ab9dc4456290 (patch)
tree468559a49a47898a8661b6afb58751c4f559bb98
parentb443190b66055089d1e112abc992512ca4f6bfba (diff)
downloadcaddy-9753c4451077d4459ec10cb3df27ab9dc4456290.tar.gz
caddy-9753c4451077d4459ec10cb3df27ab9dc4456290.zip
fileserver: fix try_policy when instantiating file matcher from CEL (#6624)
Co-authored-by: Francis Lavoie <[email protected]>
-rw-r--r--modules/caddyhttp/fileserver/matcher.go2
-rw-r--r--modules/caddyhttp/fileserver/matcher_test.go20
-rw-r--r--modules/caddyhttp/fileserver/testdata/large.txt3
3 files changed, 24 insertions, 1 deletions
diff --git a/modules/caddyhttp/fileserver/matcher.go b/modules/caddyhttp/fileserver/matcher.go
index 71de1db29..28f7b89be 100644
--- a/modules/caddyhttp/fileserver/matcher.go
+++ b/modules/caddyhttp/fileserver/matcher.go
@@ -191,7 +191,7 @@ func (MatchFile) CELLibrary(ctx caddy.Context) (cel.Library, error) {
var try_policy string
if len(values["try_policy"]) > 0 {
- root = values["try_policy"][0]
+ try_policy = values["try_policy"][0]
}
m := MatchFile{
diff --git a/modules/caddyhttp/fileserver/matcher_test.go b/modules/caddyhttp/fileserver/matcher_test.go
index 527c16bd1..95eeb8216 100644
--- a/modules/caddyhttp/fileserver/matcher_test.go
+++ b/modules/caddyhttp/fileserver/matcher_test.go
@@ -289,6 +289,7 @@ var expressionTests = []struct {
wantErr bool
wantResult bool
clientCertificate []byte
+ expectedPath string
}{
{
name: "file error no args (MatchFile)",
@@ -354,6 +355,15 @@ var expressionTests = []struct {
urlTarget: "https://example.com/nopenope.txt",
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) {
@@ -382,6 +392,16 @@ func TestMatchExpressionMatch(t *testing.T) {
if tc.expression.Match(req) != tc.wantResult {
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)
+ }
+ }
})
}
}
diff --git a/modules/caddyhttp/fileserver/testdata/large.txt b/modules/caddyhttp/fileserver/testdata/large.txt
new file mode 100644
index 000000000..c36623744
--- /dev/null
+++ b/modules/caddyhttp/fileserver/testdata/large.txt
@@ -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. \ No newline at end of file