88
type testFileSystem struct {
89
open func(name string) (File, os.Error)
92
func (fs *testFileSystem) Open(name string) (File, os.Error) {
96
func TestFileServerCleans(t *testing.T) {
97
ch := make(chan string, 1)
98
fs := FileServer(&testFileSystem{func(name string) (File, os.Error) {
100
return nil, os.ENOENT
103
reqPath, openArg string
105
{"/foo.txt", "/foo.txt"},
106
{"//foo.txt", "/foo.txt"},
107
{"/../foo.txt", "/foo.txt"},
109
req, _ := NewRequest("GET", "http://example.com", nil)
110
for n, test := range tests {
111
rec := httptest.NewRecorder()
112
req.URL.Path = test.reqPath
113
fs.ServeHTTP(rec, req)
114
if got := <-ch; got != test.openArg {
115
t.Errorf("test %d: got %q, want %q", n, got, test.openArg)
120
func TestDirJoin(t *testing.T) {
121
wfi, err := os.Stat("/etc/hosts")
123
t.Logf("skipping test; no /etc/hosts file")
126
test := func(d Dir, name string) {
127
f, err := d.Open(name)
129
t.Fatalf("open of %s: %v", name, err)
134
t.Fatalf("stat of %s: %v", err)
136
if gfi.Ino != wfi.Ino {
137
t.Errorf("%s got different inode")
140
test(Dir("/etc/"), "/hosts")
141
test(Dir("/etc/"), "hosts")
142
test(Dir("/etc/"), "../../../../hosts")
143
test(Dir("/etc"), "/hosts")
144
test(Dir("/etc"), "hosts")
145
test(Dir("/etc"), "../../../../hosts")
147
// Not really directories, but since we use this trick in
148
// ServeFile, test it:
149
test(Dir("/etc/hosts"), "")
150
test(Dir("/etc/hosts"), "/")
151
test(Dir("/etc/hosts"), "../")
88
154
func TestServeFileContentType(t *testing.T) {
89
155
const ctype = "icecream/chocolate"