summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/fileserver/staticfiles_test.go
diff options
context:
space:
mode:
authorMatt Holt <mholt@users.noreply.github.com>2020-11-02 14:20:12 -0700
committerGitHub <noreply@github.com>2020-11-02 14:20:12 -0700
commit8d038ca515ffaa4d37dca05a95181203b2db64e7 (patch)
tree09ec23687cebfe468f8079ad86facf4d40a7c31c /modules/caddyhttp/fileserver/staticfiles_test.go
parent937ec342010894f7a6239883b10f6a107ff82c9f (diff)
fileserver: Improve and clarify file hiding logic (#3844)
* fileserver: Improve and clarify file hiding logic * Oops, forgot to run integration tests * Make this one integration test OS-agnostic * See if this appeases the Windows gods * D'oh
Diffstat (limited to 'modules/caddyhttp/fileserver/staticfiles_test.go')
-rw-r--r--modules/caddyhttp/fileserver/staticfiles_test.go50
1 files changed, 42 insertions, 8 deletions
diff --git a/modules/caddyhttp/fileserver/staticfiles_test.go b/modules/caddyhttp/fileserver/staticfiles_test.go
index 1bbd77b..690f46a 100644
--- a/modules/caddyhttp/fileserver/staticfiles_test.go
+++ b/modules/caddyhttp/fileserver/staticfiles_test.go
@@ -17,6 +17,8 @@ package fileserver
import (
"net/url"
"path/filepath"
+ "runtime"
+ "strings"
"testing"
)
@@ -44,7 +46,7 @@ func TestSanitizedPathJoin(t *testing.T) {
},
{
inputPath: "/foo/",
- expect: "foo" + string(filepath.Separator),
+ expect: "foo" + separator,
},
{
inputPath: "/foo/bar",
@@ -77,7 +79,7 @@ func TestSanitizedPathJoin(t *testing.T) {
{
inputRoot: "/a/b",
inputPath: "/%2e%2e%2f%2e%2e%2f",
- expect: filepath.Join("/", "a", "b") + string(filepath.Separator),
+ expect: filepath.Join("/", "a", "b") + separator,
},
{
inputRoot: "C:\\www",
@@ -155,6 +157,26 @@ func TestFileHidden(t *testing.T) {
expect: true,
},
{
+ inputHide: []string{"*.txt"},
+ inputPath: "/foo/bar.txt",
+ expect: true,
+ },
+ {
+ inputHide: []string{"/foo/bar/*.txt"},
+ inputPath: "/foo/bar/baz.txt",
+ expect: true,
+ },
+ {
+ inputHide: []string{"/foo/bar/*.txt"},
+ inputPath: "/foo/bar.txt",
+ expect: false,
+ },
+ {
+ inputHide: []string{"/foo/bar/*.txt"},
+ inputPath: "/foo/bar/index.html",
+ expect: false,
+ },
+ {
inputHide: []string{"/foo"},
inputPath: "/foo",
expect: true,
@@ -164,17 +186,29 @@ func TestFileHidden(t *testing.T) {
inputPath: "/foobar",
expect: false,
},
+ {
+ inputHide: []string{"first", "second"},
+ inputPath: "/second",
+ expect: true,
+ },
} {
- // for Windows' sake
- tc.inputPath = filepath.FromSlash(tc.inputPath)
- for i := range tc.inputHide {
- tc.inputHide[i] = filepath.FromSlash(tc.inputHide[i])
+ if runtime.GOOS == "windows" {
+ if strings.HasPrefix(tc.inputPath, "/") {
+ tc.inputPath, _ = filepath.Abs(tc.inputPath)
+ }
+ tc.inputPath = filepath.FromSlash(tc.inputPath)
+ for i := range tc.inputHide {
+ if strings.HasPrefix(tc.inputHide[i], "/") {
+ tc.inputHide[i], _ = filepath.Abs(tc.inputHide[i])
+ }
+ tc.inputHide[i] = filepath.FromSlash(tc.inputHide[i])
+ }
}
actual := fileHidden(tc.inputPath, tc.inputHide)
if actual != tc.expect {
- t.Errorf("Test %d: Is %s hidden in %v? Got %t but expected %t",
- i, tc.inputPath, tc.inputHide, actual, tc.expect)
+ t.Errorf("Test %d: Does %v hide %s? Got %t but expected %t",
+ i, tc.inputHide, tc.inputPath, actual, tc.expect)
}
}
}