Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Add Cookies.Delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikcreemers authored and markbates committed Jul 29, 2017
1 parent f7372d5 commit 1684e47
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ func (c *Cookies) SetWithExpirationTime(name, value string, expires time.Time) {

http.SetCookie(c.res, &ck)
}

// Delete sets a header that tells the browser to remove the cookie with the given name.
func (c *Cookies) Delete(name string) {
ck := http.Cookie{
Name: name,
Value: "v",
// Setting a time in the distant past, like the unix epoch, removes the cookie,
// since it has long expired.
Expires: time.Unix(0, 0),
}

http.SetCookie(c.res, &ck)
}
12 changes: 12 additions & 0 deletions cookies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ func TestCookies_SetWithExpirationTime(t *testing.T) {
h := res.Header().Get("Set-Cookie")
r.Equal("name=Rob Pike; Expires=Sat, 29 Jul 2017 19:28:45 GMT", h)
}

func TestCookies_Delete(t *testing.T) {
r := require.New(t)
res := httptest.NewRecorder()

c := Cookies{&http.Request{}, res}

c.Delete("remove-me")

h := res.Header().Get("Set-Cookie")
r.Equal("remove-me=v; Expires=Thu, 01 Jan 1970 00:00:00 GMT", h)
}

0 comments on commit 1684e47

Please sign in to comment.