Skip to content

Commit 51cd267

Browse files
committed
添加router entry 测试
1 parent ba4ccee commit 51cd267

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

http/router/entry.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package router
22

3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// NewEntry 行健条目
9+
func NewEntry(path, method, resource string) *Entry {
10+
return &Entry{
11+
Path: path,
12+
Method: method,
13+
Resource: resource,
14+
Labels: map[string]string{},
15+
}
16+
}
17+
318
// Entry 路由条目
419
type Entry struct {
5-
Path string `json:"path,omitempty"`
6-
Method string `json:"method,omitempty"`
7-
FunctionName string `json:"function_name,omitempty"`
8-
Resource string `json:"resource,omitempty"`
9-
AuthEnable bool `json:"auth_enable"`
10-
PermissionEnable bool `json:"permission_enable"`
11-
Labels map[string]string `json:"labels,omitempty"`
20+
Path string `bson:"path" json:"path,omitempty"`
21+
Method string `bson:"method" json:"method,omitempty"`
22+
FunctionName string `bson:"function_name" json:"function_name,omitempty"`
23+
Resource string `bson:"resource" json:"resource,omitempty"`
24+
AuthEnable bool `bson:"auth_enable" json:"auth_enable"`
25+
PermissionEnable bool `bson:"permission_enable" json:"permission_enable"`
26+
Labels map[string]string `bson:"labels" json:"labels,omitempty"`
27+
}
28+
29+
func (e *Entry) String() string {
30+
return fmt.Sprintf("%s [%s] %s %v", e.Path, e.Method, e.Resource, e.Labels)
1231
}
1332

1433
// AddLabel 添加Label
@@ -54,9 +73,20 @@ type EntrySet struct {
5473
Items []*Entry `json:"items"`
5574
}
5675

76+
func (s *EntrySet) String() string {
77+
strs := []string{}
78+
for i := range s.Items {
79+
strs = append(strs, s.Items[i].String())
80+
}
81+
82+
return strings.Join(strs, "\n")
83+
}
84+
5785
// AddEntry 添加Entry
58-
func (s *EntrySet) AddEntry(e Entry) {
59-
s.Items = append(s.Items, &e)
86+
func (s *EntrySet) AddEntry(es ...Entry) {
87+
for i := range es {
88+
s.Items = append(s.Items, &es[i])
89+
}
6090
}
6191

6292
// GetEntry 获取条目

http/router/entry_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package router_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/infraboard/mcube/http/label"
9+
"github.com/infraboard/mcube/http/router"
10+
)
11+
12+
func TestEntry(t *testing.T) {
13+
should := assert.New(t)
14+
15+
e := router.NewEntry("/mcube/v1/", "GET", "Monkey")
16+
e.EnableAuth()
17+
e.EnablePermission()
18+
e.AddLabel(label.Get)
19+
20+
should.Equal("/mcube/v1/ [GET] Monkey map[action:get]", e.String())
21+
22+
set := router.NewEntrySet()
23+
set.AddEntry(*e, *e)
24+
should.Equal("/mcube/v1/ [GET] Monkey map[action:get]\n/mcube/v1/ [GET] Monkey map[action:get]", set.String())
25+
}

0 commit comments

Comments
 (0)