diff --git a/ginmill.go b/ginmill.go index b18b555..d15f2fc 100644 --- a/ginmill.go +++ b/ginmill.go @@ -29,6 +29,14 @@ func NewFeatures(routes gin.RoutesInfo) *Features { return f } +// GetRoutes is a getter for RoutesInfo +func (f *Features) GetRoutes() gin.RoutesInfo { + r := make(gin.RoutesInfo, len(f.routes)) + copy(r, f.routes) + return r + +} + // With pre-defined Features for ginmill func (s *Server) With(f *Features) *Server { for _, r := range f.routes { diff --git a/ginmill_test.go b/ginmill_test.go index 9c21026..c629603 100644 --- a/ginmill_test.go +++ b/ginmill_test.go @@ -77,6 +77,28 @@ func TestNewFeatures(t *testing.T) { } } +func TestFeatures_GetRoutes(t *testing.T) { + r := routes() + f := NewFeatures(r) + got := f.GetRoutes() + + if len(got) != len(f.routes) { + t.Errorf("GetRoutes() length = %d, want %d", len(got), len(f.routes)) + } + + for i := range got { + if got[i].Method != f.routes[i].Method || got[i].Path != f.routes[i].Path { + t.Errorf("GetRoutes() element %d not equal", i) + } + } + + if len(got) > 0 { + got[0].Path = "/changed" + if got[0].Path == f.routes[0].Path { + t.Errorf("GetRoutes should return a copy, not a reference") + } + } +} func TestServer_With(t *testing.T) { type args struct {