From 19b25eb670b2121b09eb0d89df8350286e8856ff Mon Sep 17 00:00:00 2001 From: naruse Date: Wed, 27 Aug 2008 01:38:23 +0000 Subject: [PATCH] * lib/cgi.rb (CGI::Cookie): remove delagate. * test/cgi/test_cgi_cookie.rb: added for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18877 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++ lib/cgi.rb | 5 +- test/cgi/test_cgi_cookie.rb | 107 ++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 test/cgi/test_cgi_cookie.rb diff --git a/ChangeLog b/ChangeLog index 8f66bc8534..699e3d3978 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Wed Aug 27 10:34:42 2008 NARUSE, Yui + + * lib/cgi.rb (CGI::Cookie): remove delagate. + + * test/cgi/test_cgi_cookie.rb: added for above. + Wed Aug 27 01:13:54 2008 Tanaka Akira * transcode.c (transcode_loop): simplified. diff --git a/lib/cgi.rb b/lib/cgi.rb index 9900923ed8..852891cbdb 100644 --- a/lib/cgi.rb +++ b/lib/cgi.rb @@ -749,9 +749,6 @@ class CGI stdoutput.print(*options) end - require "delegate" - - # Class representing an HTTP cookie. # # In addition to its specific fields and methods, a Cookie instance # is a delegator to the array of its values. @@ -784,7 +781,7 @@ class CGI # cookie1.domain = 'domain' # cookie1.expires = Time.now + 30 # cookie1.secure = true - class Cookie < DelegateClass(Array) + class Cookie < Array # Create a new CGI::Cookie object. # diff --git a/test/cgi/test_cgi_cookie.rb b/test/cgi/test_cgi_cookie.rb new file mode 100644 index 0000000000..f9950d1fb1 --- /dev/null +++ b/test/cgi/test_cgi_cookie.rb @@ -0,0 +1,107 @@ +require 'test/unit' +require 'cgi' +require 'stringio' + + +class CGICookieTest < Test::Unit::TestCase + + + def setup + ENV['REQUEST_METHOD'] = 'GET' + end + + def teardown + %W[REQUEST_METHOD SCRIPT_NAME].each do |name| + ENV.delete(name) + end + end + + + def test_cgi_cookie_new_simple + cookie = CGI::Cookie.new('name1', 'val1', '&<>"', "\245\340\245\271\245\253") + assert_equal('name1', cookie.name) + assert_equal(['val1', '&<>"', "\245\340\245\271\245\253"], cookie.value) + assert_nil(cookie.domain) + assert_nil(cookie.expires) + assert_equal('', cookie.path) + assert_equal(false, cookie.secure) + assert_equal("name1=val1&%26%3C%3E%22&%A5%E0%A5%B9%A5%AB; path=", cookie.to_s) + end + + + def test_cgi_cookie_new_complex + t = Time.gm(2030, 12, 31, 23, 59, 59) + value = ['val1', '&<>"', "\245\340\245\271\245\253"] + cookie = CGI::Cookie.new('name'=>'name1', + 'value'=>value, + 'path'=>'/cgi-bin/myapp/', + 'domain'=>'www.example.com', + 'expires'=>t, + 'secure'=>true + ) + assert_equal('name1', cookie.name) + assert_equal(value, cookie.value) + assert_equal('www.example.com', cookie.domain) + assert_equal(t, cookie.expires) + assert_equal('/cgi-bin/myapp/', cookie.path) + assert_equal(true, cookie.secure) + assert_equal('name1=val1&%26%3C%3E%22&%A5%E0%A5%B9%A5%AB; domain=www.example.com; path=/cgi-bin/myapp/; expires=Tue, 31 Dec 2030 23:59:59 GMT; secure', cookie.to_s) + end + + + def test_cgi_cookie_scriptname + cookie = CGI::Cookie.new('name1', 'value1') + assert_equal('', cookie.path) + cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1') + assert_equal('', cookie.path) + ## when ENV['SCRIPT_NAME'] is set, cookie.path is set automatically + ENV['SCRIPT_NAME'] = '/cgi-bin/app/example.cgi' + cookie = CGI::Cookie.new('name1', 'value1') + assert_equal('/cgi-bin/app/', cookie.path) + cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1') + assert_equal('/cgi-bin/app/', cookie.path) + end + + + def test_cgi_cookie_parse + ## ';' separator + cookie_str = 'name1=val1&val2; name2=val2&%26%3C%3E%22;_session_id=12345' + cookies = CGI::Cookie.parse(cookie_str) + list = [ + ['name1', ['val1', 'val2']], + ['name2', ['val2', '&<>"']], + ['_session_id', ['12345']], + ] + list.each do |name, value| + cookie = cookies[name] + assert_equal(name, cookie.name) + assert_equal(value, cookie.value) + end + ## ',' separator + cookie_str = 'name1=val1&val2, name2=val2&%26%3C%3E%22,_session_id=12345' + cookies = CGI::Cookie.parse(cookie_str) + list.each do |name, value| + cookie = cookies[name] + assert_equal(name, cookie.name) + assert_equal(value, cookie.value) + end + end + + + def test_cgi_cookie_arrayinterface + cookie = CGI::Cookie.new('name1', 'a', 'b', 'c') + assert_equal('a', cookie[0]) + assert_equal('c', cookie[2]) + assert_nil(cookie[3]) + assert_equal('a', cookie.first) + assert_equal('c', cookie.last) + assert_equal(['A', 'B', 'C'], cookie.collect{|e| e.upcase}) + end + + + + instance_methods.each do |method| + private method if method =~ /^test_(.*)/ && $1 != ENV['TEST'] + end if ENV['TEST'] + +end