Project

General

Profile

Feature #502 » test_cgi_cookie.rb

xibbar (Takeyuki FUJIOKA), 08/27/2008 09:48 AM

 
1
require 'test/unit'
2
require 'cgi'
3
require 'stringio'
4

    
5

    
6
class CGICookieTest < Test::Unit::TestCase
7

    
8

    
9
  def setup
10
    ENV['REQUEST_METHOD'] = 'GET'
11
  end
12

    
13
  def teardown
14
    %W[REQUEST_METHOD SCRIPT_NAME].each do |name|
15
      ENV.delete(name)
16
    end
17
  end
18

    
19

    
20
  def test_cgi_cookie_new_simple
21
    cookie = CGI::Cookie.new('name1', 'val1', '&<>"', "\245\340\245\271\245\253")
22
    assert_equal('name1', cookie.name)
23
    assert_equal(['val1', '&<>"', "\245\340\245\271\245\253"], cookie.value)
24
    assert_nil(cookie.domain)
25
    assert_nil(cookie.expires)
26
    assert_equal('', cookie.path)
27
    assert_equal(false, cookie.secure)
28
    assert_equal("name1=val1&%26%3C%3E%22&%A5%E0%A5%B9%A5%AB; path=", cookie.to_s)
29
  end
30

    
31

    
32
  def test_cgi_cookie_new_complex
33
    t = Time.gm(2030, 12, 31, 23, 59, 59)
34
    value = ['val1', '&<>"', "\245\340\245\271\245\253"]
35
    cookie = CGI::Cookie.new('name'=>'name1',
36
                             'value'=>value,
37
                             'path'=>'/cgi-bin/myapp/',
38
                             'domain'=>'www.example.com',
39
                             'expires'=>t,
40
                             'secure'=>true
41
                             )
42
    assert_equal('name1', cookie.name)
43
    assert_equal(value, cookie.value)
44
    assert_equal('www.example.com', cookie.domain)
45
    assert_equal(t, cookie.expires)
46
    assert_equal('/cgi-bin/myapp/', cookie.path)
47
    assert_equal(true, cookie.secure)
48
    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)
49
  end
50

    
51

    
52
  def test_cgi_cookie_scriptname
53
    cookie = CGI::Cookie.new('name1', 'value1')
54
    assert_equal('', cookie.path)
55
    cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1')
56
    assert_equal('', cookie.path)
57
    ## when ENV['SCRIPT_NAME'] is set, cookie.path is set automatically
58
    ENV['SCRIPT_NAME'] = '/cgi-bin/app/example.cgi'
59
    cookie = CGI::Cookie.new('name1', 'value1')
60
    assert_equal('/cgi-bin/app/', cookie.path)
61
    cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1')
62
    assert_equal('/cgi-bin/app/', cookie.path)
63
  end
64

    
65

    
66
  def test_cgi_cookie_parse
67
    ## ';' separator
68
    cookie_str = 'name1=val1&val2; name2=val2&%26%3C%3E%22;_session_id=12345'
69
    cookies = CGI::Cookie.parse(cookie_str)
70
    list = [
71
      ['name1', ['val1', 'val2']],
72
      ['name2', ['val2', '&<>"']],
73
      ['_session_id', ['12345']],
74
    ]
75
    list.each do |name, value|
76
      cookie = cookies[name]
77
      assert_equal(name, cookie.name)
78
      assert_equal(value, cookie.value)
79
    end
80
    ## ',' separator
81
    cookie_str = 'name1=val1&val2, name2=val2&%26%3C%3E%22,_session_id=12345'
82
    cookies = CGI::Cookie.parse(cookie_str)
83
    list.each do |name, value|
84
      cookie = cookies[name]
85
      assert_equal(name, cookie.name)
86
      assert_equal(value, cookie.value)
87
    end
88
  end
89

    
90

    
91
  def test_cgi_cookie_arrayinterface
92
    cookie = CGI::Cookie.new('name1', 'a', 'b', 'c')
93
    assert_equal('a', cookie[0])
94
    assert_equal('c', cookie[2])
95
    assert_nil(cookie[3])
96
    assert_equal('a', cookie.first)
97
    assert_equal('c', cookie.last)
98
    assert_equal(['A', 'B', 'C'], cookie.collect{|e| e.upcase})
99
  end
100

    
101

    
102

    
103
  instance_methods.each do |method|
104
    private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
105
  end if ENV['TEST']
106

    
107
end