forked from google/weasel
/
handle_test.go
327 lines (296 loc) · 9 KB
/
handle_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package weasel
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
"google.golang.org/appengine/memcache"
)
func TestValidate_ConfigFile(t *testing.T) {
var cfg appConfig
if err := readConfig(&cfg, configFile); err != nil {
t.Fatal(err)
}
if _, ok := cfg.Buckets["default"]; !ok {
t.Errorf("want default bucket in %+v", cfg)
}
}
func TestRedirect(t *testing.T) {
ti, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer ti.Close()
const (
redirectTo = "https://www.example.com"
code = http.StatusFound
)
handler := redirectHandler(redirectTo, code)
urls := []string{"/", "/page", "/page/", "/page?with=query"}
for _, u := range urls {
req, err := ti.NewRequest("GET", u, nil)
if err != nil {
t.Errorf("%s: %v", u, err)
continue
}
req.Host = "example.org"
res := httptest.NewRecorder()
handler.ServeHTTP(res, req)
if res.Code != code {
t.Errorf("%s: res.Code = %d; want %d", u, res.Code, code)
}
redir := redirectTo + u
if v := res.Header().Get("location"); v != redir {
t.Errorf("%s: location = %q; want %q", u, v, redir)
}
}
}
func TestServe_DefaultGCS(t *testing.T) {
ti, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer ti.Close()
const (
bucket = "default-bucket"
reqFile = "/dir/"
realFile = bucket + "/dir/index.html"
contentType = "text/plain"
contents = "contents"
cacheControl = "public,max-age=0"
// dev_appserver app identity stub
authorization = "Bearer InvalidToken:" + scopeStorageRead
)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path[1:] != realFile {
t.Errorf("r.URL.Path = %q; want /%s", r.URL.Path, realFile)
}
if v := r.Header.Get("authorization"); !strings.HasPrefix(v, authorization) {
t.Errorf("auth = %q; want prefix %q", v, authorization)
}
if v, exist := r.Header["X-Foo"]; exist {
t.Errorf("found x-foo: %q", v)
}
// weasel client => GCS always uses gzip where available
if v := r.Header.Get("accept-encoding"); v != "gzip" {
t.Errorf("accept-encoding = %q; want 'gzip'", v)
}
w.Header().Set("cache-control", cacheControl)
w.Header().Set("content-type", contentType)
w.Header().Set("x-test", "should not propagate")
w.Write([]byte(contents))
}))
defer ts.Close()
gcsBase = ts.URL
config.Buckets = map[string]string{"default": bucket}
req, _ := ti.NewRequest("GET", reqFile, nil)
req.Header.Set("accept-encoding", "client/accept")
req.Header.Set("x-foo", "bar")
// make sure we're not getting memcached results
if err := memcache.Flush(appengine.NewContext(req)); err != nil {
t.Fatal(err)
}
res := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
}
if v := res.Header().Get("cache-control"); v != cacheControl {
t.Errorf("cache-control = %q; want %q", v, cacheControl)
}
if v := res.Header().Get("content-type"); v != contentType {
t.Errorf("content-type = %q; want %q", v, contentType)
}
if v := res.Header().Get("x-test"); v != "" {
t.Errorf("found x-test header: %q", v)
}
if s := res.Body.String(); s != contents {
t.Errorf("res.Body = %q; want %q", s, contents)
}
}
func TestServe_Methods(t *testing.T) {
ti, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer ti.Close()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/plain")
w.Write([]byte("methods test"))
}))
defer ts.Close()
gcsBase = ts.URL
config.Buckets = map[string]string{"default": "bucket"}
tests := []struct {
method, body string
code int
}{
{"HEAD", "", http.StatusOK},
{"OPTIONS", "", http.StatusOK},
// it is important that GET comes last to verify requests like HEAD
// do not corrupt object cache
{"GET", "methods test", http.StatusOK},
{"PUT", "", http.StatusMethodNotAllowed},
{"POST", "", http.StatusMethodNotAllowed},
{"DELETE", "", http.StatusMethodNotAllowed},
}
for i, test := range tests {
r, _ := ti.NewRequest(test.method, "/file.txt", nil)
rw := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(rw, r)
if rw.Code != test.code {
t.Errorf("%d: rw.Code = %d; want %d", i, rw.Code, test.code)
}
if v := strings.TrimSpace(rw.Body.String()); v != test.body {
t.Errorf("%d: rw.Body = %q; want %q", i, v, test.body)
}
}
}
func TestServe_Memcache(t *testing.T) {
ti, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer ti.Close()
const (
bucket = "default-bucket"
reqFile = "/index.html"
realFile = bucket + "/index.html"
contentType = "text/html"
contents = "cached file"
cacheControl = "public,max-age=10"
)
req, _ := ti.NewRequest("GET", reqFile, nil)
ctx := appengine.NewContext(req)
obj := &object{
Meta: map[string]string{
"content-type": contentType,
"cache-control": cacheControl,
},
Body: []byte(contents),
}
item := memcache.Item{Key: realFile, Object: obj}
if err := memcache.Gob.Set(ctx, &item); err != nil {
t.Fatal(err)
}
// make sure we don't hit real GCS
gcsBase = "invalid"
// overwrite global config
config.Buckets = map[string]string{"default": bucket}
res := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
}
if v := res.Header().Get("cache-control"); v != cacheControl {
t.Errorf("cache-control = %q; want %q", v, cacheControl)
}
if v := res.Header().Get("content-type"); v != contentType {
t.Errorf("content-type = %q; want %q", v, contentType)
}
if s := res.Body.String(); s != contents {
t.Errorf("res.Body = %q; want %q", s, contents)
}
}
func TestServe_GCSErrors(t *testing.T) {
ti, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer ti.Close()
const code = http.StatusBadRequest
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
}))
defer ts.Close()
gcsBase = ts.URL
req, err := ti.NewRequest("GET", "/bad", nil)
if err != nil {
t.Fatal(err)
}
res := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != code {
t.Errorf("res.Code = %d; want %d", res.Code, code)
}
}
func TestServe_NoTrailSlash(t *testing.T) {
ti, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer ti.Close()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/bucket/dir-one/two/index.html" {
w.WriteHeader(http.StatusNotFound)
return
}
// stat request
if r.Method != "HEAD" {
t.Errorf("r.Method = %q; want HEAD", r.Method)
}
}))
defer ts.Close()
gcsBase = ts.URL
config.Buckets = map[string]string{"default": "bucket"}
req, _ := ti.NewRequest("GET", "/dir-one/two", nil)
// make sure we're not getting memcached results
if err := memcache.Flush(appengine.NewContext(req)); err != nil {
t.Fatal(err)
}
res := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != http.StatusMovedPermanently {
t.Errorf("res.Code = %d; want %d", res.Code, http.StatusMovedPermanently)
}
loc := "/dir-one/two/"
if v := res.Header().Get("location"); v != loc {
t.Errorf("location = %q; want %q", v, loc)
}
}
func TestHook(t *testing.T) {
ti, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer ti.Close()
body := `{"bucket": "dummy", "name": "path/obj"}`
req, _ := ti.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))
const cacheKey = "dummy/path/obj"
ctx := appengine.NewContext(req)
item := &memcache.Item{Key: cacheKey, Value: []byte("ignored")}
if err := memcache.Set(ctx, item); err != nil {
t.Fatal(err)
}
// must remove cached item
res := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
}
if _, err := memcache.Get(ctx, cacheKey); err != memcache.ErrCacheMiss {
t.Fatalf("memcache.Get(%q): %v; want ErrCacheMiss", cacheKey, err)
}
// cache misses must not respond with an error code
req, _ = ti.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))
res = httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
}
}