MCPcopy
hub / github.com/caddyserver/caddy / fileMatcherTest

Function fileMatcherTest

modules/caddyhttp/fileserver/matcher_test.go:151–190  ·  view source on GitHub ↗
(t *testing.T, i int, tc testCase)

Source from the content-addressed store, hash-verified

149}
150
151func fileMatcherTest(t *testing.T, i int, tc testCase) {
152 m := &MatchFile{
153 fsmap: &filesystems.FileSystemMap{},
154 Root: "./testdata",
155 TryFiles: []string{"{http.request.uri.path}", "{http.request.uri.path}/"},
156 }
157
158 u, err := url.Parse(tc.path)
159 if err != nil {
160 t.Errorf("Test %d: parsing path: %v", i, err)
161 }
162
163 req := &http.Request{URL: u}
164 repl := caddyhttp.NewTestReplacer(req)
165
166 result, err := m.MatchWithError(req)
167 if err != nil {
168 t.Errorf("Test %d: unexpected error: %v", i, err)
169 }
170 if result != tc.matched {
171 t.Errorf("Test %d: expected match=%t, got %t", i, tc.matched, result)
172 }
173
174 rel, ok := repl.Get("http.matchers.file.relative")
175 if !ok && result {
176 t.Errorf("Test %d: expected replacer value", i)
177 }
178 if !result {
179 return
180 }
181
182 if rel != tc.expectedPath {
183 t.Errorf("Test %d: actual path: %v, expected: %v", i, rel, tc.expectedPath)
184 }
185
186 fileType, _ := repl.Get("http.matchers.file.type")
187 if fileType != tc.expectedType {
188 t.Errorf("Test %d: actual file type: %v, expected: %v", i, fileType, tc.expectedType)
189 }
190}
191
192func TestTryFilesRewriteEscapesMatchedPath(t *testing.T) {
193 root := t.TempDir()

Callers 2

TestFileMatcherFunction · 0.85

Calls 3

MatchWithErrorMethod · 0.95
NewTestReplacerFunction · 0.92
GetMethod · 0.65

Tested by

no test coverage detected