From 04950f6796a5d75ef3543fb75e5c615012cb5f21 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 16 Apr 2013 16:39:55 +0900 Subject: [PATCH] Cookie#new: Reject a mixed case symbol as keyword for simplicity. This fixes error with Ruby 1.8. --- lib/http/cookie.rb | 25 +++++++++---------------- test/test_http_cookie.rb | 10 +++++----- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/http/cookie.rb b/lib/http/cookie.rb index d675369..5916a7f 100644 --- a/lib/http/cookie.rb +++ b/lib/http/cookie.rb @@ -111,15 +111,11 @@ class HTTP::Cookie # new(**attr_hash) # # 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 - # string of downcased attribute names. - # - # 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. 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. + # is called if defined and any error (typically ArgumentError or + # TypeError) that is raised will be passed through. Each key can be + # either a downcased symbol or a string that may be mixed case. + # Support for the latter may, however, be obsoleted in future when + # Ruby 2.0's 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. @@ -183,20 +179,17 @@ class HTTP::Cookie @httponly = val when :secure, :secure? @secure = val - when /[A-Z]/ - warn "keyword should be downcased: #{okey.inspect}" if $VERBOSE - key = key.downcase - redo when Symbol setter = :"#{key}=" if respond_to?(setter) __send__(setter, val) else - warn "unknown keyword: #{okey.inspect}" if $VERBOSE + warn "unknown attribute name: #{okey.inspect}" if $VERBOSE + next end when String - warn "use symbol for keyword: #{okey.inspect}" if $VERBOSE - key = key.to_sym + warn "use downcased symbol for keyword: #{okey.inspect}" if $VERBOSE + key = key.downcase.to_sym redo else warn "invalid keyword ignored: #{okey.inspect}" if $VERBOSE diff --git a/test/test_http_cookie.rb b/test/test_http_cookie.rb index cfa2a22..8854bcf 100644 --- a/test/test_http_cookie.rb +++ b/test/test_http_cookie.rb @@ -528,25 +528,25 @@ class TestHTTPCookie < Test::Unit::TestCase # various keywords [ - [:Expires, /should be downcased/], - ["Expires", /should be downcased.*use symbol/m], + ["Expires", /use downcased symbol/], ].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 + assert_equal expires, cookie.expires, "key: #{key.inspect}" } } [ - [:expires?, /unknown keyword/], + [:Expires, /unknown attribute name/], + [:expires?, /unknown attribute name/], [[: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 + assert_equal nil, cookie.expires, "key: #{key.inspect}" } }