Skip to content

Commit

Permalink
Previously peridot would delete all cookies whenever the "Set-Cookie"
Browse files Browse the repository at this point in the history
header received by get in merge-cookies was an empty list, which would
normally be the case for code like this:

    (-> (session app)
      (request "/foo")
      (request "/bar"))

Before these changes, cookies established by /foo would be missing for
/bar.

Update the tests to expire cookies instead of just dropping them on
"/delete".

Via Rob Browning <rlb@defaultvalue.org>
  • Loading branch information
xeqi committed Sep 7, 2014
1 parent 6459a0b commit c2ca8c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/peridot/cookie_jar.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@

(defn merge-cookies [headers cookie-jar uri host]
(let [cookie-string (get headers "Set-Cookie")]
(if cookie-string
(if (empty? cookie-string)
(dissoc cookie-jar host)
(update-in cookie-jar [host] merge
(into {} (map #(build-cookie % uri host) cookie-string))))
cookie-jar)))
(if (empty? cookie-string)
cookie-jar
(update-in cookie-jar [host] merge
(into {} (map #(build-cookie % uri host) cookie-string))))))

(defn cookies-for [cookie-jar scheme uri host]
(let [cookie-string
Expand All @@ -86,4 +84,4 @@
(interpose ";")
(apply str))]
(when (not (empty? cookie-string))
{"Cookie" cookie-string})))
{"Cookie" cookie-string})))
11 changes: 9 additions & 2 deletions test/peridot/test/cookie_jar.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
{k (merge {:value v} (f k v))})
(seq m))))

(def expired-date
(.format (first cj/cookie-date-formats) (java.util.Date. 0)))

(defn expire-cookie [m]
(assoc m :expires expired-date :value ""))

(def app
(params/wrap-params
(cookies/wrap-cookies
Expand All @@ -42,7 +48,8 @@
["delete"]
{:get (fn [req]
(assoc (response/response "ok")
:cookies {}))}
:cookies (into {} (for [[k v] (:cookies req)]
[k (expire-cookie v)]))))}
["set-secure"]
{:get (fn [req]
(assoc (response/response "ok")
Expand Down Expand Up @@ -230,4 +237,4 @@
(doto
(#(is (= (get (:headers (:request %)) "cookie")
"value=1")
"http-only cookies are sent")))))
"http-only cookies are sent")))))

3 comments on commit c2ca8c1

@jayp
Copy link

@jayp jayp commented on c2ca8c1 Nov 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @xeqi, great work. Any timeline on when you'll be publishing this fix to clojars?

@glenjamin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just pushed out peridot 0.3.1 which contains this fix

@jayp
Copy link

@jayp jayp commented on c2ca8c1 Nov 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @glenjamin. Double thanks for also pushing newer release of kerodon.

Please sign in to comment.