Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 07:00 UTC
fileserver: fix try_policy when instantiating file matcher from CEL (#6624)
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
Logan Fleur committed
Oct 20, 2024 at 16:08 UTC
9753c4451077d4459ec10cb3df27ab9dc4456290
3 files changed
+24
-1
modules/caddyhttp/fileserver/matcher.go
+1
-1
@@ -191,7 +191,7 @@ func (MatchFile) CELLibrary(ctx caddy.Context) (cel.Library, error) {
191
192
var try_policy string
193
if len(values["try_policy"]) > 0 {
194
- root = values["try_policy"][0]
194
+ try_policy = values["try_policy"][0]
195
}
196
197
m := MatchFile{
modules/caddyhttp/fileserver/matcher_test.go
+20
@@ -289,6 +289,7 @@ var expressionTests = []struct {
289
wantErr bool
290
wantResult bool
291
clientCertificate []byte
292
+ expectedPath string
293
}{
294
{
295
name: "file error no args (MatchFile)",
@@ -354,6 +355,15 @@ var expressionTests = []struct {
355
urlTarget: "https://example.com/nopenope.txt",
356
wantResult: false,
357
},
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
+ },
367
}
368
369
func TestMatchExpressionMatch(t *testing.T) {
@@ -382,6 +392,16 @@ func TestMatchExpressionMatch(t *testing.T) {
392
if tc.expression.Match(req) != tc.wantResult {
393
t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tc.wantResult, tc.expression.Expr)
394
}
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
+ }
405
})
406
}
407
}
modules/caddyhttp/fileserver/testdata/large.txt
new
+3
@@ -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.
\ No newline at end of file