-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem_test.go
143 lines (126 loc) · 2.84 KB
/
filesystem_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package mid
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/spf13/afero"
)
func TestSpaFileSystem(t *testing.T) {
var indexResponse = "<script src='/js/app.js'></script>"
var javascriptResponse = "document.write('JS loaded')"
var dotfileResponse = "oh noes!"
var apiResponse = "api"
// apiHandler would represent a response from the Go backend instead
// of a static asset request
apiHandler := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(apiResponse))
}
//
// Virtual, in-memory filesystem
//
var fs = afero.NewMemMapFs()
file, err := fs.Create("/index.html")
if err != nil {
t.Fatal(err)
}
file.Write([]byte(indexResponse))
err = file.Close()
if err != nil {
t.Fatal(err)
}
file, err = fs.Create("/.git/HEAD")
if err != nil {
t.Fatal(err)
}
file.Write([]byte(dotfileResponse))
err = file.Close()
if err != nil {
t.Fatal(err)
}
file, err = fs.Create("/js/app.js")
if err != nil {
t.Fatal(err)
}
file.Write([]byte(javascriptResponse))
err = file.Close()
if err != nil {
t.Fatal(err)
}
// In the real world we would be using http.Dir("/dist") instead
// dirFs := http.Dir("/dist")
dirFs := afero.NewHttpFs(fs)
// The FileSystems we are actually testing
// spaFS := SpaFileSystem(NewDotFileHidingFileSystem(dirFs))
spaFS := SpaFileSystem(dirFs)
testCases := []struct {
desc string
path string
handler http.Handler
response string
location string
}{
{
desc: "api",
path: "/api",
handler: http.HandlerFunc(apiHandler),
response: apiResponse,
},
{
desc: "index",
path: "/",
handler: http.FileServer(spaFS),
response: indexResponse,
},
{
desc: "index.html",
path: "/index.html",
handler: http.FileServer(spaFS),
// response: indexResponse,
location: "./",
},
{
desc: "missing",
path: "/foo/bar",
handler: http.FileServer(spaFS),
response: indexResponse,
},
{
desc: "dotfile",
path: "/.git/HEAD",
handler: http.FileServer(spaFS),
response: dotfileResponse,
// location: "./",
},
{
desc: "javascript",
path: "/js/app.js",
handler: http.FileServer(spaFS),
response: javascriptResponse,
},
{
desc: "javascript",
path: "/js/",
handler: http.FileServer(spaFS),
response: "404 page not found\n",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
req, err := http.NewRequest("GET", tc.path, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
tc.handler.ServeHTTP(rr, req)
if tc.response != "" {
if rr.Body.String() != tc.response {
t.Errorf("got: %q\nwant: %q\n", rr.Body.String(), tc.response)
}
} else {
if rr.Header().Get("Location") != tc.location {
t.Errorf("got: %q\nwant: %q\n", rr.HeaderMap, tc.location)
}
}
})
}
}