Cookie#new: Reject a mixed case symbol as keyword for simplicity.

This fixes error with Ruby 1.8.
This commit is contained in:
Akinori MUSHA 2013-04-16 16:39:55 +09:00
parent 20cb22bbb0
commit 04950f6796
2 changed files with 14 additions and 21 deletions

View file

@ -111,15 +111,11 @@ class HTTP::Cookie
# new(**attr_hash) # new(**attr_hash)
# #
# Creates a cookie object. For each key of `attr_hash`, the setter # Creates a cookie object. For each key of `attr_hash`, the setter
# is called if defined. Each key can be either a symbol or a # is called if defined and any error (typically ArgumentError or
# string of downcased attribute names. # TypeError) that is raised will be passed through. Each key can be
# # either a downcased symbol or a string that may be mixed case.
# This method accepts any attribute name for which a setter method # Support for the latter may, however, be obsoleted in future when
# is defined. Beware, however, any error (typically ArgumentError) # Ruby 2.0's keyword syntax is adopted.
# 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 # If `value` is omitted or it is nil, an expiration cookie is
# created unless `max_age` or `expires` (`expires_at`) is given. # created unless `max_age` or `expires` (`expires_at`) is given.
@ -183,20 +179,17 @@ class HTTP::Cookie
@httponly = val @httponly = val
when :secure, :secure? when :secure, :secure?
@secure = val @secure = val
when /[A-Z]/
warn "keyword should be downcased: #{okey.inspect}" if $VERBOSE
key = key.downcase
redo
when Symbol when Symbol
setter = :"#{key}=" setter = :"#{key}="
if respond_to?(setter) if respond_to?(setter)
__send__(setter, val) __send__(setter, val)
else else
warn "unknown keyword: #{okey.inspect}" if $VERBOSE warn "unknown attribute name: #{okey.inspect}" if $VERBOSE
next
end end
when String when String
warn "use symbol for keyword: #{okey.inspect}" if $VERBOSE warn "use downcased symbol for keyword: #{okey.inspect}" if $VERBOSE
key = key.to_sym key = key.downcase.to_sym
redo redo
else else
warn "invalid keyword ignored: #{okey.inspect}" if $VERBOSE warn "invalid keyword ignored: #{okey.inspect}" if $VERBOSE

View file

@ -528,25 +528,25 @@ class TestHTTPCookie < Test::Unit::TestCase
# various keywords # various keywords
[ [
[:Expires, /should be downcased/], ["Expires", /use downcased symbol/],
["Expires", /should be downcased.*use symbol/m],
].each { |key, pattern| ].each { |key, pattern|
assert_warning(pattern, "warn of key: #{key.inspect}") { assert_warning(pattern, "warn of key: #{key.inspect}") {
cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup) cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup)
assert_equal 'key', cookie.name assert_equal 'key', cookie.name
assert_equal 'value', cookie.value assert_equal 'value', cookie.value
assert_equal expires, cookie.expires assert_equal expires, cookie.expires, "key: #{key.inspect}"
} }
} }
[ [
[:expires?, /unknown keyword/], [:Expires, /unknown attribute name/],
[:expires?, /unknown attribute name/],
[[:expires], /invalid keyword/], [[:expires], /invalid keyword/],
].each { |key, pattern| ].each { |key, pattern|
assert_warning(pattern, "warn of key: #{key.inspect}") { assert_warning(pattern, "warn of key: #{key.inspect}") {
cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup) cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup)
assert_equal 'key', cookie.name assert_equal 'key', cookie.name
assert_equal 'value', cookie.value assert_equal 'value', cookie.value
assert_equal nil, cookie.expires assert_equal nil, cookie.expires, "key: #{key.inspect}"
} }
} }