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 # is called if defined. Each key can be either a symbol or a
# string of downcased attribute names. # 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) # 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 # 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.
@ -157,8 +160,8 @@ class HTTP::Cookie
end end
for_domain = false for_domain = false
domain = max_age = origin = nil domain = max_age = origin = nil
attr_hash.each_pair { |key, val| attr_hash.each_pair { |okey, val|
case key.to_sym case key ||= okey
when :name when :name
self.name = val self.name = val
when :value when :value
@ -181,7 +184,7 @@ class HTTP::Cookie
when :secure, :secure? when :secure, :secure?
@secure = val @secure = val
when /[A-Z]/ when /[A-Z]/
warn "keyword should be downcased: #{key}" if $VERBOSE warn "keyword should be downcased: #{okey.inspect}" if $VERBOSE
key = key.downcase key = key.downcase
redo redo
when Symbol when Symbol
@ -189,14 +192,15 @@ class HTTP::Cookie
if respond_to?(setter) if respond_to?(setter)
__send__(setter, val) __send__(setter, val)
else else
warn "unknown keyword: #{key}" if $VERBOSE warn "unknown keyword: #{okey.inspect}" if $VERBOSE
end end
when String when String
warn "use symbol for keyword: #{okey.inspect}" if $VERBOSE
key = key.to_sym key = key.to_sym
redo redo
else else
key = key.to_s warn "invalid keyword ignored: #{okey.inspect}" if $VERBOSE
redo next
end end
} }
if @name.nil? if @name.nil?

View file

@ -42,7 +42,11 @@ class HTTP::CookieJar::YAMLSaver < HTTP::CookieJar::AbstractSaver
# YAML::Object of Syck # YAML::Object of Syck
cookie_hash = cookie_hash.ivars cookie_hash = cookie_hash.ivars
end 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) jar.add(cookie)
} }
} }

View file

@ -3,6 +3,30 @@ require 'test-unit'
require 'uri' require 'uri'
require 'http/cookie' 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 module Enumerable
def combine def combine
masks = inject([[], 1]){|(ar, m), e| [ar << m, m << 1 ] }[0] 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__)) require File.expand_path('helper', File.dirname(__FILE__))
class TestHTTPCookie < Test::Unit::TestCase class TestHTTPCookie < Test::Unit::TestCase
def silently
warn_level, $VERBOSE = $VERBOSE, false
yield
ensure
$VERBOSE = warn_level
end
def setup def setup
httpdate = 'Sun, 27-Sep-2037 00:00:00 GMT' httpdate = 'Sun, 27-Sep-2037 00:00:00 GMT'
@ -46,12 +39,10 @@ class TestHTTPCookie < Test::Unit::TestCase
dates.each do |date| dates.each do |date|
cookie = "PREF=1; expires=#{date}" cookie = "PREF=1; expires=#{date}"
silently do assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c| assert c.expires, "Tried parsing: #{date}"
assert c.expires, "Tried parsing: #{date}" assert_send [c.expires, :<, yesterday]
assert_send [c.expires, :<, yesterday] }.size
}.size
end
end end
[ [
@ -177,14 +168,12 @@ class TestHTTPCookie < Test::Unit::TestCase
"20/06/95 21:07", "20/06/95 21:07",
] ]
silently do dates.each { |date|
dates.each do |date| cookie = "PREF=1; expires=#{date}"
cookie = "PREF=1; expires=#{date}" assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c| assert_equal(true, c.expires.nil?)
assert_equal(true, c.expires.nil?) }.size
}.size }
end
end
end end
def test_parse_domain_dot def test_parse_domain_dot
@ -537,6 +526,30 @@ class TestHTTPCookie < Test::Unit::TestCase
cookie.acceptable? 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) cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup)
assert_equal 'key', cookie.name assert_equal 'key', cookie.name
assert_equal 'value', cookie.value assert_equal 'value', cookie.value