-
Notifications
You must be signed in to change notification settings - Fork 12
/
app_resolve_test.go
125 lines (115 loc) · 3.09 KB
/
app_resolve_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
package gimlet
import (
"bytes"
"fmt"
"net/http"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRouteResolutionHelpers(t *testing.T) {
hndlr := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
logger := MakeRecoveryLogger()
app := &APIApp{}
app.SetPrefix("baz")
for _, tc := range []struct {
route *APIRoute
expected string
addPrefix bool
}{
{
expected: "/v1/foo/bar",
addPrefix: false,
route: &APIRoute{
prefix: "/foo",
version: 1,
route: "/bar",
handler: hndlr,
},
},
{
expected: "/baz/v1/foo/bar",
addPrefix: true,
route: &APIRoute{
prefix: "/foo",
version: 1,
handler: hndlr,
route: "/bar",
},
},
{
expected: "/baz/v1/foo/bar",
addPrefix: true,
route: &APIRoute{
handler: hndlr,
version: 1,
route: "/foo/bar",
},
},
{
expected: "/v1/foo/bar",
addPrefix: false,
route: &APIRoute{
prefix: "/foo",
handler: hndlr,
version: 1,
route: "/bar",
},
},
{
expected: "/foo/v1/bar",
addPrefix: true,
route: &APIRoute{
prefix: "/foo",
handler: hndlr,
version: 1,
route: "/bar",
overrideAppPrefix: true,
},
},
{
expected: "/v1/foo/bar",
addPrefix: false,
route: &APIRoute{
prefix: "/foo",
handler: hndlr,
version: 1,
route: "/bar",
overrideAppPrefix: true,
},
},
} {
// by default there's no middleware and everything's
// the same
assert.Equal(t, tc.expected, tc.route.resolveVersionedRoute(app, tc.addPrefix))
h := tc.route.getHandlerWithMiddlware(nil)
assert.Equal(t, fmt.Sprint(hndlr), fmt.Sprint(h))
// if there's global middleware, we're different
h = tc.route.getHandlerWithMiddlware([]Middleware{logger})
assert.NotEqual(t, fmt.Sprint(hndlr), fmt.Sprint(h))
// if you add wrapper middleware we're different differently
tc.route.wrappers = append(tc.route.wrappers, logger)
h = tc.route.getHandlerWithMiddlware(nil)
assert.NotEqual(t, fmt.Sprint(hndlr), fmt.Sprint(h))
}
}
func TestPrefixRoute(t *testing.T) {
router := mux.NewRouter()
app := NewApp()
app.NoVersions = true
app.AddPrefixRoute("/match/everything/under/this/path").Handler(func(http.ResponseWriter, *http.Request) {}).Get()
assert.NoError(t, app.attachRoutes(router, false))
// match the path itself
req, err := http.NewRequest("GET", "http://www.example.com/match/everything/under/this/path", bytes.NewBuffer([]byte{}))
require.NoError(t, err)
assert.True(t, router.Match(req, &mux.RouteMatch{}))
// match under this path
req, err = http.NewRequest("GET", "http://www.example.com/match/everything/under/this/path/abcd", bytes.NewBuffer([]byte{}))
require.NoError(t, err)
assert.True(t, router.Match(req, &mux.RouteMatch{}))
// don't match another path
req, err = http.NewRequest("GET", "http://www.example.com/another/path", bytes.NewBuffer([]byte{}))
require.NoError(t, err)
assert.False(t, router.Match(req, &mux.RouteMatch{}))
}