summaryrefslogtreecommitdiff
path: root/test/webrick/test_cookie.rb
blob: 48f0235aebaff0d779dec92e740f0e53e1a96102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require "test/unit"
require "webrick/cookie"

class TestWEBrickCookie < Test::Unit::TestCase
  def test_new
    cookie = WEBrick::Cookie.new("foo","bar")
    assert_equal("foo", cookie.name)
    assert_equal("bar", cookie.value)
    assert_equal("foo=bar", cookie.to_s)
  end

  def test_time
    cookie = WEBrick::Cookie.new("foo","bar")
    t = 1000000000
    cookie.max_age = t
    assert_match(t.to_s, cookie.to_s)

    cookie = WEBrick::Cookie.new("foo","bar")
    t = Time.at(1000000000)
    cookie.expires = t
    assert_equal(Time, cookie.expires.class)
    assert_equal(t, cookie.expires)
    ts = t.httpdate
    cookie.expires = ts
    assert_equal(Time, cookie.expires.class)
    assert_equal(t, cookie.expires)
    assert_match(ts, cookie.to_s)
  end

  def test_parse
    data = ""
    data << '$Version="1"; '
    data << 'Customer="WILE_E_COYOTE"; $Path="/acme"; '
    data << 'Part_Number="Rocket_Launcher_0001"; $Path="/acme"; '
    data << 'Shipping="FedEx"; $Path="/acme"'
    cookies = WEBrick::Cookie.parse(data)
    assert_equal(1, cookies[0].version)
    assert_equal("Customer", cookies[0].name)
    assert_equal("WILE_E_COYOTE", cookies[0].value)
    assert_equal("/acme", cookies[0].path)
    assert_equal(1, cookies[1].version)
    assert_equal("Part_Number", cookies[1].name)
    assert_equal("Rocket_Launcher_0001", cookies[1].value)
    assert_equal(1, cookies[2].version)
    assert_equal("Shipping", cookies[2].name)
    assert_equal("FedEx", cookies[2].value)

    data = "hoge=moge; __div__session=9865ecfd514be7f7"
    cookies = WEBrick::Cookie.parse(data)
    assert_equal(0, cookies[0].version)
    assert_equal("hoge", cookies[0].name)
    assert_equal("moge", cookies[0].value)
    assert_equal("__div__session", cookies[1].name)
    assert_equal("9865ecfd514be7f7", cookies[1].value)
  end

  def test_parse_non_whitespace
    data = [
      '$Version="1";',
      'Customer="WILE_E_COYOTE";$Path="/acme";',
      'Part_Number="Rocket_Launcher_0001";$Path="/acme";',
      'Shipping="FedEx";$Path="/acme"'
    ].join
    cookies = WEBrick::Cookie.parse(data)
    assert_equal(1, cookies[0].version)
    assert_equal("Customer", cookies[0].name)
    assert_equal("WILE_E_COYOTE", cookies[0].value)
    assert_equal("/acme", cookies[0].path)
    assert_equal(1, cookies[1].version)
    assert_equal("Part_Number", cookies[1].name)
    assert_equal("Rocket_Launcher_0001", cookies[1].value)
    assert_equal(1, cookies[2].version)
    assert_equal("Shipping", cookies[2].name)
    assert_equal("FedEx", cookies[2].value)
  end

  def test_parse_set_cookie
    data = %(Customer="WILE_E_COYOTE"; Version="1"; Path="/acme")
    cookie = WEBrick::Cookie.parse_set_cookie(data)
    assert_equal("Customer", cookie.name)
    assert_equal("WILE_E_COYOTE", cookie.value)
    assert_equal(1, cookie.version)
    assert_equal("/acme", cookie.path)

    data = %(Shipping="FedEx"; Version="1"; Path="/acme"; Secure)
    cookie = WEBrick::Cookie.parse_set_cookie(data)
    assert_equal("Shipping", cookie.name)
    assert_equal("FedEx", cookie.value)
    assert_equal(1, cookie.version)
    assert_equal("/acme", cookie.path)
    assert_equal(true, cookie.secure)
  end

  def test_parse_set_cookies
    data = %(Shipping="FedEx"; Version="1"; Path="/acme"; Secure)
    data << %(, CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; path=/; Secure)
    data << %(, name="Aaron"; Version="1"; path="/acme")
    cookies = WEBrick::Cookie.parse_set_cookies(data)
    assert_equal(3, cookies.length)

    fed_ex = cookies.find { |c| c.name == 'Shipping' }
    assert_not_nil(fed_ex)
    assert_equal("Shipping", fed_ex.name)
    assert_equal("FedEx", fed_ex.value)
    assert_equal(1, fed_ex.version)
    assert_equal("/acme", fed_ex.path)
    assert_equal(true, fed_ex.secure)

    name = cookies.find { |c| c.name == 'name' }
    assert_not_nil(name)
    assert_equal("name", name.name)
    assert_equal("Aaron", name.value)
    assert_equal(1, name.version)
    assert_equal("/acme", name.path)

    customer = cookies.find { |c| c.name == 'CUSTOMER' }
    assert_not_nil(customer)
    assert_equal("CUSTOMER", customer.name)
    assert_equal("WILE_E_COYOTE", customer.value)
    assert_equal(0, customer.version)
    assert_equal("/", customer.path)
    assert_equal(Time.utc(1999, 11, 9, 23, 12, 40), customer.expires)
  end
end