This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
route_group_test.go
94 lines (81 loc) · 2.15 KB
/
route_group_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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package clevergo
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewRouteGroup(t *testing.T) {
tests := []struct {
path string
expectedPath string
shouldPanic bool
}{
{"without-prefix-slash", "", true},
{"/", "/", false},
{"//", "/", false},
{"/users", "/users", false},
{"/users/", "/users", false},
}
app := New()
for _, test := range tests {
if test.shouldPanic {
recv := catchPanic(func() {
newRouteGroup(app, test.path)
})
assert.NotNil(t, recv)
continue
}
route := newRouteGroup(app, test.path)
assert.Equal(t, test.expectedPath, route.path)
assert.Equal(t, test.expectedPath, route.name)
}
}
func ExampleRouteGroup() {
app := New()
api := app.Group("/api", RouteGroupMiddleware(echoMiddleware("api")))
v1 := api.Group("/v1", RouteGroupMiddleware(
echoMiddleware("v1"),
echoMiddleware("authenticate"),
))
v1.Get("/users/:name", func(c *Context) error {
c.WriteString(fmt.Sprintf("user: %s", c.Params.String("name")))
return nil
}, RouteMiddleware(
echoMiddleware("fizz1"),
echoMiddleware("fizz2"),
))
v2 := api.Group("/v2", RouteGroupMiddleware(
echoMiddleware("v2"),
echoMiddleware("authenticate"),
))
v2.Get("/users/:name", func(c *Context) error {
c.WriteString(fmt.Sprintf("user: %s", c.Params.String("name")))
return nil
}, RouteMiddleware(
echoMiddleware("buzz1"),
echoMiddleware("buzz2"),
))
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/users/foo", nil)
app.ServeHTTP(w, req)
fmt.Println(w.Body.String())
w = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodGet, "/api/v2/users/bar", nil)
app.ServeHTTP(w, req)
fmt.Println(w.Body.String())
// Output:
// api v1 authenticate fizz1 fizz2 user: foo
// api v2 authenticate buzz1 buzz2 user: bar
}
func TestRouteGroupName(t *testing.T) {
for _, name := range []string{"foo", "bar"} {
g := &RouteGroup{}
RouteGroupName(name)(g)
assert.Equal(t, name, g.name)
}
}