Skip to content

Commit 078ae11

Browse files
authored
Merge pull request #14 from dpouris/tests
Add Initial Testing Files
2 parents 073a99d + 8c54def commit 078ae11

File tree

4 files changed

+291
-0
lines changed

4 files changed

+291
-0
lines changed

goster_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package goster
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type IsDynamicMatchCase struct {
8+
name string
9+
url string
10+
dynPath string
11+
expectedResult bool
12+
}
13+
14+
func TestIsDynamicRouteMatch(t *testing.T) {
15+
ctx := Ctx{
16+
Meta: Meta{
17+
Query: Params{
18+
values: make(map[string]string),
19+
},
20+
Path: Path{
21+
values: make(map[string]string),
22+
},
23+
},
24+
}
25+
26+
testCases := []IsDynamicMatchCase{
27+
{
28+
name: "Depth 1",
29+
dynPath: "path/another_path/:var",
30+
url: "path/another_path/something",
31+
expectedResult: true,
32+
},
33+
{
34+
name: "Depth 1 (with '/' suffix on URL)",
35+
dynPath: "path/another_path/:var",
36+
url: "path/another_path/something/",
37+
expectedResult: true,
38+
},
39+
{
40+
name: "Depth 1 (with '/' suffix on dynPath)",
41+
dynPath: "path/another_path/:var/",
42+
url: "path/another_path/something",
43+
expectedResult: true,
44+
},
45+
{
46+
name: "Depth 1 (with Depth 2 URL)",
47+
dynPath: "path/another_path/:var",
48+
url: "path/another_path/something/something2",
49+
expectedResult: false,
50+
},
51+
{
52+
name: "Depth 2",
53+
dynPath: "path/another_path/:var/:var2",
54+
url: "path/another_path/something/something2",
55+
expectedResult: true,
56+
},
57+
{
58+
name: "Depth 2 (with Depth 1 URL)",
59+
dynPath: "path/another_path/:var/:var2",
60+
url: "path/another_path/something",
61+
expectedResult: false,
62+
},
63+
{
64+
name: "Depth 2 (with '/' suffix on dynPath)",
65+
dynPath: "path/another_path/:var/:var2/",
66+
url: "path/another_path/something/something2",
67+
expectedResult: true,
68+
},
69+
{
70+
name: "Depth 2 (with '/' suffix on URL)",
71+
dynPath: "path/another_path/:var/:var2",
72+
url: "path/another_path/something/something2/",
73+
expectedResult: true,
74+
},
75+
}
76+
77+
failedCases := make(map[int]IsDynamicMatchCase, 0)
78+
for i, c := range testCases {
79+
if isDynamicRouteMatch(&ctx, c.url, c.dynPath) != c.expectedResult {
80+
failedCases[i] = c
81+
} else {
82+
t.Logf("PASSED [%d] - %s\n", i, c.name)
83+
}
84+
}
85+
86+
// Space
87+
t.Log("")
88+
89+
for i, c := range failedCases {
90+
t.Errorf("FAILED [%d] - %s\n", i, c.name)
91+
}
92+
93+
t.Logf("TOTAL CASES: %d\n", len(testCases))
94+
t.Logf("FAILED CASES: %d\n", len(failedCases))
95+
}

meta_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package goster
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type ParseUrlCase struct {
8+
name string
9+
url string
10+
expectedUrl string
11+
shouldFail bool
12+
}
13+
14+
func TestParseUrl(t *testing.T) {
15+
meta := Meta{
16+
Query: Params{
17+
values: make(map[string]string),
18+
},
19+
}
20+
testCases := []ParseUrlCase{
21+
{
22+
name: "1",
23+
url: "/home/var/",
24+
expectedUrl: "/home/var",
25+
shouldFail: false,
26+
},
27+
{
28+
name: "2",
29+
url: "/home/var/",
30+
expectedUrl: "/home/var/",
31+
shouldFail: true,
32+
},
33+
{
34+
name: "3",
35+
url: "/home/var///",
36+
expectedUrl: "/home/var///",
37+
shouldFail: true,
38+
},
39+
{
40+
name: "4",
41+
url: "/home/var///",
42+
expectedUrl: "/home/var//",
43+
shouldFail: false,
44+
},
45+
{
46+
name: "5",
47+
url: "////",
48+
expectedUrl: "///",
49+
shouldFail: false,
50+
},
51+
}
52+
53+
failedCases := make(map[int]ParseUrlCase, 0)
54+
for i, c := range testCases {
55+
meta.ParseUrl(&c.url)
56+
if (c.url != c.expectedUrl) == !c.shouldFail {
57+
failedCases[i] = c
58+
} else {
59+
t.Logf("PASSED [%d] - %s\n", i, c.name)
60+
}
61+
}
62+
63+
// Space
64+
t.Log("")
65+
66+
for i, c := range failedCases {
67+
t.Errorf("FAILED [%d] - %s\n", i, c.name)
68+
t.Errorf("Expected '%s' path, but got '%s'", c.expectedUrl, c.url)
69+
}
70+
71+
t.Logf("TOTAL CASES: %d\n", len(testCases))
72+
t.Logf("FAILED CASES: %d\n", len(failedCases))
73+
}

