Skip to content

Commit cb436f0

Browse files
committed
fileserver: Fix tests on Windows
1 parent a108119 commit cb436f0

File tree

2 files changed

+69
-44
lines changed

2 files changed

+69
-44
lines changed

modules/caddyhttp/fileserver/matcher_test.go

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ import (
2020
"net/http/httptest"
2121
"net/url"
2222
"os"
23+
"path/filepath"
2324
"runtime"
25+
"strings"
2426
"testing"
2527

2628
"github.com/caddyserver/caddy/v2"
2729
"github.com/caddyserver/caddy/v2/internal/filesystems"
2830
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
2931
)
3032

33+
type testCase struct {
34+
path string
35+
expectedPath string
36+
expectedType string
37+
matched bool
38+
}
39+
3140
func TestFileMatcher(t *testing.T) {
3241
// Windows doesn't like colons in files names
3342
isWindows := runtime.GOOS == "windows"
@@ -45,12 +54,7 @@ func TestFileMatcher(t *testing.T) {
4554
f.Close()
4655
}
4756

48-
for i, tc := range []struct {
49-
path string
50-
expectedPath string
51-
expectedType string
52-
matched bool
53-
}{
57+
for i, tc := range []testCase{
5458
{
5559
path: "/foo.txt",
5660
expectedPath: "/foo.txt",
@@ -115,51 +119,72 @@ func TestFileMatcher(t *testing.T) {
115119
expectedType: "file",
116120
matched: !isWindows,
117121
},
118-
{
119-
path: "/foodir/secr%5Cet.txt",
120-
expectedPath: "/foodir/secr\\et.txt",
121-
expectedType: "file",
122-
matched: true,
123-
},
124122
} {
125-
m := &MatchFile{
126-
fsmap: &filesystems.FileSystemMap{},
127-
Root: "./testdata",
128-
TryFiles: []string{"{http.request.uri.path}", "{http.request.uri.path}/"},
129-
}
123+
fileMatcherTest(t, i, tc)
124+
}
125+
}
130126

131-
u, err := url.Parse(tc.path)
132-
if err != nil {
133-
t.Errorf("Test %d: parsing path: %v", i, err)
134-
}
127+
func TestFileMatcherNonWindows(t *testing.T) {
128+
if runtime.GOOS == "windows" {
129+
return
130+
}
135131

136-
req := &http.Request{URL: u}
137-
repl := caddyhttp.NewTestReplacer(req)
132+
// this is impossible to test on Windows, but tests a security patch for other platforms
133+
tc := testCase{
134+
path: "/foodir/secr%5Cet.txt",
135+
expectedPath: "/foodir/secr\\et.txt",
136+
expectedType: "file",
137+
matched: true,
138+
}
138139

139-
result, err := m.MatchWithError(req)
140-
if err != nil {
141-
t.Errorf("Test %d: unexpected error: %v", i, err)
142-
}
143-
if result != tc.matched {
144-
t.Errorf("Test %d: expected match=%t, got %t", i, tc.matched, result)
145-
}
140+
f, err := os.Create(filepath.Join("testdata", strings.TrimPrefix(tc.expectedPath, "/")))
141+
if err != nil {
142+
t.Fatalf("could not create test file: %v", err)
143+
}
144+
defer f.Close()
145+
defer os.Remove(f.Name())
146146

147-
rel, ok := repl.Get("http.matchers.file.relative")
148-
if !ok && result {
149-
t.Errorf("Test %d: expected replacer value", i)
150-
}
151-
if !result {
152-
continue
153-
}
147+
fileMatcherTest(t, 0, tc)
148+
}
154149

155-
if rel != tc.expectedPath {
156-
t.Errorf("Test %d: actual path: %v, expected: %v", i, rel, tc.expectedPath)
157-
}
150+
func fileMatcherTest(t *testing.T, i int, tc testCase) {
151+
m := &MatchFile{
152+
fsmap: &filesystems.FileSystemMap{},
153+
Root: "./testdata",
154+
TryFiles: []string{"{http.request.uri.path}", "{http.request.uri.path}/"},
155+
}
158156

159-
fileType, _ := repl.Get("http.matchers.file.type")
160-
if fileType != tc.expectedType {
161-
t.Errorf("Test %d: actual file type: %v, expected: %v", i, fileType, tc.expectedType)
162-
}
157+
u, err := url.Parse(tc.path)
158+
if err != nil {
159+
t.Errorf("Test %d: parsing path: %v", i, err)
160+
}
161+
162+
req := &http.Request{URL: u}
163+
repl := caddyhttp.NewTestReplacer(req)
164+
165+
result, err := m.MatchWithError(req)
166+
if err != nil {
167+
t.Errorf("Test %d: unexpected error: %v", i, err)
168+
}
169+
if result != tc.matched {
170+
t.Errorf("Test %d: expected match=%t, got %t", i, tc.matched, result)
171+
}
172+
173+
rel, ok := repl.Get("http.matchers.file.relative")
174+
if !ok && result {
175+
t.Errorf("Test %d: expected replacer value", i)
176+
}
177+
if !result {
178+
return
179+
}
180+
181+
if rel != tc.expectedPath {
182+
t.Errorf("Test %d: actual path: %v, expected: %v", i, rel, tc.expectedPath)
183+
}
184+
185+
fileType, _ := repl.Get("http.matchers.file.type")
186+
if fileType != tc.expectedType {
187+
t.Errorf("Test %d: actual file type: %v, expected: %v", i, fileType, tc.expectedType)
163188
}
164189
}
165190

modules/caddyhttp/fileserver/testdata/foodir/secr\et.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)