mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
feat(context): add DeleteCookie and DeleteCookieData helpers
This commit is contained in:
parent
43fe48e8a0
commit
692cfcf41b
57
context.go
57
context.go
@ -1197,6 +1197,63 @@ func (c *Context) Cookie(name string) (string, error) {
|
||||
return val, nil
|
||||
}
|
||||
|
||||
// DeleteCookie expires the named cookie so that the client removes it. HTTP
|
||||
// has no dedicated delete operation; a cookie is removed by sending a
|
||||
// Set-Cookie header with the same name, path and domain and an already
|
||||
// expired date.
|
||||
//
|
||||
// The path defaults to "/" when empty. An empty domain targets a host-only
|
||||
// cookie, while a non-empty domain targets a domain-scoped cookie; callers
|
||||
// must pass the same path and domain used when the cookie was originally
|
||||
// set.
|
||||
//
|
||||
// SameSite is inherited from Context.SetSameSite. Secure is set
|
||||
// automatically when SameSite is None or when the name uses the __Secure-
|
||||
// or __Host- prefix, since browsers reject those cookies otherwise.
|
||||
// Set-Cookie headers are only honored before the response is written, so
|
||||
// DeleteCookie must be called before the handler writes the status or body.
|
||||
func (c *Context) DeleteCookie(name, path, domain string) {
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
cookie := &http.Cookie{
|
||||
Name: name,
|
||||
Path: path,
|
||||
Domain: domain,
|
||||
MaxAge: -1,
|
||||
Expires: time.Unix(1, 0),
|
||||
SameSite: c.sameSite,
|
||||
}
|
||||
|
||||
if c.sameSite == http.SameSiteNoneMode ||
|
||||
strings.HasPrefix(name, "__Secure-") ||
|
||||
strings.HasPrefix(name, "__Host-") {
|
||||
cookie.Secure = true
|
||||
}
|
||||
|
||||
http.SetCookie(c.Writer, cookie)
|
||||
}
|
||||
|
||||
// DeleteCookieData is the *http.Cookie counterpart of DeleteCookie. It
|
||||
// allows callers to control attributes such as SameSite, Secure or
|
||||
// Partitioned while Value, MaxAge and Expires are overwritten so the
|
||||
// cookie expires immediately. The provided cookie must have a valid Name.
|
||||
func (c *Context) DeleteCookieData(cookie *http.Cookie) {
|
||||
if cookie.Path == "" {
|
||||
cookie.Path = "/"
|
||||
}
|
||||
|
||||
if cookie.SameSite == http.SameSiteDefaultMode {
|
||||
cookie.SameSite = c.sameSite
|
||||
}
|
||||
|
||||
cookie.Value = ""
|
||||
cookie.MaxAge = -1
|
||||
cookie.Expires = time.Unix(1, 0)
|
||||
http.SetCookie(c.Writer, cookie)
|
||||
}
|
||||
|
||||
// Render writes the response headers and calls render.Render to render data.
|
||||
func (c *Context) Render(code int, r render.Render) {
|
||||
c.Status(code)
|
||||
|
||||
203
context_test.go
203
context_test.go
@ -16,6 +16,7 @@ import (
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -3840,6 +3841,208 @@ func TestContextSetCookieData(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestContextDeleteCookie(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.DeleteCookie("user", "/", "localhost")
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "user=")
|
||||
assert.Contains(t, setCookie, "Path=/")
|
||||
assert.Contains(t, setCookie, "Domain=localhost")
|
||||
assert.Contains(t, setCookie, "Max-Age=0")
|
||||
assert.Contains(t, setCookie, "Expires=")
|
||||
}
|
||||
|
||||
func TestContextDeleteCookiePathEmpty(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.DeleteCookie("user", "", "")
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "Path=/")
|
||||
assert.NotContains(t, setCookie, "Domain=")
|
||||
assert.Contains(t, setCookie, "Max-Age=0")
|
||||
}
|
||||
|
||||
func TestContextDeleteCookieSameSite(t *testing.T) {
|
||||
t.Run("SameSite is inherited from the context", func(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.SetSameSite(http.SameSiteStrictMode)
|
||||
c.DeleteCookie("user", "/", "")
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "SameSite=Strict")
|
||||
})
|
||||
|
||||
t.Run("SameSite=None sets Secure", func(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.SetSameSite(http.SameSiteNoneMode)
|
||||
c.DeleteCookie("user", "/", "")
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "SameSite=None")
|
||||
assert.Contains(t, setCookie, "Secure")
|
||||
})
|
||||
}
|
||||
|
||||
func TestContextDeleteCookieSecurePrefixes(t *testing.T) {
|
||||
for _, name := range []string{"__Secure-user", "__Host-user"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.DeleteCookie(name, "/", "")
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, name+"=")
|
||||
assert.Contains(t, setCookie, "Secure")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextDeleteCookieData(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
cookie := &http.Cookie{
|
||||
Name: "user",
|
||||
Value: "gin",
|
||||
Path: "/",
|
||||
Domain: "localhost",
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
}
|
||||
|
||||
c.DeleteCookieData(cookie)
|
||||
|
||||
// The passed cookie is expired and cleared in place.
|
||||
assert.Empty(t, cookie.Value)
|
||||
assert.Equal(t, -1, cookie.MaxAge)
|
||||
assert.False(t, cookie.Expires.IsZero())
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "user=")
|
||||
assert.NotContains(t, setCookie, "gin")
|
||||
assert.Contains(t, setCookie, "Path=/")
|
||||
assert.Contains(t, setCookie, "Domain=localhost")
|
||||
assert.Contains(t, setCookie, "Max-Age=0")
|
||||
assert.Contains(t, setCookie, "Expires=")
|
||||
assert.Contains(t, setCookie, "HttpOnly")
|
||||
assert.Contains(t, setCookie, "Secure")
|
||||
}
|
||||
|
||||
func TestContextDeleteCookieDataPathEmpty(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.DeleteCookieData(&http.Cookie{
|
||||
Name: "user",
|
||||
Value: "gin",
|
||||
})
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "Path=/")
|
||||
assert.NotContains(t, setCookie, "Domain=")
|
||||
assert.Contains(t, setCookie, "Max-Age=0")
|
||||
}
|
||||
|
||||
func TestContextDeleteCookieDataSameSite(t *testing.T) {
|
||||
t.Run("SameSiteDefaultMode inherits context sameSite", func(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.SetSameSite(http.SameSiteStrictMode)
|
||||
c.DeleteCookieData(&http.Cookie{
|
||||
Name: "user",
|
||||
Value: "gin",
|
||||
Path: "/",
|
||||
SameSite: http.SameSiteDefaultMode,
|
||||
})
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "SameSite=Strict")
|
||||
})
|
||||
|
||||
t.Run("explicit SameSite is preserved", func(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.SetSameSite(http.SameSiteStrictMode)
|
||||
c.DeleteCookieData(&http.Cookie{
|
||||
Name: "user",
|
||||
Value: "gin",
|
||||
Path: "/",
|
||||
SameSite: http.SameSiteNoneMode,
|
||||
Secure: true,
|
||||
})
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "SameSite=None")
|
||||
assert.Contains(t, setCookie, "Secure")
|
||||
})
|
||||
}
|
||||
|
||||
func TestContextDeleteCookieDataPreservesAttributes(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.DeleteCookieData(&http.Cookie{
|
||||
Name: "user",
|
||||
Value: "gin",
|
||||
Path: "/app",
|
||||
Domain: "localhost",
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
Partitioned: true,
|
||||
})
|
||||
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "Path=/app")
|
||||
assert.Contains(t, setCookie, "Domain=localhost")
|
||||
assert.Contains(t, setCookie, "HttpOnly")
|
||||
assert.Contains(t, setCookie, "Secure")
|
||||
assert.Contains(t, setCookie, "Partitioned")
|
||||
}
|
||||
|
||||
func TestContextDeleteCookieEndToEnd(t *testing.T) {
|
||||
router := New()
|
||||
router.GET("/set", func(c *Context) {
|
||||
c.SetCookieData(&http.Cookie{
|
||||
Name: "user",
|
||||
Value: "gin",
|
||||
Path: "/",
|
||||
MaxAge: 3600,
|
||||
HttpOnly: true,
|
||||
})
|
||||
})
|
||||
router.GET("/delete", func(c *Context) {
|
||||
c.DeleteCookie("user", "/", "")
|
||||
})
|
||||
router.GET("/read", func(c *Context) {
|
||||
if _, err := c.Cookie("user"); err != nil {
|
||||
c.String(http.StatusOK, "missing")
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, "present")
|
||||
})
|
||||
|
||||
ts := httptest.NewServer(router)
|
||||
defer ts.Close()
|
||||
|
||||
jar, err := cookiejar.New(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
u, err := url.Parse(ts.URL)
|
||||
require.NoError(t, err)
|
||||
|
||||
client := ts.Client()
|
||||
client.Jar = jar
|
||||
|
||||
resp, err := client.Get(ts.URL + "/set")
|
||||
require.NoError(t, err)
|
||||
resp.Body.Close()
|
||||
require.Len(t, jar.Cookies(u), 1)
|
||||
|
||||
resp, err = client.Get(ts.URL + "/delete")
|
||||
require.NoError(t, err)
|
||||
resp.Body.Close()
|
||||
assert.Empty(t, jar.Cookies(u))
|
||||
|
||||
resp, err = client.Get(ts.URL + "/read")
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "missing", string(body))
|
||||
}
|
||||
|
||||
func TestGetMapFromFormData(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user