-
Notifications
You must be signed in to change notification settings - Fork 6
/
vanity_test.go
466 lines (443 loc) · 11.2 KB
/
vanity_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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package vanity_test
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"kkn.fi/vanity"
)
var addr = "https://kkn.fi"
func TestHTTPMethodsSupport(t *testing.T) {
tests := []struct {
method string
status int
}{
{
method: http.MethodGet,
status: http.StatusOK,
},
{
method: http.MethodHead,
status: http.StatusMethodNotAllowed,
},
{
method: http.MethodPost,
status: http.StatusMethodNotAllowed,
},
{
method: http.MethodPut,
status: http.StatusMethodNotAllowed,
},
{
method: http.MethodDelete,
status: http.StatusMethodNotAllowed,
},
{
method: http.MethodTrace,
status: http.StatusMethodNotAllowed,
},
{
method: http.MethodOptions,
status: http.StatusMethodNotAllowed,
},
}
for _, test := range tests {
test := test
t.Run(test.method, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(test.method, addr+"/gist?go-get=1", nil)
rec := httptest.NewRecorder()
srv, err := vanity.NewHandlerWithOptions(
vanity.Log(log.New(io.Discard, "", 0)),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != test.status {
t.Errorf("Expecting status code %v for method '%v', but got %v", test.status, test.method, res.StatusCode)
}
})
}
}
func TestHostOptionGoTool(t *testing.T) {
tests := []struct {
name string
url string
result string
}{
{
name: "host go.kkn.fi/vanity redirects to kkn.fi/vanity",
url: "https://go.kkn.fi/vanity?go-get=1",
result: "kkn.fi/vanity git https://github.com/kare/vanity",
},
{
name: "hostname go.kkn.fi/infra redirects to kkn.fi/infra",
url: "https://go.kkn.fi/infra?go-get=1",
result: "kkn.fi/infra git https://github.com/kare/infra",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, test.url, nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.VCSURL("https://github.com/kare"),
vanity.Host("kkn.fi"),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("expected response status %v, but got %v", http.StatusOK, res.StatusCode)
}
body, _ := io.ReadAll(res.Body)
if !strings.Contains(string(body), test.result) {
t.Errorf("expecting\n%v be contained in\n%v", test.result, string(body))
}
})
}
}
func TestHostOptionBrowserGoDoc(t *testing.T) {
tests := []struct {
name string
url string
result string
}{
{
name: "host go.kkn.fi/vanity redirects to kkn.fi/vanity",
url: "https://go.kkn.fi/vanity",
result: `<a href="https://pkg.go.dev/kkn.fi/vanity">Temporary Redirect</a>.`,
},
{
name: "hostname go.kkn.fi/infra redirects to kkn.fi/infra",
url: "https://go.kkn.fi/infra",
result: `<a href="https://pkg.go.dev/kkn.fi/infra">Temporary Redirect</a>.`,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, test.url, nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.VCSURL("https://github.com/kare"),
vanity.Host("kkn.fi"),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusTemporaryRedirect {
t.Errorf("expected response status %v, but got %v", http.StatusTemporaryRedirect, res.StatusCode)
}
body, _ := io.ReadAll(res.Body)
if !strings.Contains(string(body), test.result) {
t.Errorf("expecting\n%v be contained in\n%v", test.result, string(body))
}
})
}
}
func TestIndexPageNotFound(t *testing.T) {
tests := []struct {
name string
url string
}{
{
name: "with trailing slash",
url: "https://kkn.fi/",
},
{
name: "without trailing slash",
url: "https://kkn.fi",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, test.url, nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.VCSURL("https://github.com/kare"),
vanity.Log(log.New(io.Discard, "", 0)),
vanity.StaticDir("/tmp", "/.static/"),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusNotFound {
t.Errorf("%v: expected response status 404, but got %v\n", test.name, res.StatusCode)
}
})
}
}
func TestBrowserGoDoc(t *testing.T) {
tests := []struct {
path string
moduleServer string
result string
}{
{
path: "/gist",
moduleServer: "https://pkg.go.dev/",
result: "https://pkg.go.dev/kkn.fi/gist",
},
{
path: "/gist/",
moduleServer: "https://pkg.go.dev",
result: "https://pkg.go.dev/kkn.fi/gist",
},
{
path: "/set",
moduleServer: "https://pkg.go.dev",
result: "https://pkg.go.dev/kkn.fi/set",
},
{
path: "/cmd/kkn.fi-srv",
moduleServer: "https://pkg.go.dev",
result: "https://pkg.go.dev/kkn.fi/cmd/kkn.fi-srv",
},
{
path: "/cmd/tcpproxy/",
moduleServer: "https://pkg.go.dev",
result: "https://pkg.go.dev/kkn.fi/cmd/tcpproxy",
},
{
path: "/pkgabc/sub/foo",
moduleServer: "https://pkg.go.dev",
result: "https://pkg.go.dev/kkn.fi/pkgabc/sub",
},
{
path: "/vanity",
moduleServer: "",
result: "https://pkg.go.dev/kkn.fi/vanity",
},
{
path: "/cmd/healthcheck",
moduleServer: "https://github.com/kare",
result: "https://github.com/kare/healthcheck",
},
{
path: "/vanity",
moduleServer: "https://github.com/kare/",
result: "https://github.com/kare/vanity",
},
}
for _, test := range tests {
test := test
t.Run(test.path, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, addr+test.path, nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.ModuleServerURL(test.moduleServer),
vanity.Log(log.New(io.Discard, "", 0)),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusTemporaryRedirect {
t.Errorf("expected response status %v, but got %v", http.StatusTemporaryRedirect, res.StatusCode)
}
body, _ := io.ReadAll(res.Body)
if !strings.Contains(string(body), test.result) {
t.Errorf("expecting\n%v be contained in\n%v", test.result, string(body))
}
})
}
}
func TestGoTool(t *testing.T) {
tests := []struct {
path string
vcs string
vcsURL string
result string
}{
{
path: "/gist?go-get=1",
vcs: "hg",
vcsURL: "https://bitbucket.org/kare/",
result: "kkn.fi/gist hg https://bitbucket.org/kare/gist",
},
{
path: "/set/?go-get=1",
vcs: "hg",
vcsURL: "https://bitbucket.org/kare",
result: "kkn.fi/set hg https://bitbucket.org/kare/set",
},
{
path: "/cmd/kkn.fi-srv?go-get=1",
vcs: "git",
vcsURL: "https://github.com/kare/",
result: "kkn.fi/cmd/kkn.fi-srv git https://github.com/kare/kkn.fi-srv",
},
{
path: "/cmd/kkn.fi-srv/?go-get=1",
vcs: "git",
vcsURL: "https://github.com/kare",
result: "kkn.fi/cmd/kkn.fi-srv git https://github.com/kare/kkn.fi-srv",
},
{
path: "/pkgabc/sub/foo?go-get=1",
vcs: "git",
vcsURL: "https://github.com/kare",
result: "kkn.fi/pkgabc/sub/foo git https://github.com/kare/pkgabc",
},
{
path: "/pkgabc/sub/foo/?go-get=1",
vcs: "git",
vcsURL: "https://github.com/kare",
result: "kkn.fi/pkgabc/sub/foo git https://github.com/kare/pkgabc",
},
}
for _, test := range tests {
test := test
t.Run(test.path, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, addr+test.path, nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.VCS(test.vcs),
vanity.VCSURL(test.vcsURL),
vanity.Log(log.New(io.Discard, "", 0)),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
body, _ := io.ReadAll(res.Body)
expected := fmt.Sprintf(`<meta name="go-import" content="%v">`, test.result)
if !strings.Contains(string(body), expected) {
t.Errorf("expecting url '%v' body to contain html meta tag:\n%v, but got:\n%v", test.path, expected, string(body))
}
if res.StatusCode != http.StatusOK {
t.Errorf("expected response status 200, but got %v", res.StatusCode)
}
})
}
}
func TestStaticDir(t *testing.T) {
tests := []struct {
name string
url string
}{
{
name: "with trailing slash",
url: "https://kkn.fi/",
},
{
name: "without trailing slash",
url: "https://kkn.fi",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, test.url, nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.StaticDir("testdata", "dir"),
vanity.Log(log.New(io.Discard, "", 0)),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("%v: expected response status 200, but got %v", test.name, res.StatusCode)
}
body, _ := io.ReadAll(res.Body)
expected := `<!doctype html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<p>homepage</p>
</body>
</html>
`
if string(body) != expected {
t.Errorf("%v: expecting body to match:\n'%v', but got:\n'%s'", test.name, expected, body)
}
})
}
}
func TestRobotsTxt(t *testing.T) {
tests := []struct {
name string
value string
}{
{
name: "GET default /robots.txt",
value: ``,
},
{
name: "GET custom /robots.txt",
value: `robots are here`,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "https://kkn.fi/robots.txt", nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.RobotsTxt(test.value),
)
if err != nil {
t.Error(err)
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("%v: expected response status 200, but got %v", test.name, res.StatusCode)
}
body, _ := io.ReadAll(res.Body)
expected := vanity.DefaultRobotsTxt
if test.value != "" {
expected = test.value
}
if string(body) != expected {
t.Errorf("%v: expecting body to match:\n'%v', but got:\n'%s'", test.name, expected, body)
}
})
}
}
func ExampleHandler() {
errorLog := log.New(os.Stderr, "vanity: ", log.Ldate|log.Ltime|log.LUTC)
srv, err := vanity.NewHandlerWithOptions(
vanity.ModuleServerURL("https://pkg.go.dev"),
vanity.VCSURL("https://github.com/kare"),
vanity.VCS("git"),
vanity.Host("go.kkn.fi"),
vanity.Log(errorLog),
vanity.StaticDir("testdata", "/.static/"),
vanity.IndexPageHandler(vanity.DefaultIndexPageHandler("testdata/index.html")),
)
if err != nil {
fmt.Fprintf(os.Stderr, "error while running vanity handler options: %v", err)
}
http.Handle("/", srv)
// Output:
}