diff --git a/context.go b/context.go index baa4b0f9..3592c76e 100644 --- a/context.go +++ b/context.go @@ -946,7 +946,7 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, } http.SetCookie(c.Writer, &http.Cookie{ Name: name, - Value: url.QueryEscape(value), + Value: value, MaxAge: maxAge, Path: path, Domain: domain, @@ -965,8 +965,7 @@ func (c *Context) Cookie(name string) (string, error) { if err != nil { return "", err } - val, _ := url.QueryUnescape(cookie.Value) - return val, nil + return cookie.Value, nil } // Render writes the response headers and calls render.Render to render data. diff --git a/context_test.go b/context_test.go index 8bbf2700..899383c9 100644 --- a/context_test.go +++ b/context_test.go @@ -685,6 +685,13 @@ func TestContextSetCookiePathEmpty(t *testing.T) { assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", c.Writer.Header().Get("Set-Cookie")) } +func TestContextSetCookieWithSpace(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.SetSameSite(http.SameSiteLaxMode) + c.SetCookie("user", "gin test", 1, "/", "localhost", true, true) + assert.Equal(t, "user=\"gin test\"; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", c.Writer.Header().Get("Set-Cookie")) +} + func TestContextGetCookie(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("GET", "/get", nil)