From 28458101b69c31568a82885cfa26b709e996d9a6 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Mon, 15 Apr 2013 08:54:09 +0900 Subject: [PATCH] HTTP::Cookie.set_cookie_value: Don't take an origin argument. --- lib/http/cookie.rb | 28 ++++++++++++++++------------ test/test_http_cookie.rb | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/http/cookie.rb b/lib/http/cookie.rb index 759e910..6f56acd 100644 --- a/lib/http/cookie.rb +++ b/lib/http/cookie.rb @@ -593,21 +593,25 @@ class HTTP::Cookie end alias to_s cookie_value - # Returns a string for use in the Set-Cookie header. If the cookie - # does not have an origin set, one must be given from the argument. - # - # This method does not check if this cookie will be accepted from - # the origin. - def set_cookie_value(origin = nil) - origin = origin ? URI(origin) : @origin or - raise "origin must be specified to produce a value for Set-Cookie" - + # Returns a string for use in the Set-Cookie header. If necessary + # information like a path or domain (when `for_domain` is set) is + # missing, RuntimeError is raised. It is always the best to set an + # origin before calling this method. + def set_cookie_value string = cookie_value if @for_domain - string << "; Domain=#{@domain}" + if @domain + string << "; Domain=#{@domain}" + else + raise "for_domain is specified but domain is known" + end end - if (origin + './').path != @path - string << "; Path=#{@path}" + if @path + if !@origin || (@origin + './').path != @path + string << "; Path=#{@path}" + end + else + raise "path is known" end if @max_age string << "; Max-Age=#{@max_age}" diff --git a/test/test_http_cookie.rb b/test/test_http_cookie.rb index dc2b6ad..160db16 100644 --- a/test/test_http_cookie.rb +++ b/test/test_http_cookie.rb @@ -458,6 +458,22 @@ class TestHTTPCookie < Test::Unit::TestCase def test_set_cookie_value url = URI.parse('http://rubyforge.org/path/') + [ + HTTP::Cookie.new('a', 'b', :domain => 'rubyforge.org', :path => '/path/'), + HTTP::Cookie.new('a', 'b', :origin => url), + ].each { |cookie| + cookie.set_cookie_value + } + + [ + HTTP::Cookie.new('a', 'b', :domain => 'rubyforge.org'), + HTTP::Cookie.new('a', 'b', :for_domain => true, :path => '/path/'), + ].each { |cookie| + assert_raises(RuntimeError) { + cookie.set_cookie_value + } + } + ['foo=bar', 'foo="bar"', 'foo="ba\"r baz"'].each { |cookie_value| cookie_params = @cookie_params.merge('path' => '/path/', 'secure' => 'secure', 'max-age' => 'Max-Age=1000') date = Time.at(Time.now.to_i)