-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstatic_test.go
137 lines (130 loc) · 3.18 KB
/
static_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
package zhttp
import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"testing/fstest"
"zgo.at/zstd/ztest"
)
func TestStatic(t *testing.T) {
files := os.DirFS(".")
tests := []struct {
name string
srv Static
req *http.Request
wantCode int
wantHeaders http.Header
}{
{
"no cache, no packed",
NewStatic("", files, nil),
httptest.NewRequest("GET", "/static_test.go", nil),
200,
http.Header{
"Cache-Control": nil,
"Content-Type": {"text/x-go; charset=utf-8"},
},
},
{
"default cache",
NewStatic("", files, map[string]int{"": 42}),
httptest.NewRequest("GET", "/static_test.go", nil),
200,
http.Header{
"Cache-Control": {"public, max-age=42"},
"Content-Type": {"text/x-go; charset=utf-8"},
},
},
{
"cache",
NewStatic("", files, map[string]int{"": 42, "/static_test.go": 666}),
httptest.NewRequest("GET", "/static_test.go", nil),
200,
http.Header{
"Cache-Control": {"public, max-age=666"},
"Content-Type": {"text/x-go; charset=utf-8"},
},
},
{
"cache glob",
NewStatic("", files, map[string]int{"": 42, "/*.go": 666}),
httptest.NewRequest("GET", "/static_test.go", nil),
200,
http.Header{
"Cache-Control": {"public, max-age=666"},
"Content-Type": {"text/x-go; charset=utf-8"},
},
},
{
"packed cache",
NewStatic("",
fstest.MapFS{"doesnt_exist_on_fs.txt": {Data: []byte("XXXXXXXXXX")}},
map[string]int{"": 42, "/*.go": 666}),
httptest.NewRequest("GET", "/doesnt_exist_on_fs.txt", nil),
200,
http.Header{
"Cache-Control": {"public, max-age=42"},
"Content-Type": {"text/plain; charset=utf-8"},
},
},
{
"packed cache",
NewStatic("",
fstest.MapFS{"doesnt_exist_on_fs.go": {Data: []byte("XXXXXXXXXX")}},
map[string]int{"": 42, "/*.go": 666}),
httptest.NewRequest("GET", "/doesnt_exist_on_fs.go", nil),
200,
http.Header{
"Cache-Control": {"public, max-age=666"},
"Content-Type": {"text/x-go; charset=utf-8"},
},
},
{
"404",
NewStatic("", files, map[string]int{"": 42, "/*.go": 666}),
httptest.NewRequest("GET", "/doesnt_exist_on_fs.go", nil),
404,
http.Header{
"Cache-Control": nil,
"Content-Type": {"text/plain; charset=utf-8"},
},
},
{
"packed 404",
NewStatic("",
fstest.MapFS{"doesnt_exist_on_fs.go": {Data: []byte("XXXXXXXXXX")}},
map[string]int{"": 42, "/*.go": 666}),
httptest.NewRequest("GET", "/doesnt_exist_on_in_pack.go", nil),
404,
http.Header{
"Cache-Control": nil,
"Content-Type": {"text/plain; charset=utf-8"},
},
},
{
"malicious",
NewStatic("", files, map[string]int{"": 42, "/*.go": 666}),
httptest.NewRequest("GET", "/../foo", nil),
403,
http.Header{
"Cache-Control": nil,
"Content-Type": {"text/plain; charset=utf-8"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rr := httptest.NewRecorder()
tt.srv.ServeHTTP(rr, tt.req)
ztest.Code(t, rr, tt.wantCode)
for k, want := range tt.wantHeaders {
got := rr.Header()[k]
if !reflect.DeepEqual(got, want) {
t.Errorf("%q header wrong\ngot: %#v\nwant: %#v", k, got, want)
}
}
})
}
}