Removeall #18

Merged
merged 1 commit into from Oct 7, 2016
Jump to file or symbol
Failed to load files and symbols.
+107 −0
Split
View
26 jar.go
@@ -396,6 +396,32 @@ func (j *Jar) deleteExpired(now time.Time) {
}
}
+// RemoveAllHost removes any cookies from the jar that were set for the given host.
+func (j *Jar) RemoveAllHost(host string) {
+ host, err := canonicalHost(host)
+ if err != nil {
+ return
+ }
+ key := jarKey(host, j.psList)
+
+ j.mu.Lock()
+ defer j.mu.Unlock()
+
+ expired := time.Now().Add(-1 * time.Second)
+ submap := j.entries[key]
+ for id, e := range submap {
+ if e.CanonicalHost == host {
+ // Save some space by deleting the value when the cookie
+ // expires. We can't delete the cookie itself because then
+ // we wouldn't know that the cookie had expired when
+ // we merge with another cookie jar.
+ e.Value = ""
+ e.Expires = expired
+ submap[id] = e
+ }
+ }
+}
+
// SetCookies implements the SetCookies method of the http.CookieJar interface.
//
// It does nothing if the URL's scheme is not HTTP or HTTPS.
View
@@ -1964,6 +1964,87 @@ func TestRemoveCookies(t *testing.T) {
}
}
+func TestRemoveAll(t *testing.T) {
+ testRemoveAll(t, mustParseURL("https://www.apple.com"), "www.apple.com", true)
+}
+
+func TestRemoveAllRoot(t *testing.T) {
+ testRemoveAll(t, mustParseURL("https://www.apple.com"), "apple.com", false)
+}
+
+func TestRemoveAllDifferent(t *testing.T) {
+ testRemoveAll(t, mustParseURL("https://www.apple.com"), "foo.apple.com", false)
+}
+
+func TestRemoveAllWithPort(t *testing.T) {
+ testRemoveAll(t, mustParseURL("https://www.apple.com"), "www.apple.com:80", true)
+}
+
+func TestRemoveAllIP(t *testing.T) {
+ testRemoveAll(t, mustParseURL("https://10.1.1.1"), "10.1.1.1", true)
+}
+
+func testRemoveAll(t *testing.T, setURL *url.URL, removeHost string, shouldRemove bool) {
+ jar := newTestJar("")
+ google := mustParseURL("https://www.google.com")
+ jar.SetCookies(
+ google,
+ []*http.Cookie{
+ &http.Cookie{
+ Name: "test-cookie",
+ Value: "test-value",
+ Expires: time.Now().Add(24 * time.Hour),
+ },
+ &http.Cookie{
+ Name: "test-cookie2",
+ Value: "test-value",
+ Expires: time.Now().Add(24 * time.Hour),
+ },
+ },
+ )
+ onlyGoogle := jar.AllCookies()
+ if len(onlyGoogle) != 2 {
+ t.Fatalf("Expected 2 cookies, got %d", len(onlyGoogle))
+ }
+
+ jar.SetCookies(
+ setURL,
+ []*http.Cookie{
+ &http.Cookie{
+ Name: "test-cookie3",
+ Value: "test-value",
+ Expires: time.Now().Add(24 * time.Hour),
+ },
+ &http.Cookie{
+ Name: "test-cookie4",
+ Value: "test-value",
+ Expires: time.Now().Add(24 * time.Hour),
+ },
+ },
+ )
+ withSet := jar.AllCookies()
+ if len(withSet) != 4 {
+ t.Fatalf("Expected 4 cookies, got %d", len(withSet))
+ }
+ jar.RemoveAllHost(removeHost)
+ after := jar.AllCookies()
+ if !shouldRemove {
+ if len(after) != len(withSet) {
+ t.Fatalf("Expected %d cookies, got %d", len(withSet), len(after))
+ }
+ return
+ }
+ if len(after) != len(onlyGoogle) {
+ t.Fatalf("Expected %d cookies, got %d", len(onlyGoogle), len(after))
+ }
+ if !cookiesEqual(onlyGoogle[0], after[0]) {
+ t.Fatalf("Expected %v, got %v", onlyGoogle[0], after[0])
+ }
+ if !cookiesEqual(onlyGoogle[1], after[1]) {
+ t.Fatalf("Expected %v, got %v", onlyGoogle[1], after[1])
+ }
+}
+
func cookiesEqual(a, b *http.Cookie) bool {
return a.Name == b.Name &&
a.Value == b.Value &&