This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
92 lines (83 loc) · 2.07 KB
/
main_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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
type fixture struct {
header string
hostURL string
method string
reqURL string
statusCode int
}
func TestHandler(t *testing.T) {
t.Parallel()
testTable := make(map[string]fixture)
testTable["TestHTTPSRedirect"] = fixture{
statusCode: http.StatusMovedPermanently,
reqURL: "http://syscll.org/vanity",
}
testTable["TestProtoHeaderHTTPSRedirect"] = fixture{
header: "http",
reqURL: "http://syscll.org/vanity",
statusCode: http.StatusMovedPermanently,
}
testTable["TestMethodNotAllowed"] = fixture{
method: http.MethodPost,
reqURL: "https://syscll.org/vanity",
statusCode: http.StatusMethodNotAllowed,
}
testTable["TestRedirectURLError"] = fixture{
hostURL: "invalid host",
method: http.MethodGet,
reqURL: "https://syscll.org/vanity",
statusCode: http.StatusInternalServerError,
}
testTable["TestNoGoGet"] = fixture{
method: http.MethodGet,
reqURL: "https://syscll.org/vanity",
statusCode: http.StatusTemporaryRedirect,
}
testTable["TestNoPath"] = fixture{
method: http.MethodGet,
reqURL: "https://syscll.org?go-get=1",
statusCode: http.StatusTemporaryRedirect,
}
testTable["TestSuccess"] = fixture{
method: http.MethodGet,
reqURL: "https://syscll.org/vanity?go-get=1",
statusCode: http.StatusOK,
}
for name, test := range testTable {
t.Run(name, func(t *testing.T) {
t.Parallel()
u, err := url.Parse(test.reqURL)
if err != nil {
t.Fatal(err)
}
r := &http.Request{
Header: make(http.Header),
Method: test.method,
URL: u,
}
if test.header != "" {
r.Header.Add("X-Forwarded-Proto", test.header)
}
u, err = url.Parse("https://github.com/syscll")
if err != nil {
t.Fatal(err)
}
if test.hostURL != "" {
u.Host = test.hostURL
}
h := handler("git", u)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, r)
if rec.Code != test.statusCode {
t.Errorf("expected status code: %d, got: %d", test.statusCode, rec.Code)
}
})
}
}