Debian Codegraph @

modules/caddyhttp/fileserver/staticfiles_test.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package fileserver
16
17 import (
18 "context"
19 "net/http"
20 "net/http/httptest"
21 "os"
22 "path/filepath"
23 "runtime"
24 "strings"
25 "testing"
26 "time"
27
28 "github.com/caddyserver/caddy/v2"
29 )
30
31 func TestFileHidden(t *testing.T) {
32 for i, tc := range []struct {
33 inputHide []string
34 inputPath string
35 expect bool
36 }{
37 {
38 inputHide: nil,
39 inputPath: "",
40 expect: false,
41 },
42 {
43 inputHide: []string{".gitignore"},
44 inputPath: "/.gitignore",
45 expect: true,
46 },
47 {
48 inputHide: []string{".git"},
49 inputPath: "/.gitignore",
50 expect: false,
51 },
52 {
53 inputHide: []string{"/.git"},
54 inputPath: "/.gitignore",
55 expect: false,
56 },
57 {
58 inputHide: []string{".git"},
59 inputPath: "/.git",
60 expect: true,
61 },
62 {
63 inputHide: []string{".git"},
64 inputPath: "/.git/foo",
65 expect: true,
66 },
67 {
68 inputHide: []string{".git"},
69 inputPath: "/foo/.git/bar",
70 expect: true,
71 },
72 {
73 inputHide: []string{"/prefix"},
74 inputPath: "/prefix/foo",
75 expect: true,
76 },
77 {
78 inputHide: []string{"/foo/*/bar"},
79 inputPath: "/foo/asdf/bar",
80 expect: true,
81 },
82 {
83 inputHide: []string{"*.txt"},
84 inputPath: "/foo/bar.txt",
85 expect: true,
86 },
87 {
88 inputHide: []string{"/foo/bar/*.txt"},
89 inputPath: "/foo/bar/baz.txt",
90 expect: true,
91 },
92 {
93 inputHide: []string{"/foo/bar/*.txt"},
94 inputPath: "/foo/bar.txt",
95 expect: false,
96 },
97 {
98 inputHide: []string{"/foo/bar/*.txt"},
99 inputPath: "/foo/bar/index.html",
100 expect: false,
101 },
102 {
103 inputHide: []string{"/foo"},
104 inputPath: "/foo",
105 expect: true,
106 },
107 {
108 inputHide: []string{"/foo"},
109 inputPath: "/foobar",
110 expect: false,
111 },
112 {
113 inputHide: []string{"first", "second"},
114 inputPath: "/second",
115 expect: true,
116 },
117 } {
118 if runtime.GOOS == "windows" {
119 if strings.HasPrefix(tc.inputPath, "/") {
120 tc.inputPath, _ = filepath.Abs(tc.inputPath)
121 }
122 tc.inputPath = filepath.FromSlash(tc.inputPath)
123 for i := range tc.inputHide {
124 if strings.HasPrefix(tc.inputHide[i], "/") {
125 tc.inputHide[i], _ = filepath.Abs(tc.inputHide[i])
126 }
127 tc.inputHide[i] = filepath.FromSlash(tc.inputHide[i])
128 }
129 }
130
131 actual := fileHidden(tc.inputPath, tc.inputHide)
132 if actual != tc.expect {
133 t.Errorf("Test %d: Does %v hide %s? Got %t but expected %t",
134 i, tc.inputHide, tc.inputPath, actual, tc.expect)
135 }
136 }
137 }
138
139 // Check to make sure that we don't serve ETag and Last-Modified headers
140 // for files with invalid modification times
141 func TestModTimeHeaders(t *testing.T) {
142 check_validator_headers(time.Now(), true, t)
143 check_validator_headers(time.Unix(0, 0), false, t)
144 check_validator_headers(time.Unix(1, 0), false, t)
145 check_validator_headers(time.Unix(2, 0), true, t)
146 }
147
148 func check_validator_headers(modTime time.Time, expect_headers bool, t *testing.T) {
149 f := false
150 fsrv := FileServer{
151 Root: "./testdata",
152 CanonicalURIs: &f,
153 }
154 w := httptest.NewRecorder()
155 r, err := http.NewRequest("GET", "/modtime.txt", nil)
156 if err != nil {
157 t.Fatal(err)
158 }
159 repl := caddy.NewReplacer()
160 ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
161 r = r.WithContext(ctx)
162
163 ctx2, _ := caddy.NewContext(caddy.Context{Context: context.Background()}) // module will be nil by default
164 fsrv.Provision(ctx2)
165
166 path := "testdata/modtime.txt"
167 os.Chtimes(path, modTime, modTime)
168
169 fsrv.ServeHTTP(w, r, nil)
170
171 if expect_headers {
172 if w.Header().Get("ETag") == "" {
173 t.Errorf("Didn't get ETag header for file with valid mod time %s", modTime)
174 }
175 if w.Header().Get("Last-Modified") == "" {
176 t.Errorf("Didn't get Last-Modified header for file with valid mod time %s", modTime)
177 }
178 } else {
179 if w.Header().Get("ETag") != "" {
180 t.Errorf("Got ETag header for file with invalid mod time %s", modTime)
181 }
182 if w.Header().Get("Last-Modified") != "" {
183 t.Errorf("Got Last-Modified header for file with invalid mod time %s", modTime)
184 }
185 }
186 }