methods_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package goster
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
type MethodNewCase struct {
9+
name string
10+
method string
11+
url string
12+
expectedPath string
13+
handler RequestHandler
14+
}
15+
16+
func TestMethodNew(t *testing.T) {
17+
g := NewServer()
18+
testCases := []MethodNewCase{
19+
{
20+
name: "1",
21+
method: "GET",
22+
url: "/home/var",
23+
expectedPath: "/home/var",
24+
handler: func(ctx *Ctx) error { return errors.New("test error") },
25+
},
26+
{
27+
name: "2",
28+
method: "GET",
29+
url: "/home/var////",
30+
expectedPath: "/home/var///",
31+
handler: func(ctx *Ctx) error { return errors.New("test error") },
32+
},
33+
}
34+
35+
failedCases := make(map[int]MethodNewCase, 0)
36+
for i, c := range testCases {
37+
g.New(c.method, c.url, c.handler)
38+
if _, exists := g.Routes[c.method][c.expectedPath]; exists {
39+
t.Logf("PASSED [%d] - %s\n", i, c.name)
40+
} else {
41+
failedCases[i] = c
42+
}
43+
}
44+
45+
// Space
46+
t.Log("")
47+
48+
for i, c := range failedCases {
49+
t.Errorf("FAILED [%d] - %s\n", i, c.name)
50+
}
51+
52+
// Space
53+
t.Log("")
54+
t.Logf("Routes: ")
55+
t.Log(g.Routes)
56+
57+
t.Logf("TOTAL CASES: %d\n", len(testCases))
58+
t.Logf("FAILED CASES: %d\n", len(failedCases))
59+
}

utils_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package goster
2+
3+
import "testing"
4+
5+
type CleanPathCase struct {
6+
name string
7+
path string
8+
expectedPath string
9+
}
10+
11+
func TestCleanPath(t *testing.T) {
12+
testCases := []CleanPathCase{
13+
{
14+
name: "Same path",
15+
path: "home/var",
16+
expectedPath: "/home/var",
17+
},
18+
{
19+
name: "No path",
20+
path: "",
21+
expectedPath: "",
22+
},
23+
{
24+
name: "Single slash",
25+
path: "/",
26+
expectedPath: "",
27+
},
28+
{
29+
name: "A lot of slashes",
30+
path: "/////",
31+
expectedPath: "////",
32+
},
33+
{
34+
name: "Trailing slash",
35+
path: "home/var/",
36+
expectedPath: "/home/var",
37+
},
38+
{
39+
name: "A lot of trailing slashes",
40+
path: "home/var///////",
41+
expectedPath: "/home/var//////",
42+
},
43+
}
44+
45+
failedCases := make(map[int]CleanPathCase, 0)
46+
for i, c := range testCases {
47+
cleanPath(&c.path)
48+
if c.path != c.expectedPath {
49+
failedCases[i] = c
50+
} else {
51+
t.Logf("PASSED [%d] - %s\n", i, c.name)
52+
}
53+
}
54+
55+
// Space
56+
t.Log("")
57+
58+
for i, c := range failedCases {
59+
t.Errorf("FAILED [%d] - %s\n", i, c.name)
60+
}
61+
62+
t.Logf("TOTAL CASES: %d\n", len(testCases))
63+
t.Logf("FAILED CASES: %d\n", len(failedCases))
64+
}

0 commit comments

Comments
 (0)