HTTP::Cookie.set_cookie_value: Don't take an origin argument.

This commit is contained in:
Akinori MUSHA 2013-04-15 08:54:09 +09:00
parent 88e3f28591
commit 28458101b6
2 changed files with 32 additions and 12 deletions

View file

@ -593,21 +593,25 @@ class HTTP::Cookie
end end
alias to_s cookie_value alias to_s cookie_value
# Returns a string for use in the Set-Cookie header. If the cookie # Returns a string for use in the Set-Cookie header. If necessary
# does not have an origin set, one must be given from the argument. # information like a path or domain (when `for_domain` is set) is
# # missing, RuntimeError is raised. It is always the best to set an
# This method does not check if this cookie will be accepted from # origin before calling this method.
# the origin. def set_cookie_value
def set_cookie_value(origin = nil)
origin = origin ? URI(origin) : @origin or
raise "origin must be specified to produce a value for Set-Cookie"
string = cookie_value string = cookie_value
if @for_domain if @for_domain
string << "; Domain=#{@domain}" if @domain
string << "; Domain=#{@domain}"
else
raise "for_domain is specified but domain is known"
end
end end
if (origin + './').path != @path if @path
string << "; Path=#{@path}" if !@origin || (@origin + './').path != @path
string << "; Path=#{@path}"
end
else
raise "path is known"
end end
if @max_age if @max_age
string << "; Max-Age=#{@max_age}" string << "; Max-Age=#{@max_age}"

View file

@ -458,6 +458,22 @@ class TestHTTPCookie < Test::Unit::TestCase
def test_set_cookie_value def test_set_cookie_value
url = URI.parse('http://rubyforge.org/path/') 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| ['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') cookie_params = @cookie_params.merge('path' => '/path/', 'secure' => 'secure', 'max-age' => 'Max-Age=1000')
date = Time.at(Time.now.to_i) date = Time.at(Time.now.to_i)