-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfs_test.go
52 lines (48 loc) · 1.45 KB
/
fs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package httpgzip_test
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/shurcooL/httpgzip"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/mapfs"
)
func ExampleFileServer() {
http.Handle("/assets/", http.StripPrefix("/assets", httpgzip.FileServer(
http.Dir("assets"),
httpgzip.FileServerOptions{
IndexHTML: true,
},
)))
}
// Test that there are no infinite redirects at root path even if the entire
// req.URL.Path is stripped, e.g., via an overly aggressive http.StripPrefix.
// See https://github.com/shurcooL/httpgzip/pull/3
// and https://github.com/golang/go/commit/3745716bc3940f471137bf06fbe8c042257a43d3.
func TestFileServerImplicitLeadingSlash(t *testing.T) {
fs := httpfs.New(mapfs.New(map[string]string{
"foo.txt": "Hello world",
}))
ts := httptest.NewServer(http.StripPrefix("/bar/", httpgzip.FileServer(fs, httpgzip.FileServerOptions{})))
defer ts.Close()
get := func(suffix string) string {
res, err := http.Get(ts.URL + suffix)
if err != nil {
t.Fatalf("Get %s: %v", suffix, err)
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("ReadAll %s: %v", suffix, err)
}
return string(b)
}
if got := get("/bar/"); !strings.Contains(got, ">foo.txt<") {
t.Errorf("got:\n%v\nwant a directory listing with foo.txt\n", got)
}
if got, want := get("/bar/foo.txt"), "Hello world"; got != want {
t.Errorf("got:\n%v\nwant:\n%v\n", got, want)
}
}