Skip to content

Commit 8ca5d8c

Browse files
committed
修复API root的条目的顺序
1 parent 4f02cf8 commit 8ca5d8c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

http/router/httprouter/entriy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func newEntrySet() *entrySet {
2323

2424
// EntrySet 路由集合
2525
type entrySet struct {
26+
order []string
2627
items map[string]*entry
2728
}
2829

@@ -34,6 +35,7 @@ func (s *entrySet) AddEntry(es ...*entry) error {
3435
return fmt.Errorf("router entry %s has exist", key)
3536
}
3637
s.items[key] = e
38+
s.order = append(s.order, key)
3739
}
3840

3941
return nil
@@ -42,8 +44,8 @@ func (s *entrySet) AddEntry(es ...*entry) error {
4244
// ShowEntries 显示理由条目
4345
func (s *entrySet) EntrieSet() *router.EntrySet {
4446
es := router.NewEntrySet()
45-
for _, v := range s.items {
46-
es.AddEntry(*v.Entry)
47+
for _, key := range s.order {
48+
es.AddEntry(*s.items[key].Entry)
4749
}
4850

4951
return es

http/router/httprouter/httprouter_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,25 @@ func TestAPIRootOK(t *testing.T) {
140140
should.NotNil(es.GetEntry("/test", "GET"))
141141
}
142142
}
143+
144+
func TestAPIRootOrderOK(t *testing.T) {
145+
should := assert.New(t)
146+
147+
r := httprouter.New()
148+
149+
req, _ := http.NewRequest("GET", "/", nil)
150+
w := httptest.NewRecorder()
151+
152+
r.AddProtected("GET", "/test1", IndexHandler)
153+
r.AddProtected("GET", "/test2", IndexHandler)
154+
r.AddProtected("GET", "/test3", IndexHandler)
155+
r.EnableAPIRoot()
156+
r.ServeHTTP(w, req)
157+
158+
es := router.NewEntrySet()
159+
if should.NoError(response.GetDataFromBody(w.Result().Body, es)) {
160+
should.NotNil("/test1", es.Items[0].Path)
161+
should.NotNil("/test2", es.Items[1].Path)
162+
should.NotNil("/test3", es.Items[2].Path)
163+
}
164+
}

0 commit comments

Comments
 (0)