From ad2dacedd654cdc1c1526971fe83c118318975ba Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 24 Mar 2017 20:43:23 +0800 Subject: [PATCH] feat: Support get value from request header. (#839) --- context.go | 5 +++++ context_test.go | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/context.go b/context.go index b4f23952..f638e623 100644 --- a/context.go +++ b/context.go @@ -440,6 +440,11 @@ func (c *Context) Header(key, value string) { } } +// GetHeader returns value from request headers +func (c *Context) GetHeader(key string) string { + return c.requestHeader(key) +} + func (c *Context) SetCookie( name string, value string, diff --git a/context_test.go b/context_test.go index ebc5050e..52606281 100644 --- a/context_test.go +++ b/context_test.go @@ -1007,3 +1007,12 @@ func TestWebsocketsRequired(t *testing.T) { assert.False(t, c.IsWebsocket()) } + +func TestGetRequestHeaderValue(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest("GET", "/chat", nil) + c.Request.Header.Set("Gin-Version", "1.0.0") + + assert.Equal(t, "1.0.0", c.GetHeader("Gin-Version")) + assert.Equal(t, "", c.GetHeader("Connection")) +}