summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/fileserver/matcher_test.go
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2020-09-16 20:09:28 -0400
committerGitHub <noreply@github.com>2020-09-16 18:09:28 -0600
commitb95b87381a282e1fe57295d145b71645d7801f07 (patch)
tree853f7368ac81f6ede2de30503201cf943841b34e /modules/caddyhttp/fileserver/matcher_test.go
parentb01bb275b395643542ceca4fbc82bedea8e43937 (diff)
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
Diffstat (limited to 'modules/caddyhttp/fileserver/matcher_test.go')
-rw-r--r--modules/caddyhttp/fileserver/matcher_test.go93
1 files changed, 93 insertions, 0 deletions
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)
+ }
}
}