-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_test.go
75 lines (70 loc) · 1.5 KB
/
route_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
package fox
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
)
func TestRoute_HandleMiddlewareMalloc(t *testing.T) {
f, _ := New()
for _, rte := range githubAPI {
require.NoError(t, onlyError(f.Handle(rte.method, rte.path, emptyHandler)))
}
for _, rte := range githubAPI {
req := httptest.NewRequest(rte.method, rte.path, nil)
w := httptest.NewRecorder()
r, c, _ := f.Lookup(&recorder{ResponseWriter: w}, req)
allocs := testing.AllocsPerRun(100, func() {
r.HandleMiddleware(c)
})
c.Close()
assert.Equal(t, float64(0), allocs)
}
}
func TestRoute_HostnamePath(t *testing.T) {
cases := []struct {
name string
pattern string
wantPath string
wantHost string
}{
{
name: "only path",
pattern: "/foo/bar",
wantPath: "/foo/bar",
},
{
name: "only slash",
pattern: "/",
wantPath: "/",
},
{
name: "host and path",
pattern: "a.b.c/foo/bar",
wantPath: "/foo/bar",
wantHost: "a.b.c",
},
{
name: "host and slash",
pattern: "a.b.c/",
wantPath: "/",
wantHost: "a.b.c",
},
{
name: "single letter host and slash",
pattern: "a/",
wantPath: "/",
wantHost: "a",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f, _ := New()
r, err := f.Handle(http.MethodGet, tc.pattern, emptyHandler)
require.NoError(t, err)
assert.Equal(t, tc.wantHost, r.Hostname())
assert.Equal(t, tc.wantPath, r.Path())
})
}
}