Adds more units tests

This commit is contained in:
Manu Mtz-Almeida 2015-06-04 04:50:40 +02:00
parent ee021d06ea
commit 92475baba6
2 changed files with 29 additions and 0 deletions

View File

@ -7,6 +7,7 @@ package gin
import ( import (
"net/http" "net/http"
"path" "path"
"regexp"
"strings" "strings"
) )
@ -50,6 +51,9 @@ func (group *RouterGroup) handle(httpMethod, relativePath string, handlers Handl
} }
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) { 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) group.handle(httpMethod, relativePath, handlers)
} }

View File

@ -123,3 +123,28 @@ func TestRouterGroupTooManyHandlers(t *testing.T) {
router.GET("/", handlers2...) 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", "/")
})
}