From f5b1fb44bb819bd8059a84998c3a902bd01d92b3 Mon Sep 17 00:00:00 2001 From: Damon Zhao Date: Thu, 27 Aug 2015 16:04:50 +0800 Subject: [PATCH 1/4] Add SetCookie and GetCookie method for Context --- context.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ context_test.go | 17 ++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/context.go b/context.go index 20a20e3..c4fb96f 100644 --- a/context.go +++ b/context.go @@ -9,6 +9,7 @@ import ( "io" "math" "net/http" + "net/url" "strings" "time" @@ -321,6 +322,64 @@ func (c *Context) Header(key, value string) { } } +func (c *Context) SetCookie(name string, value string, options ...interface{}) { + cookie := http.Cookie{} + cookie.Name = name + cookie.Value = url.QueryEscape(value) + + if len(options) > 0 { + switch v := options[0].(type) { + case int: + cookie.MaxAge = v + case int64: + cookie.MaxAge = int(v) + case int32: + cookie.MaxAge = int(v) + } + } + + cookie.Path = "/" + if len(options) > 1 { + if v, ok := options[1].(string); ok && len(v) > 0 { + cookie.Path = v + } + } + + if len(options) > 2 { + if v, ok := options[2].(string); ok && len(v) > 0 { + cookie.Domain = v + } + } + + if len(options) > 3 { + switch v := options[3].(type) { + case bool: + cookie.Secure = v + default: + if options[3] != nil { + cookie.Secure = true + } + } + } + + if len(options) > 4 { + if v, ok := options[4].(bool); ok && v { + cookie.HttpOnly = true + } + } + + c.Writer.Header().Add("Set-Cookie", cookie.String()) +} + +func (c *Context) GetCookie(name string) string { + cookie, err := c.Request.Cookie(name) + if err != nil { + return "" + } + val, _ := url.QueryUnescape(cookie.Value) + return val +} + func (c *Context) Render(code int, r render.Render) { c.writermem.WriteHeader(code) if err := r.Render(c.Writer); err != nil { diff --git a/context_test.go b/context_test.go index d95125a..5697a04 100644 --- a/context_test.go +++ b/context_test.go @@ -246,6 +246,23 @@ func TestContextPostFormMultipart(t *testing.T) { assert.Equal(t, c.PostForm("bar"), "foo") } +func TestContextSetCookie(t *testing.T) { + c, w, _ := createTestContext() + c.SetCookie("user", "gin", 1, "/", "localhost", true, true) + c.SetCookie("user", "gin", int32(1), "/", "localhost", 1) + c.SetCookie("user", "gin", int64(1)) + + c.Request, _ = http.NewRequest("GET", "/set", nil) + assert.Equal(t, c.GetCookie("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure") +} + +func TestContextGetCookie(t *testing.T) { + c, w, _ := createTestContext() + c.Request, _ = http.NewRequest("GET", "/get", nil) + c.Request.Header.Set("Cookie", "user=gin") + assert.Equal(t, w.Body.String(), "gin") +} + // Tests that the response is serialized as JSON // and Content-Type is set to application/json func TestContextRenderJSON(t *testing.T) { From 7bf97883262733f7534fce486727ed6cea1e86bb Mon Sep 17 00:00:00 2001 From: Damon Zhao Date: Thu, 27 Aug 2015 16:16:16 +0800 Subject: [PATCH 2/4] fix ci --- context_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 5697a04..cb704bb 100644 --- a/context_test.go +++ b/context_test.go @@ -260,7 +260,7 @@ func TestContextGetCookie(t *testing.T) { c, w, _ := createTestContext() c.Request, _ = http.NewRequest("GET", "/get", nil) c.Request.Header.Set("Cookie", "user=gin") - assert.Equal(t, w.Body.String(), "gin") + assert.Equal(t, c.GetCookie("Cookie"), "gin") } // Tests that the response is serialized as JSON From e1d27558b3b1aa95a864036b1960fe2c267fb3ea Mon Sep 17 00:00:00 2001 From: Damon Zhao Date: Tue, 8 Sep 2015 15:37:20 +0800 Subject: [PATCH 3/4] add parameters for set cookie --- context.go | 52 +++++++++++++++------------------------------------- 1 file changed, 15 insertions(+), 37 deletions(-) diff --git a/context.go b/context.go index c4fb96f..37b1100 100644 --- a/context.go +++ b/context.go @@ -322,51 +322,29 @@ func (c *Context) Header(key, value string) { } } -func (c *Context) SetCookie(name string, value string, options ...interface{}) { +func (c *Context) SetCookie( + name string, + value string, + maxAge int, + path string, + domain string, + secure bool, + httpOnly bool, +) { cookie := http.Cookie{} cookie.Name = name cookie.Value = url.QueryEscape(value) - if len(options) > 0 { - switch v := options[0].(type) { - case int: - cookie.MaxAge = v - case int64: - cookie.MaxAge = int(v) - case int32: - cookie.MaxAge = int(v) - } - } + cookie.MaxAge = maxAge cookie.Path = "/" - if len(options) > 1 { - if v, ok := options[1].(string); ok && len(v) > 0 { - cookie.Path = v - } + if path != "" { + cookie.Path = path } - if len(options) > 2 { - if v, ok := options[2].(string); ok && len(v) > 0 { - cookie.Domain = v - } - } - - if len(options) > 3 { - switch v := options[3].(type) { - case bool: - cookie.Secure = v - default: - if options[3] != nil { - cookie.Secure = true - } - } - } - - if len(options) > 4 { - if v, ok := options[4].(bool); ok && v { - cookie.HttpOnly = true - } - } + cookie.Domain = domain + cookie.Secure = secure + cookie.HttpOnly = httpOnly c.Writer.Header().Add("Set-Cookie", cookie.String()) } From 8e37eb84984714af6cea105d1885798b70025444 Mon Sep 17 00:00:00 2001 From: Javier Provecho Fernandez Date: Fri, 2 Oct 2015 12:37:51 +0200 Subject: [PATCH 4/4] Fix tests for GetCookie() and SetCookie() --- context.go | 6 +++--- context_test.go | 12 +++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/context.go b/context.go index 4bf7986..a636f7a 100644 --- a/context.go +++ b/context.go @@ -349,13 +349,13 @@ func (c *Context) SetCookie( c.Writer.Header().Add("Set-Cookie", cookie.String()) } -func (c *Context) GetCookie(name string) string { +func (c *Context) GetCookie(name string) (string, error) { cookie, err := c.Request.Cookie(name) if err != nil { - return "" + return "", err } val, _ := url.QueryUnescape(cookie.Value) - return val + return val, nil } func (c *Context) Render(code int, r render.Render) { diff --git a/context_test.go b/context_test.go index 18e5460..c6bb591 100644 --- a/context_test.go +++ b/context_test.go @@ -248,20 +248,18 @@ func TestContextPostFormMultipart(t *testing.T) { } func TestContextSetCookie(t *testing.T) { - c, w, _ := createTestContext() + c, _, _ := createTestContext() c.SetCookie("user", "gin", 1, "/", "localhost", true, true) - c.SetCookie("user", "gin", int32(1), "/", "localhost", 1) - c.SetCookie("user", "gin", int64(1)) - c.Request, _ = http.NewRequest("GET", "/set", nil) - assert.Equal(t, c.GetCookie("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure") + assert.Equal(t, c.Writer.Header().Get("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure") } func TestContextGetCookie(t *testing.T) { - c, w, _ := createTestContext() + c, _, _ := createTestContext() c.Request, _ = http.NewRequest("GET", "/get", nil) c.Request.Header.Set("Cookie", "user=gin") - assert.Equal(t, c.GetCookie("Cookie"), "gin") + cookie, _ := c.GetCookie("user") + assert.Equal(t, cookie, "gin") } // Tests that the response is serialized as JSON