Skip to content

Commit bbbe74e

Browse files
committed
mo' tests
1 parent 540653d commit bbbe74e

File tree

2 files changed

+153
-13
lines changed

2 files changed

+153
-13
lines changed

goredirect_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,45 @@ type outerCase struct {
2828
func TestGoGetHandler(t *testing.T) {
2929
testcases := [...]outerCase{
3030
{
31+
// Simple case
3132
Mappings: []Mapping{
33+
{"git", "https", "github.com", NewStringMapperOrBust("", "/path-to-my-repo/on-github")},
34+
},
35+
DefaultHandler: nil,
36+
InnerCases: []innerCase{
37+
{
38+
URL: "http://myhost.com?go-get=1",
39+
ExpHTTPStatus: http.StatusOK,
40+
ExpRoot: "myhost.com",
41+
ExpVCS: "git",
42+
ExpRedirectRoot: "https://github.com/path-to-my-repo/on-github",
43+
},
44+
},
45+
},
46+
{
47+
// Check that default handler is being called
48+
Mappings: nil,
49+
DefaultHandler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
50+
http.Error(w, "Some error", http.StatusBadRequest)
51+
}),
52+
InnerCases: []innerCase{
53+
{
54+
URL: "http://myhost.com/owner/repo?go-get=1",
55+
ExpHTTPStatus: http.StatusNotFound,
56+
},
57+
{
58+
URL: "http://myhost.com/owner/repo",
59+
ExpHTTPStatus: http.StatusBadRequest,
60+
},
61+
},
62+
},
63+
{
64+
// Advanced cases
65+
Mappings: []Mapping{
66+
{"git", "https", "github.com", NewStringMapperOrBust("/customPath", "/path/to/custom")},
67+
{"git", "https", "github.com", NewStringMapperOrBust("/repo(?P<repo>.+)/user(?P<owner>.+)", "/{{.owner}}/{{.repo}}")},
68+
{"hg", "https", "bitbucket.org", NewStringMapperOrBust("/hg/(?P<owner>.+)/(?P<repo>.+)", "/{{.owner}}/{{.repo}}")},
69+
{"git", "https", "github.com", NewStringMapperOrBust("/(?P<owner>.+)/(?P<repo>.+)\\.git", "/{{.owner}}/{{.repo}}")},
3270
{"git", "https", "github.com", NewStringMapperOrBust("/(?P<owner>.+)/(?P<repo>.+)", "/{{.owner}}/{{.repo}}")},
3371
},
3472
DefaultHandler: nil,
@@ -40,6 +78,41 @@ func TestGoGetHandler(t *testing.T) {
4078
ExpVCS: "git",
4179
ExpRedirectRoot: "https://github.com/owner/repo",
4280
},
81+
{
82+
URL: "http://myhost.com/owner/repo.git?go-get=1",
83+
ExpHTTPStatus: http.StatusOK,
84+
ExpRoot: "myhost.com/owner/repo.git",
85+
ExpVCS: "git",
86+
ExpRedirectRoot: "https://github.com/owner/repo",
87+
},
88+
{
89+
URL: "http://myhost.com/hg/owner/repo?go-get=1",
90+
ExpHTTPStatus: http.StatusOK,
91+
ExpRoot: "myhost.com/hg/owner/repo",
92+
ExpVCS: "hg",
93+
ExpRedirectRoot: "https://bitbucket.org/owner/repo",
94+
},
95+
{
96+
URL: "http://myhost.com/repofoo/userbob?go-get=1",
97+
ExpHTTPStatus: http.StatusOK,
98+
ExpRoot: "myhost.com/repofoo/userbob",
99+
ExpVCS: "git",
100+
ExpRedirectRoot: "https://github.com/bob/foo",
101+
},
102+
{
103+
URL: "http://myhost.com/customPath?go-get=1",
104+
ExpHTTPStatus: http.StatusOK,
105+
ExpRoot: "myhost.com/customPath",
106+
ExpVCS: "git",
107+
ExpRedirectRoot: "https://github.com/path/to/custom",
108+
},
109+
{
110+
URL: "http://myhost.com/customPath/subpkg/path?go-get=1",
111+
ExpHTTPStatus: http.StatusOK,
112+
ExpRoot: "myhost.com/customPath",
113+
ExpVCS: "git",
114+
ExpRedirectRoot: "https://github.com/path/to/custom",
115+
},
43116
{
44117
URL: "http://myhost.com/owner/repo",
45118
ExpHTTPStatus: http.StatusNotFound,

stringmapper_test.go

+80-13
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,80 @@ import (
55
"testing"
66
)
77

8-
type stringMapperMapping struct {
9-
Input string
10-
Output string
11-
Err error
8+
type stringPrefixMapperTestCase struct {
9+
InputPattern string
10+
OutputPattern string
11+
Mappings []stringPrefixMapperMapping
12+
}
13+
14+
type stringPrefixMapperMapping struct {
15+
Input string
16+
Mapped string
17+
Matched string
18+
Tail string
19+
Err error
20+
}
21+
22+
func TestMapStringPrefix(t *testing.T) {
23+
testCases := []stringPrefixMapperTestCase{
24+
{
25+
InputPattern: "/(?P<first>.+)/(?P<second>.+)/",
26+
OutputPattern: "{{.second}}/{{.first}}",
27+
Mappings: []stringPrefixMapperMapping{
28+
{"/first/second/", "second/first", "/first/second/", "", nil},
29+
},
30+
},
31+
{
32+
InputPattern: "/prefix/(?P<first>.+)/(?P<second>.+)/",
33+
OutputPattern: "/newprefix/{{.second}}/{{.first}}",
34+
Mappings: []stringPrefixMapperMapping{
35+
{"/prefix/first/second/", "/newprefix/second/first", "/prefix/first/second/", "", nil},
36+
{"/prefix/first/", "", "", "", errors.New("Error: prefix not matched")},
37+
{"/prefix/first/second/third", "/newprefix/second/first", "/prefix/first/second/", "third", nil},
38+
},
39+
},
40+
{
41+
InputPattern: "/hardcodedprefix/",
42+
OutputPattern: "/newprefix/",
43+
Mappings: []stringPrefixMapperMapping{
44+
{"/hardcodedprefix/", "/newprefix/", "/hardcodedprefix/", "", nil},
45+
{"/foo/", "", "", "", errors.New("Error: prefix not matched")},
46+
{"/hardcodedprefix/sub/path", "/newprefix/", "/hardcodedprefix/", "sub/path", nil},
47+
},
48+
},
49+
}
50+
51+
for _, testCase := range testCases {
52+
t.Logf(`Testing "%s" -> "%s"`, testCase.InputPattern, testCase.OutputPattern)
53+
m, err := NewStringMapper(testCase.InputPattern, testCase.OutputPattern)
54+
if err != nil {
55+
t.Fatalf("Unable to create new StringMapper due to error: %s", err.Error())
56+
}
57+
58+
for _, mapping := range testCase.Mappings {
59+
t.Logf(" mapping string %s", mapping.Input)
60+
61+
actualMapped, actualMatched, actualTail, err := m.MapStringPrefix(mapping.Input)
62+
if err == nil && mapping.Err == nil {
63+
if actualMapped != mapping.Mapped {
64+
t.Errorf("Mapped: %s != %s", mapping.Mapped, actualMapped)
65+
}
66+
if actualMatched != mapping.Matched {
67+
t.Errorf("Matched: %s != %s", mapping.Matched, actualMatched)
68+
}
69+
if actualTail != mapping.Tail {
70+
t.Errorf("Tail: %s != %s", mapping.Tail, actualTail)
71+
}
72+
} else if err == nil || mapping.Err == nil || err.Error() != mapping.Err.Error() {
73+
if err != nil {
74+
t.Errorf("Did not expect error: %s", err.Error())
75+
}
76+
if mapping.Err != nil {
77+
t.Errorf("Expected error: %s", mapping.Err.Error())
78+
}
79+
}
80+
}
81+
}
1282
}
1383

1484
type stringMapperTestCase struct {
@@ -17,21 +87,18 @@ type stringMapperTestCase struct {
1787
Mappings []stringMapperMapping
1888
}
1989

90+
type stringMapperMapping struct {
91+
Input string
92+
Output string
93+
Err error
94+
}
95+
2096
func TestMapString(t *testing.T) {
2197
testCases := []stringMapperTestCase{
22-
{
23-
InputPattern: "/(?P<first>.+)/(?P<second>.+)/",
24-
OutputPattern: "{{.second}}/{{.first}}",
25-
Mappings: []stringMapperMapping{
26-
{"/first/second/", "second/first", nil},
27-
},
28-
},
2998
{
3099
InputPattern: "/prefix/(?P<first>.+)/(?P<second>.+)/",
31100
OutputPattern: "/newprefix/{{.second}}/{{.first}}",
32101
Mappings: []stringMapperMapping{
33-
{"/prefix/first/second/", "/newprefix/second/first", nil},
34-
{"/prefix/first/", "", errors.New("Error: prefix not matched")},
35102
{"/prefix/first/second/third", "", errors.New("Error: not matched")},
36103
},
37104
},

0 commit comments

Comments
 (0)