File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ func newEntrySet() *entrySet {
2323
2424// EntrySet 路由集合
2525type 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 显示理由条目
4345func (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
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments