forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
366 lines (350 loc) · 11.2 KB
/
response_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package goproxy
import (
"errors"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestSetResponseCacheControlHeader(t *testing.T) {
for _, tt := range []struct {
n int
maxAge int
wantCacheControl string
}{
{1, 60, "public, max-age=60"},
{2, 0, "public, max-age=0"},
{3, -1, "must-revalidate, no-cache, no-store"},
{4, -2, ""},
} {
rec := httptest.NewRecorder()
setResponseCacheControlHeader(rec, tt.maxAge)
recr := rec.Result()
if got, want := recr.Header.Get("Cache-Control"), tt.wantCacheControl; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
}
}
func TestResponseString(t *testing.T) {
for _, tt := range []struct {
n int
method string
content string
wantContent string
}{
{
n: 1,
content: "foobar",
wantContent: "foobar",
},
{
n: 2,
method: http.MethodHead,
content: "foobar",
},
} {
rec := httptest.NewRecorder()
responseString(rec, httptest.NewRequest(tt.method, "/", nil), http.StatusOK, 60, tt.content)
recr := rec.Result()
if got, want := recr.StatusCode, http.StatusOK; got != want {
t.Errorf("test(%d): got %d, want %d", tt.n, got, want)
}
if got, want := recr.Header.Get("Content-Type"), "text/plain; charset=utf-8"; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if got, want := recr.Header.Get("Cache-Control"), "public, max-age=60"; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if b, err := io.ReadAll(recr.Body); err != nil {
t.Fatalf("test(%d): unexpected error %q", tt.n, err)
} else if got, want := string(b), tt.wantContent; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
}
}
func TestResponseNotFound(t *testing.T) {
for _, tt := range []struct {
n int
msgs []any
wantContent string
}{
{1, nil, "not found"},
{2, []any{}, "not found"},
{3, []any{""}, "not found"},
{4, []any{"not found"}, "not found"},
{5, []any{fs.ErrNotExist}, "not found: file does not exist"},
{6, []any{"foobar"}, "not found: foobar"},
{7, []any{"foo", "bar"}, "not found: foobar"},
{8, []any{errors.New("foo"), "bar"}, "not found: foobar"},
{9, []any{"not found: foobar"}, "not found: foobar"},
{10, []any{"bad request: foobar"}, "not found: foobar"},
{11, []any{"gone: foobar"}, "not found: foobar"},
} {
rec := httptest.NewRecorder()
responseNotFound(rec, httptest.NewRequest("", "/", nil), 60, tt.msgs...)
recr := rec.Result()
if got, want := recr.StatusCode, http.StatusNotFound; got != want {
t.Errorf("test(%d): got %d, want %d", tt.n, got, want)
}
if got, want := recr.Header.Get("Content-Type"), "text/plain; charset=utf-8"; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if got, want := recr.Header.Get("Cache-Control"), "public, max-age=60"; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if b, err := io.ReadAll(recr.Body); err != nil {
t.Fatalf("test(%d): unexpected error %q", tt.n, err)
} else if got, want := string(b), tt.wantContent; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
}
}
func TestResponseMethodNotAllowed(t *testing.T) {
rec := httptest.NewRecorder()
responseMethodNotAllowed(rec, httptest.NewRequest("", "/", nil), 60)
recr := rec.Result()
if got, want := recr.StatusCode, http.StatusMethodNotAllowed; got != want {
t.Errorf("got %d, want %d", got, want)
}
if got, want := recr.Header.Get("Content-Type"), "text/plain; charset=utf-8"; got != want {
t.Errorf("got %q, want %q", got, want)
}
if got, want := recr.Header.Get("Cache-Control"), "public, max-age=60"; got != want {
t.Errorf("got %q, want %q", got, want)
}
if b, err := io.ReadAll(recr.Body); err != nil {
t.Fatalf("unexpected error %q", err)
} else if got, want := string(b), "method not allowed"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestResponseInternalServerError(t *testing.T) {
rec := httptest.NewRecorder()
responseInternalServerError(rec, httptest.NewRequest("", "/", nil))
recr := rec.Result()
if got, want := recr.StatusCode, http.StatusInternalServerError; got != want {
t.Errorf("got %d, want %d", got, want)
}
if got, want := recr.Header.Get("Content-Type"), "text/plain; charset=utf-8"; got != want {
t.Errorf("got %q, want %q", got, want)
}
if got, want := recr.Header.Get("Cache-Control"), ""; got != want {
t.Errorf("got %q, want %q", got, want)
}
if b, err := io.ReadAll(recr.Body); err != nil {
t.Fatalf("unexpected error %q", err)
} else if got, want := string(b), "internal server error"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}
type successResponseBody_Size struct {
io.Reader
size int64
}
func (srb successResponseBody_Size) Size() int64 { return srb.size }
type successResponseBody_LastModified struct {
io.Reader
lastModified time.Time
}
func (srb successResponseBody_LastModified) LastModified() time.Time { return srb.lastModified }
type successResponseBody_ModTime struct {
io.Reader
modTime time.Time
}
func (srb successResponseBody_ModTime) ModTime() time.Time { return srb.modTime }
type successResponseBody_ETag struct {
io.Reader
etag string
}
func (srb successResponseBody_ETag) ETag() string { return srb.etag }
func TestResponseSuccess(t *testing.T) {
for _, tt := range []struct {
n int
method string
content io.Reader
wantContentLength int64
wantLastModified string
wantETag string
wantContent string
}{
{
n: 1,
content: strings.NewReader("foobar"),
wantContentLength: 6,
wantContent: "foobar",
},
{
n: 2,
method: http.MethodHead,
wantContentLength: 6,
content: strings.NewReader("foobar"),
},
{
n: 3,
content: successResponseBody_Size{
Reader: strings.NewReader("foobar"),
size: 6,
},
wantContentLength: 6,
wantContent: "foobar",
},
{
n: 4,
content: successResponseBody_LastModified{
Reader: strings.NewReader("foobar"),
lastModified: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
},
wantContentLength: -1,
wantLastModified: "Sat, 01 Jan 2000 00:00:00 GMT",
wantContent: "foobar",
},
{
n: 5,
content: successResponseBody_ModTime{
Reader: strings.NewReader("foobar"),
modTime: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
},
wantContentLength: -1,
wantLastModified: "Sat, 01 Jan 2000 00:00:00 GMT",
wantContent: "foobar",
},
{
n: 6,
content: successResponseBody_ETag{
Reader: strings.NewReader("foobar"),
etag: `"foobar"`,
},
wantContentLength: -1,
wantETag: `"foobar"`,
wantContent: "foobar",
},
{
n: 7,
content: struct {
io.Reader
successResponseBody_Size
successResponseBody_LastModified
successResponseBody_ModTime
successResponseBody_ETag
}{
strings.NewReader("foobar"),
successResponseBody_Size{size: 6},
successResponseBody_LastModified{lastModified: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)},
successResponseBody_ModTime{modTime: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC)},
successResponseBody_ETag{etag: `"foobar"`},
},
wantContentLength: 6,
wantLastModified: "Sat, 01 Jan 2000 00:00:00 GMT",
wantETag: `"foobar"`,
wantContent: "foobar",
},
} {
rec := httptest.NewRecorder()
responseSuccess(rec, httptest.NewRequest(tt.method, "/", nil), tt.content, "text/plain; charset=utf-8", 60)
recr := rec.Result()
if got, want := recr.StatusCode, http.StatusOK; got != want {
t.Errorf("test(%d): got %d, want %d", tt.n, got, want)
}
if got, want := recr.Header.Get("Content-Type"), "text/plain; charset=utf-8"; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if got, want := recr.Header.Get("Cache-Control"), "public, max-age=60"; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if got, want := recr.ContentLength, tt.wantContentLength; got != want {
t.Errorf("test(%d): got %d, want %d", tt.n, got, want)
}
if got, want := recr.Header.Get("Last-Modified"), tt.wantLastModified; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if got, want := recr.Header.Get("ETag"), tt.wantETag; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if b, err := io.ReadAll(recr.Body); err != nil {
t.Fatalf("test(%d): unexpected error %q", tt.n, err)
} else if got, want := string(b), tt.wantContent; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
}
}
func TestResponseError(t *testing.T) {
for _, tt := range []struct {
n int
err error
cacheSensitive bool
wantStatusCode int
wantCacheControl string
wantContent string
}{
{
n: 1,
err: fs.ErrNotExist,
wantStatusCode: http.StatusNotFound,
wantCacheControl: "public, max-age=600",
wantContent: "not found",
},
{
n: 2,
err: errBadUpstream,
wantStatusCode: http.StatusNotFound,
wantCacheControl: "must-revalidate, no-cache, no-store",
wantContent: "not found: bad upstream",
},
{
n: 3,
err: errFetchTimedOut,
wantStatusCode: http.StatusNotFound,
wantCacheControl: "must-revalidate, no-cache, no-store",
wantContent: "not found: fetch timed out",
},
{
n: 4,
err: notExistErrorf("cache sensitive"),
cacheSensitive: true,
wantStatusCode: http.StatusNotFound,
wantCacheControl: "public, max-age=60",
wantContent: "not found: cache sensitive",
},
{
n: 5,
err: notExistErrorf("not found: bad upstream"),
wantStatusCode: http.StatusNotFound,
wantCacheControl: "must-revalidate, no-cache, no-store",
wantContent: "not found: bad upstream",
},
{
n: 6,
err: notExistErrorf("not found: fetch timed out"),
wantStatusCode: http.StatusNotFound,
wantCacheControl: "must-revalidate, no-cache, no-store",
wantContent: "not found: fetch timed out",
},
{
n: 7,
err: errors.New("internal server error"),
wantStatusCode: http.StatusInternalServerError,
wantContent: "internal server error",
},
} {
rec := httptest.NewRecorder()
responseError(rec, httptest.NewRequest("", "/", nil), tt.err, tt.cacheSensitive)
recr := rec.Result()
if got, want := recr.StatusCode, tt.wantStatusCode; got != want {
t.Errorf("test(%d): got %d, want %d", tt.n, got, want)
}
if got, want := recr.Header.Get("Content-Type"), "text/plain; charset=utf-8"; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if got, want := recr.Header.Get("Cache-Control"), tt.wantCacheControl; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
if b, err := io.ReadAll(recr.Body); err != nil {
t.Fatalf("test(%d): unexpected error %q", tt.n, err)
} else if got, want := string(b), tt.wantContent; got != want {
t.Errorf("test(%d): got %q, want %q", tt.n, got, want)
}
}
}