From 92475baba6753c6f41e44794bcfa89fcbd055b05 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Thu, 4 Jun 2015 04:50:40 +0200 Subject: [PATCH] Adds more units tests --- routergroup.go | 4 ++++ routergroup_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/routergroup.go b/routergroup.go index ff6b40b..d429312 100644 --- a/routergroup.go +++ b/routergroup.go @@ -7,6 +7,7 @@ package gin import ( "net/http" "path" + "regexp" "strings" ) @@ -50,6 +51,9 @@ func (group *RouterGroup) handle(httpMethod, relativePath string, handlers Handl } func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) { + if matches, err := regexp.MatchString("^[A-Z]+$", httpMethod); !matches || err != nil { + panic("http method " + httpMethod + " is not valid") + } group.handle(httpMethod, relativePath, handlers) } diff --git a/routergroup_test.go b/routergroup_test.go index f46b51b..1a6b0cb 100644 --- a/routergroup_test.go +++ b/routergroup_test.go @@ -123,3 +123,28 @@ func TestRouterGroupTooManyHandlers(t *testing.T) { router.GET("/", handlers2...) }) } + +func TestRouterGroupBadMethod(t *testing.T) { + router := New() + assert.Panics(t, func() { + router.Handle("get", "/") + }) + assert.Panics(t, func() { + router.Handle(" GET", "/") + }) + assert.Panics(t, func() { + router.Handle("GET ", "/") + }) + assert.Panics(t, func() { + router.Handle("", "/") + }) + assert.Panics(t, func() { + router.Handle("PO ST", "/") + }) + assert.Panics(t, func() { + router.Handle("1GET", "/") + }) + assert.Panics(t, func() { + router.Handle("PATCh", "/") + }) +}