Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
cookie utility methods use multiline strings instead of arrays
  • Loading branch information
rtomayko committed Mar 9, 2010
1 parent 148e544 commit c028a23
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 15 additions & 8 deletions lib/rack/utils.rb
Expand Up @@ -193,27 +193,34 @@ def set_cookie_header!(header, key, value)
"#{domain}#{path}#{expires}#{secure}#{httponly}"

case header["Set-Cookie"]
when Array
header["Set-Cookie"] << cookie
when String
header["Set-Cookie"] = [header["Set-Cookie"], cookie]
when nil
when nil, ''
header["Set-Cookie"] = cookie
when String
header["Set-Cookie"] = [header["Set-Cookie"], cookie].join("\n")
when Array
header["Set-Cookie"] = (header["Set-Cookie"] + [cookie]).join("\n")
end

nil
end
module_function :set_cookie_header!

def delete_cookie_header!(header, key, value = {})
unless Array === header["Set-Cookie"]
header["Set-Cookie"] = [header["Set-Cookie"]].compact
case header["Set-Cookie"]
when nil, ''
cookies = []
when String
cookies = header["Set-Cookie"].split("\n")
when Array
cookies = header["Set-Cookie"]
end

header["Set-Cookie"].reject! { |cookie|
cookies.reject! { |cookie|
cookie =~ /\A#{escape(key)}=/
}

header["Set-Cookie"] = cookies.join("\n")

set_cookie_header!(header, key,
{:value => '', :path => nil, :domain => nil,
:expires => Time.at(0) }.merge(value))
Expand Down
10 changes: 6 additions & 4 deletions test/spec_rack_response.rb
Expand Up @@ -50,9 +50,9 @@
response.set_cookie "foo", "bar"
response["Set-Cookie"].should.equal "foo=bar"
response.set_cookie "foo2", "bar2"
response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2"]
response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2"].join("\n")
response.set_cookie "foo3", "bar3"
response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2", "foo3=bar3"]
response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2", "foo3=bar3"].join("\n")
end

specify "formats the Cookie expiration date accordingly to RFC 2109" do
Expand Down Expand Up @@ -80,8 +80,10 @@
response.set_cookie "foo", "bar"
response.set_cookie "foo2", "bar2"
response.delete_cookie "foo"
response["Set-Cookie"].should.equal ["foo2=bar2",
"foo=; expires=Thu, 01-Jan-1970 00:00:00 GMT"]
response["Set-Cookie"].should.equal [
"foo2=bar2",
"foo=; expires=Thu, 01-Jan-1970 00:00:00 GMT"
].join("\n")
end

specify "can do redirects" do
Expand Down

2 comments on commit c028a23

@josh
Copy link
Contributor

@josh josh commented on c028a23 Mar 9, 2010

Choose a reason for hiding this comment

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

thanks, this was annoying me for a while

@rtomayko
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be great if we could remove all of the Array handling from HeaderHash at some point too. I assume we have it there so that we don't break apps but the SPEC is clear on what's allowed in header values at this point.

Please sign in to comment.