Cookie#new prefers downcased symbols for keywords.

Test warnings.
This commit is contained in:
Akinori MUSHA 2013-04-16 08:44:48 +09:00
parent c293005676
commit 5bdb8f41ae
4 changed files with 75 additions and 30 deletions

View file

@ -114,9 +114,12 @@ class HTTP::Cookie
# is called if defined. Each key can be either a symbol or a
# string of downcased attribute names.
#
# This methods accepts any attribute name for which a setter method
# This method accepts any attribute name for which a setter method
# is defined. Beware, however, any error (typically ArgumentError)
# a setter method raises will be passed through.
# a setter method raises will be passed through. It is preferred
# that each keyword is a downcased symbol. Support for strings and
# mixed case keywords will be obsoleted when ruby 2.0 keyword syntax
# is adopted.
#
# If `value` is omitted or it is nil, an expiration cookie is
# created unless `max_age` or `expires` (`expires_at`) is given.
@ -157,8 +160,8 @@ class HTTP::Cookie
end
for_domain = false
domain = max_age = origin = nil
attr_hash.each_pair { |key, val|
case key.to_sym
attr_hash.each_pair { |okey, val|
case key ||= okey
when :name
self.name = val
when :value
@ -181,7 +184,7 @@ class HTTP::Cookie
when :secure, :secure?
@secure = val
when /[A-Z]/
warn "keyword should be downcased: #{key}" if $VERBOSE
warn "keyword should be downcased: #{okey.inspect}" if $VERBOSE
key = key.downcase
redo
when Symbol
@ -189,14 +192,15 @@ class HTTP::Cookie
if respond_to?(setter)
__send__(setter, val)
else
warn "unknown keyword: #{key}" if $VERBOSE
warn "unknown keyword: #{okey.inspect}" if $VERBOSE
end
when String
warn "use symbol for keyword: #{okey.inspect}" if $VERBOSE
key = key.to_sym
redo
else
key = key.to_s
redo
warn "invalid keyword ignored: #{okey.inspect}" if $VERBOSE
next
end
}
if @name.nil?

View file

@ -42,7 +42,11 @@ class HTTP::CookieJar::YAMLSaver < HTTP::CookieJar::AbstractSaver
# YAML::Object of Syck
cookie_hash = cookie_hash.ivars
end
cookie = HTTP::Cookie.new(cookie_hash)
cookie = HTTP::Cookie.new({}.tap { |hash|
cookie_hash.each_pair { |key, value|
hash[key.to_sym] = value
}
})
jar.add(cookie)
}
}

View file

@ -3,6 +3,30 @@ require 'test-unit'
require 'uri'
require 'http/cookie'
module Test
module Unit
module Assertions
def assert_warn(pattern, message = nil, &block)
class << (output = "")
alias write <<
end
stderr, $stderr = $stderr, output
yield
assert_match(pattern, output, message)
ensure
$stderr = stderr
end
def assert_warning(pattern, message = nil, &block)
verbose, $VERBOSE = $VERBOSE, true
assert_warn(pattern, message, &block)
ensure
$VERBOSE = verbose
end
end
end
end
module Enumerable
def combine
masks = inject([[], 1]){|(ar, m), e| [ar << m, m << 1 ] }[0]

View file

@ -2,13 +2,6 @@
require File.expand_path('helper', File.dirname(__FILE__))
class TestHTTPCookie < Test::Unit::TestCase
def silently
warn_level, $VERBOSE = $VERBOSE, false
yield
ensure
$VERBOSE = warn_level
end
def setup
httpdate = 'Sun, 27-Sep-2037 00:00:00 GMT'
@ -46,12 +39,10 @@ class TestHTTPCookie < Test::Unit::TestCase
dates.each do |date|
cookie = "PREF=1; expires=#{date}"
silently do
assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
assert c.expires, "Tried parsing: #{date}"
assert_send [c.expires, :<, yesterday]
}.size
end
assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
assert c.expires, "Tried parsing: #{date}"
assert_send [c.expires, :<, yesterday]
}.size
end
[
@ -177,14 +168,12 @@ class TestHTTPCookie < Test::Unit::TestCase
"20/06/95 21:07",
]
silently do
dates.each do |date|
cookie = "PREF=1; expires=#{date}"
assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
assert_equal(true, c.expires.nil?)
}.size
end
end
dates.each { |date|
cookie = "PREF=1; expires=#{date}"
assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
assert_equal(true, c.expires.nil?)
}.size
}
end
def test_parse_domain_dot
@ -537,6 +526,30 @@ class TestHTTPCookie < Test::Unit::TestCase
cookie.acceptable?
}
# various keywords
[
[:Expires, /should be downcased/],
["Expires", /should be downcased.*use symbol/m],
].each { |key, pattern|
assert_warning(pattern, "warn of key: #{key.inspect}") {
cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup)
assert_equal 'key', cookie.name
assert_equal 'value', cookie.value
assert_equal expires, cookie.expires
}
}
[
[:expires?, /unknown keyword/],
[[:expires], /invalid keyword/],
].each { |key, pattern|
assert_warning(pattern, "warn of key: #{key.inspect}") {
cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup)
assert_equal 'key', cookie.name
assert_equal 'value', cookie.value
assert_equal nil, cookie.expires
}
}
cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup)
assert_equal 'key', cookie.name
assert_equal 'value', cookie.value