public
Description: GET, POST, PUT, DELETE, with something around (ruby)
Homepage: http://rufus.rubyforge.org/rufus-verbs
Clone URL: git://github.com/jmettraux/rufus-verbs.git
rufus-verbs / test / cookie0_test.rb
100644 144 lines (106 sloc) 3.632 kb
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#
# Testing rufus-verbs
#
# jmettraux@gmail.com
#
# Sat Jan 19 18:22:48 JST 2008
#
 
 
require File.dirname(__FILE__) + '/base.rb'
#require 'rufus/verbs/cookies'
 
 
class Cookie0Test < Test::Unit::TestCase
  #include TestBaseMixin
 
  include Rufus::Verbs::CookieMixin
  include Rufus::Verbs::HostMixin
 
  #
  # testing split_host(s)
  #
  def test_0
 
    assert_equal [ 'localhost', nil ], split_host('localhost')
    assert_equal [ 'benz', '.car.co.nz' ], split_host('benz.car.co.nz')
    assert_equal [ '127.0.0.1', nil ], split_host('127.0.0.1')
    assert_equal [ '::1', nil ], split_host('::1')
  end
 
  #
  # testing the CookieJar
  #
  def test_1
 
    cookie0 = TestCookie.new
    cookie1 = TestCookie.new
 
    jar = Rufus::Verbs::CookieJar.new 77
    assert_equal 0, jar.size
 
    jar.add_cookie(".rubyforge.org", "/", cookie0)
    assert_equal 1, jar.size
    assert_equal [ cookie0 ], jar.fetch_cookies("rufus.rubyforge.org", "/main")
 
    jar.add_cookie("rufus.rubyforge.org", "/sub", cookie1)
    assert_equal 2, jar.size
    assert_equal [ cookie1, cookie0 ], jar.fetch_cookies("rufus.rubyforge.org", "/sub/0")
    assert_equal [ cookie0 ], jar.fetch_cookies("rufus.rubyforge.org", "/main")
    assert_equal [ cookie0 ], jar.fetch_cookies("rufus.rubyforge.org", "/")
 
    jar.remove_cookie("rufus.rubyforge.org", "/sub", cookie1)
    assert_equal 1, jar.size
  end
 
  #
  # testing cookie_acceptable?(opts, cookie)
  #
  def test_2
 
    jar = Rufus::Verbs::CookieJar.new 77
 
    opts = { :host => 'rufus.rubyforge.org', :path => '/' }
    c = TestCookie.new '.rubyforge.org', '/'
    r = TestResponse.new opts
    assert cookie_acceptable?(opts, r, c)
 
    # * The value for the Domain attribute contains no embedded dots
    # or does not start with a dot.
 
    opts = { :host => 'rufus.rubyforge.org', :path => '/' }
    c = TestCookie.new 'rufus.rubyforge.org', '/'
    r = TestResponse.new opts
    assert ! cookie_acceptable?(opts, r, c)
 
    opts = { :host => 'rufus.rubyforge.org', :path => '/' }
    c = TestCookie.new 'org', '/'
    r = TestResponse.new opts
    assert ! cookie_acceptable?(opts, r, c)
 
    # * The value for the Path attribute is not a prefix of the
    # request-URI.
 
    opts = { :host => 'rufus.rubyforge.org', :path => '/this' }
    c = TestCookie.new '.rubyforge.org', '/that'
    r = TestResponse.new opts
    assert ! cookie_acceptable?(opts, r, c)
 
    # * The value for the request-host does not domain-match the
    # Domain attribute.
 
    opts = { :host => 'rufus.rubyforg.org', :path => '/' }
    c = TestCookie.new '.rubyforge.org', '/'
    r = TestResponse.new opts
    assert ! cookie_acceptable?(opts, r, c)
 
    # * The request-host is a FQDN (not IP address) and has the form
    # HD, where D is the value of the Domain attribute, and H is a
    # string that contains one or more dots.
 
    # implicit...
  end
 
  #def test_webrick_cookie
  # require 'webrick/cookie'
  # cookie = "PREF=ID=18da97219de4985:TM=12007507:LM=12007507:S=Guc1JcA15ySZYl2n; expires=Mon, 18-Jan-2010 09:30:37 GMT; path=/; domain=.google.com"
  # p WEBrick::Cookie.parse_set_cookie(cookie)
  # p Rufus::Verbs::Cookie.parse_set_cookie(cookie)
  #end
 
  protected
 
  class TestCookie
 
    attr_reader :domain, :path, :name
 
    def initialize (domain=nil, path=nil, name='whatever')
 
      @domain = domain
      @path = path
      @name = name
    end
  end
 
  class TestResponse
 
    def initialize (opts)
 
      @path = opts[:path]
    end
 
    def request
 
      r = Object.new
      class << r
        attr_accessor :path
      end
      r.path = @path
      r
    end
  end
end