From b95b87381a282e1fe57295d145b71645d7801f07 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Wed, 16 Sep 2020 20:09:28 -0400 Subject: fileserver: Fix try_files for directories; windows fix (#3684) * fileserver: Fix try_files for directories, windows fix * fileserver: Add new file type placeholder, refactoring, tests * fileserver: Review cleanup * fileserver: Flip the return args order --- modules/caddyhttp/fileserver/matcher_test.go | 93 ++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'modules/caddyhttp/fileserver/matcher_test.go') diff --git a/modules/caddyhttp/fileserver/matcher_test.go b/modules/caddyhttp/fileserver/matcher_test.go index 51cd284..ddd4f78 100644 --- a/modules/caddyhttp/fileserver/matcher_test.go +++ b/modules/caddyhttp/fileserver/matcher_test.go @@ -22,30 +22,114 @@ import ( "github.com/caddyserver/caddy/v2/modules/caddyhttp" ) +func TestFileMatcher(t *testing.T) { + for i, tc := range []struct { + path string + expectedPath string + expectedType string + matched bool + }{ + { + path: "/foo.txt", + expectedPath: "/foo.txt", + expectedType: "file", + matched: true, + }, + { + path: "/foo.txt/", + expectedPath: "/foo.txt", + expectedType: "file", + matched: true, + }, + { + path: "/foodir", + expectedPath: "/foodir/", + expectedType: "directory", + matched: true, + }, + { + path: "/foodir/", + expectedPath: "/foodir/", + expectedType: "directory", + matched: true, + }, + { + path: "/foodir/foo.txt", + expectedPath: "/foodir/foo.txt", + expectedType: "file", + matched: true, + }, + { + path: "/missingfile.php", + matched: false, + }, + } { + m := &MatchFile{ + Root: "./testdata", + TryFiles: []string{"{http.request.uri.path}", "{http.request.uri.path}/"}, + } + + u, err := url.Parse(tc.path) + if err != nil { + t.Fatalf("Test %d: parsing path: %v", i, err) + } + + req := &http.Request{URL: u} + repl := caddyhttp.NewTestReplacer(req) + + result := m.Match(req) + if result != tc.matched { + t.Fatalf("Test %d: expected match=%t, got %t", i, tc.matched, result) + } + + rel, ok := repl.Get("http.matchers.file.relative") + if !ok && result { + t.Fatalf("Test %d: expected replacer value", i) + } + if !result { + continue + } + + if rel != tc.expectedPath { + t.Fatalf("Test %d: actual path: %v, expected: %v", i, rel, tc.expectedPath) + } + + fileType, ok := repl.Get("http.matchers.file.type") + if fileType != tc.expectedType { + t.Fatalf("Test %d: actual file type: %v, expected: %v", i, fileType, tc.expectedType) + } + } +} + func TestPHPFileMatcher(t *testing.T) { for i, tc := range []struct { path string expectedPath string + expectedType string matched bool }{ { path: "/index.php", expectedPath: "/index.php", + expectedType: "file", matched: true, }, { path: "/index.php/somewhere", expectedPath: "/index.php", + expectedType: "file", matched: true, }, { path: "/remote.php", expectedPath: "/remote.php", + expectedType: "file", matched: true, }, { path: "/remote.php/somewhere", expectedPath: "/remote.php", + expectedType: "file", matched: true, }, { @@ -55,11 +139,13 @@ func TestPHPFileMatcher(t *testing.T) { { path: "/notphp.php.txt", expectedPath: "/notphp.php.txt", + expectedType: "file", matched: true, }, { path: "/notphp.php.txt/", expectedPath: "/notphp.php.txt", + expectedType: "file", matched: true, }, { @@ -69,12 +155,14 @@ func TestPHPFileMatcher(t *testing.T) { { path: "/foo.php.php/index.php", expectedPath: "/foo.php.php/index.php", + expectedType: "file", matched: true, }, { // See https://github.com/caddyserver/caddy/issues/3623 path: "/%E2%C3", expectedPath: "/%E2%C3", + expectedType: "file", matched: false, }, } { @@ -108,6 +196,11 @@ func TestPHPFileMatcher(t *testing.T) { if rel != tc.expectedPath { t.Fatalf("Test %d: actual path: %v, expected: %v", i, rel, tc.expectedPath) } + + fileType, ok := repl.Get("http.matchers.file.type") + if fileType != tc.expectedType { + t.Fatalf("Test %d: actual file type: %v, expected: %v", i, fileType, tc.expectedType) + } } } -- cgit v1.2.3