From 30007af6a3414816268876bfba2bfab5c8c97cbd Mon Sep 17 00:00:00 2001 From: Nagy Salem Date: Tue, 2 Jun 2020 18:56:56 +0300 Subject: [PATCH] Fixes #25 Added custom methods (#30) --- gearbox.go | 6 ++++++ gearbox_test.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/gearbox.go b/gearbox.go index 4989553..95f7da6 100644 --- a/gearbox.go +++ b/gearbox.go @@ -121,6 +121,7 @@ type Gearbox interface { Connect(path string, handlers ...handlerFunc) error Options(path string, handlers ...handlerFunc) error Trace(path string, handlers ...handlerFunc) error + Method(method, path string, handlers ...handlerFunc) error Fallback(handlers ...handlerFunc) error Use(middlewares ...handlerFunc) } @@ -232,6 +233,11 @@ func (gb *gearbox) Trace(path string, handlers ...handlerFunc) error { return gb.registerRoute([]byte(MethodTrace), []byte(path), handlers) } +// Trace registers an http relevant method +func (gb *gearbox) Method(method, path string, handlers ...handlerFunc) error { + return gb.registerRoute([]byte(method), []byte(path), handlers) +} + // Fallback registers an http handler only fired when no other routes match with request func (gb *gearbox) Fallback(handlers ...handlerFunc) error { return gb.registerFallback(handlers) diff --git a/gearbox_test.go b/gearbox_test.go index 50193c1..98c9a60 100644 --- a/gearbox_test.go +++ b/gearbox_test.go @@ -125,6 +125,8 @@ func registerRoute(gb Gearbox, method, path string, handler func(ctx *Context)) gb.Options(path, handler) case MethodTrace: gb.Trace(path, handler) + default: + gb.Method(method, path, handler) } } @@ -149,6 +151,7 @@ func TestMethods(t *testing.T) { {method: MethodOptions, path: "/user/204/setting", handler: emptyHandler}, {method: MethodTrace, path: "/users/*", handler: emptyHandler}, {method: MethodTrace, path: "/users/test", handler: emptyHandler}, + {method: "CUSTOM", path: "/users/test/private", handler: emptyHandler}, } // get instance of gearbox @@ -184,6 +187,7 @@ func TestMethods(t *testing.T) { {method: MethodConnect, path: "/user/204", statusCode: StatusOK}, {method: MethodOptions, path: "/user/204/setting", statusCode: StatusOK}, {method: MethodTrace, path: "/users/testing", statusCode: StatusOK}, + {method: "CUSTOM", path: "/users/test/private", statusCode: StatusOK}, } for _, tc := range testCases {