summaryrefslogtreecommitdiff
path: root/test/cgi/test_cgi_cookie.rb
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-27 01:38:23 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-27 01:38:23 +0000
commit19b25eb670b2121b09eb0d89df8350286e8856ff (patch)
treec934479f4227b736f2a6ab898721bf57fe4f9129 /test/cgi/test_cgi_cookie.rb
parent1366802a1f24065c9d02f47f59d9a0519f9cd516 (diff)
* 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
Diffstat (limited to 'test/cgi/test_cgi_cookie.rb')
-rw-r--r--test/cgi/test_cgi_cookie.rb107
1 files changed, 107 insertions, 0 deletions
diff --git a/test/cgi/test_cgi_cookie.rb b/test/cgi/test_cgi_cookie.rb
new file mode 100644
index 00000000000..f9950d1fb13
--- /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