From 3f95933c3d8ab010c1454b35b6dd8b534948f928 Mon Sep 17 00:00:00 2001 From: collinmsn <4130944@qq.com> Date: Fri, 2 Jun 2017 16:00:55 +0800 Subject: [PATCH] Add method to return main handler (#930) Fix #928 Method to get main handler is desired --- context.go | 5 +++++ context_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/context.go b/context.go index 16cffec..971675d 100644 --- a/context.go +++ b/context.go @@ -85,6 +85,11 @@ func (c *Context) HandlerName() string { return nameOfFunction(c.handlers.Last()) } +// Handler returns the main handler. +func (c *Context) Handler() HandlerFunc { + return c.handlers.Last() +} + /************************************/ /*********** FLOW CONTROL ***********/ /************************************/ diff --git a/context_test.go b/context_test.go index 7923a4b..66a55cd 100644 --- a/context_test.go +++ b/context_test.go @@ -12,6 +12,7 @@ import ( "mime/multipart" "net/http" "net/http/httptest" + "reflect" "strings" "testing" "time" @@ -277,6 +278,18 @@ func handlerNameTest(c *Context) { } +var handlerTest HandlerFunc = func(c *Context) { + +} + +func TestContextHandler(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.handlers = HandlersChain{func(c *Context) {}, handlerTest} + + assert.Equal(t, reflect.ValueOf(handlerTest).Pointer(), reflect.ValueOf(c.Handler()).Pointer()) +} + + func TestContextQuery(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil)