dwaite-cookiejar 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,235 @@
1
+ require 'cookiejar'
2
+ require 'rubygems'
3
+
4
+ include CookieJar
5
+ describe CookieValidation do
6
+ describe "#validate_cookie" do
7
+ localaddr = 'http://localhost/foo/bar/'
8
+ it "should fail if version unset" do
9
+ lambda do
10
+ unversioned = Cookie.from_set_cookie localaddr, 'foo=bar'
11
+ unversioned.instance_variable_set :@version, nil
12
+ CookieValidation.validate_cookie localaddr, unversioned
13
+ end.should raise_error InvalidCookieError
14
+ end
15
+ it "should fail if the path is more specific" do
16
+ lambda do
17
+ subdirred = Cookie.from_set_cookie localaddr, 'foo=bar;path=/foo/bar/baz'
18
+ # validate_cookie localaddr, subdirred
19
+ end.should raise_error InvalidCookieError
20
+ end
21
+ it "should fail if the path is different than the request" do
22
+ lambda do
23
+ difdirred = Cookie.from_set_cookie localaddr, 'foo=bar;path=/baz/'
24
+ # validate_cookie localaddr, difdirred
25
+ end.should raise_error InvalidCookieError
26
+ end
27
+ it "should fail if the domain has no dots" do
28
+ lambda do
29
+ nodot = Cookie.from_set_cookie 'http://zero/', 'foo=bar;domain=zero'
30
+ # validate_cookie 'http://zero/', nodot
31
+ end.should raise_error InvalidCookieError
32
+ end
33
+ it "should fail for explicit localhost" do
34
+ lambda do
35
+ localhost = Cookie.from_set_cookie localaddr, 'foo=bar;domain=localhost'
36
+ # validate_cookie localaddr, localhost
37
+ end.should raise_error InvalidCookieError
38
+ end
39
+ it "should fail for mismatched domains" do
40
+ lambda do
41
+ foobar = Cookie.from_set_cookie 'http://www.foo.com/', 'foo=bar;domain=bar.com'
42
+ # validate_cookie 'http://www.foo.com/', foobar
43
+ end.should raise_error InvalidCookieError
44
+ end
45
+ it "should fail for domains more than one level up" do
46
+ lambda do
47
+ xyz = Cookie.from_set_cookie 'http://x.y.z.com/', 'foo=bar;domain=z.com'
48
+ # validate_cookie 'http://x.y.z.com/', xyz
49
+ end.should raise_error InvalidCookieError
50
+ end
51
+ it "should fail for setting subdomain cookies" do
52
+ lambda do
53
+ subdomain = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar;domain=auth.foo.com'
54
+ # validate_cookie 'http://foo.com/', subdomain
55
+ end.should raise_error InvalidCookieError
56
+ end
57
+ it "should handle a normal implicit internet cookie" do
58
+ normal = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar'
59
+ CookieValidation.validate_cookie('http://foo.com/', normal).should be_true
60
+ end
61
+ it "should handle a normal implicit localhost cookie" do
62
+ localhost = Cookie.from_set_cookie 'http://localhost/', 'foo=bar'
63
+ CookieValidation.validate_cookie('http://localhost/', localhost).should be_true
64
+ end
65
+ it "should handle an implicit IP address cookie" do
66
+ ipaddr = Cookie.from_set_cookie 'http://127.0.0.1/', 'foo=bar'
67
+ CookieValidation.validate_cookie('http://127.0.0.1/', ipaddr).should be_true
68
+ end
69
+ it "should handle an explicit domain on an internet site" do
70
+ explicit = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar;domain=.foo.com'
71
+ CookieValidation.validate_cookie('http://foo.com/', explicit).should be_true
72
+ end
73
+ it "should handle setting a cookie explicitly on a superdomain" do
74
+ superdomain = Cookie.from_set_cookie 'http://auth.foo.com/', 'foo=bar;domain=.foo.com'
75
+ CookieValidation.validate_cookie('http://foo.com/', superdomain).should be_true
76
+ end
77
+ it "should handle explicitly setting a cookie" do
78
+ explicit = Cookie.from_set_cookie 'http://foo.com/bar/', 'foo=bar;path=/bar/'
79
+ CookieValidation.validate_cookie('http://foo.com/bar/', explicit)
80
+ end
81
+ it "should handle setting a cookie on a higher path" do
82
+ higher = Cookie.from_set_cookie 'http://foo.com/bar/baz/', 'foo=bar;path=/bar/'
83
+ CookieValidation.validate_cookie('http://foo.com/bar/baz/', higher)
84
+ end
85
+ end
86
+ describe '#cookie_base_path' do
87
+ it "should leave '/' alone" do
88
+ CookieValidation.cookie_base_path('/').should == '/'
89
+ end
90
+ it "should strip off everything after the last '/'" do
91
+ CookieValidation.cookie_base_path('/foo/bar/baz').should == '/foo/bar/'
92
+ end
93
+ it "should handle query parameters and fragments with slashes" do
94
+ CookieValidation.cookie_base_path('/foo/bar?query=a/b/c#fragment/b/c').should == '/foo/'
95
+ end
96
+ it "should handle URI objects" do
97
+ CookieValidation.cookie_base_path(URI.parse('http://www.foo.com/bar/')).should == '/bar/'
98
+ end
99
+ it "should preserve case" do
100
+ CookieValidation.cookie_base_path("/BaR/").should == '/BaR/'
101
+ end
102
+ end
103
+ describe '#determine_cookie_path' do
104
+ it "should use the requested path when none is specified for the cookie" do
105
+ CookieValidation.determine_cookie_path('http://foo.com/', nil).should == '/'
106
+ CookieValidation.determine_cookie_path('http://foo.com/bar/baz', '').should == '/bar/'
107
+ end
108
+ it "should handle URI objects" do
109
+ CookieValidation.determine_cookie_path(URI.parse('http://foo.com/bar/'), '').should == '/bar/'
110
+ end
111
+ it "should handle Cookie objects" do
112
+ cookie = Cookie.from_set_cookie('http://foo.com/', "name=value;path=/")
113
+ CookieValidation.determine_cookie_path('http://foo.com/', cookie).should == '/'
114
+ end
115
+ it "should ignore the request when a path is specified" do
116
+ CookieValidation.determine_cookie_path('http://foo.com/ignorable/path', '/path/').should == '/path/'
117
+ end
118
+ end
119
+ describe '#compute_search_domains' do
120
+ it "should handle subdomains" do
121
+ CookieValidation.compute_search_domains('http://www.auth.foo.com/').should ==
122
+ ['www.auth.foo.com', '.www.auth.foo.com', '.auth.foo.com']
123
+ end
124
+ it "should handle root domains" do
125
+ CookieValidation.compute_search_domains('http://foo.com/').should ==
126
+ ['foo.com', '.foo.com']
127
+ end
128
+ it "should handle IP addresses" do
129
+ CookieValidation.compute_search_domains('http://127.0.0.1/').should ==
130
+ ['127.0.0.1']
131
+ end
132
+ it "should handle local addresses" do
133
+ CookieValidation.compute_search_domains('http://zero/').should ==
134
+ ['zero.local', '.zero.local', '.local']
135
+ end
136
+ end
137
+ describe '#determine_cookie_domain' do
138
+ it "should add a dot to the front of domains" do
139
+ CookieValidation.determine_cookie_domain('http://foo.com/', 'foo.com').should == '.foo.com'
140
+ end
141
+ it "should not add a second dot if one present" do
142
+ CookieValidation.determine_cookie_domain('http://foo.com/', '.foo.com').should == '.foo.com'
143
+ end
144
+ it "should handle Cookie objects" do
145
+ c = Cookie.from_set_cookie('http://foo.com/', "foo=bar;domain=foo.com")
146
+ CookieValidation.determine_cookie_domain('http://foo.com/', c).should == '.foo.com'
147
+ end
148
+ it "should handle URI objects" do
149
+ CookieValidation.determine_cookie_domain(URI.parse('http://foo.com/'), '.foo.com').should == '.foo.com'
150
+ end
151
+ it "should use an exact hostname when no domain specified" do
152
+ CookieValidation.determine_cookie_domain('http://foo.com/', '').should == 'foo.com'
153
+ end
154
+ it "should leave IPv4 addresses alone" do
155
+ CookieValidation.determine_cookie_domain('http://127.0.0.1/', '127.0.0.1').should == '127.0.0.1'
156
+ end
157
+ it "should leave IPv6 addresses alone" do
158
+ ['2001:db8:85a3::8a2e:370:7334', '::ffff:192.0.2.128'].each do |value|
159
+ CookieValidation.determine_cookie_domain("http://[#{value}]/", value).should == value
160
+ end
161
+ end
162
+ end
163
+ describe "#effective_host" do
164
+ it "should leave proper domains the same" do
165
+ ['google.com', 'www.google.com', 'google.com.'].each do |value|
166
+ CookieValidation.effective_host(value).should == value
167
+ end
168
+ end
169
+ it "should handle a URI object" do
170
+ CookieValidation.effective_host(URI.parse('http://example.com/')).should == 'example.com'
171
+ end
172
+ it "should add a local suffix on unqualified hosts" do
173
+ CookieValidation.effective_host('localhost').should == 'localhost.local'
174
+ end
175
+ it "should leave IPv4 addresses alone" do
176
+ CookieValidation.effective_host('127.0.0.1').should == '127.0.0.1'
177
+ end
178
+ it "should leave IPv6 addresses alone" do
179
+ ['2001:db8:85a3::8a2e:370:7334', ':ffff:192.0.2.128'].each do |value|
180
+ CookieValidation.effective_host(value).should == value
181
+ end
182
+ end
183
+ it "should lowercase addresses" do
184
+ CookieValidation.effective_host('FOO.COM').should == 'foo.com'
185
+ end
186
+ end
187
+ describe '#match_domains' do
188
+ it "should handle exact matches" do
189
+ CookieValidation.domains_match('localhost.local', 'localhost.local').should == 'localhost.local'
190
+ CookieValidation.domains_match('foo.com', 'foo.com').should == 'foo.com'
191
+ CookieValidation.domains_match('127.0.0.1', '127.0.0.1').should == '127.0.0.1'
192
+ CookieValidation.domains_match('::ffff:192.0.2.128', '::ffff:192.0.2.128').should == '::ffff:192.0.2.128'
193
+ end
194
+ it "should handle matching a superdomain" do
195
+ CookieValidation.domains_match('.foo.com', 'auth.foo.com').should == '.foo.com'
196
+ CookieValidation.domains_match('.y.z.foo.com', 'x.y.z.foo.com').should == '.y.z.foo.com'
197
+ end
198
+ it "should not match superdomains, or illegal domains" do
199
+ CookieValidation.domains_match('.z.foo.com', 'x.y.z.foo.com').should be_nil
200
+ CookieValidation.domains_match('foo.com', 'com').should be_nil
201
+ end
202
+ it "should not match domains with and without a dot suffix together" do
203
+ CookieValidation.domains_match('foo.com.', 'foo.com').should be_nil
204
+ end
205
+ end
206
+ describe '#hostname_reach' do
207
+ it "should find the next highest subdomain" do
208
+ {'www.google.com' => 'google.com', 'auth.corp.companyx.com' => 'corp.companyx.com'}.each do |entry|
209
+ CookieValidation.hostname_reach(entry[0]).should == entry[1]
210
+ end
211
+ end
212
+ it "should handle domains with suffixed dots" do
213
+ CookieValidation.hostname_reach('www.google.com.').should == 'google.com.'
214
+ end
215
+ it "should return nil for a root domain" do
216
+ CookieValidation.hostname_reach('github.com').should be_nil
217
+ end
218
+ it "should return 'local' for a local domain" do
219
+ ['foo.local', 'foo.local.'].each do |hostname|
220
+ CookieValidation.hostname_reach(hostname).should == 'local'
221
+ end
222
+ end
223
+ it "should handle mixed-case '.local'" do
224
+ CookieValidation.hostname_reach('foo.LOCAL').should == 'local'
225
+ end
226
+ it "should return nil for an IPv4 address" do
227
+ CookieValidation.hostname_reach('127.0.0.1').should be_nil
228
+ end
229
+ it "should return nil for IPv6 addresses" do
230
+ ['2001:db8:85a3::8a2e:370:7334', '::ffff:192.0.2.128'].each do |value|
231
+ CookieValidation.hostname_reach(value).should be_nil
232
+ end
233
+ end
234
+ end
235
+ end
data/test/jar_test.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'cookiejar'
2
2
  require 'yaml'
3
+ require 'rubygems'
4
+
3
5
  include CookieJar
4
6
 
5
7
  describe Jar do
@@ -53,9 +55,92 @@ describe Jar do
53
55
  uri = 'http://foo.com/a/b/c/d'
54
56
  jar.set_cookie uri, 'a=bar'
55
57
  jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
56
- cookie_headers = jar.get_cookie_headers uri
57
- cookie_headers.should have(2).items
58
- cookie_headers.should == [['Cookie', 'b=baz'],['Cookie', 'a=bar']]
58
+ cookie_headers = jar.get_cookie_header uri
59
+ cookie_headers.should == "b=baz;a=bar"
60
+ end
61
+ end
62
+ describe '.add_cookie' do
63
+ it "should let me add a pre-existing cookie" do
64
+ jar = Jar.new
65
+ cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=bar'
66
+ jar.add_cookie cookie
67
+ end
68
+ end
69
+ describe '.to_a' do
70
+ it "should return me an array of all cookie objects" do
71
+ uri = 'http://foo.com/a/b/c/d'
72
+ jar = Jar.new
73
+ jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
74
+ jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
75
+ jar.set_cookie uri, 'c=bar;path=/a/b'
76
+ jar.set_cookie uri, 'd=bar;path=/a/'
77
+ jar.set_cookie 'http://localhost/', 'foo=bar'
78
+ jar.to_a.should have(5).items
79
+ end
80
+ end
81
+ describe '.expire_cookies' do
82
+ it "should expire cookies which are no longer valid" do
83
+ uri = 'http://foo.com/a/b/c/d'
84
+ jar = Jar.new
85
+ jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
86
+ jar.set_cookie uri, 'b=baz;path=/a/b/c/d;expires=Wednesday, 01-Nov-2028 12:00:00 GMT'
87
+ jar.set_cookie uri, 'c=bar;path=/a/b'
88
+ jar.set_cookie uri, 'd=bar;path=/a/'
89
+ jar.set_cookie 'http://localhost/', 'foo=bar'
90
+ jar.to_a.should have(5).items
91
+ jar.expire_cookies
92
+ jar.to_a.should have(4).items
93
+ end
94
+ it "should let me expire all session cookies" do
95
+ uri = 'http://foo.com/a/b/c/d'
96
+ jar = Jar.new
97
+ jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
98
+ jar.set_cookie uri, 'b=baz;path=/a/b/c/d;expires=Wednesday, 01-Nov-2028 12:00:00 GMT'
99
+ jar.set_cookie uri, 'c=bar;path=/a/b'
100
+ jar.set_cookie uri, 'd=bar;path=/a/'
101
+ jar.set_cookie 'http://localhost/', 'foo=bar'
102
+ jar.to_a.should have(5).items
103
+ jar.expire_cookies true
104
+ jar.to_a.should have(1).items
105
+ end
106
+ end
107
+ begin
108
+ require 'json'
109
+ describe ".to_json" do
110
+ it "should serialize cookies to JSON" do
111
+
112
+ c = Cookie.from_set_cookie 'https://localhost/', 'foo=bar;secure;expires=Wed, 01-Nov-2028 12:00:00 GMT'
113
+ jar = Jar.new
114
+ jar.add_cookie c
115
+ json = jar.to_json
116
+ json.should be_a String
117
+ end
118
+ end
119
+ describe ".json_create" do
120
+ it "should deserialize a JSON array to a jar" do
121
+ json = "[{\"name\":\"foo\",\"value\":\"bar\",\"domain\":\"localhost.local\",\"path\":\"\\/\",\"created_at\":\"2009-09-11 12:51:03 -0600\",\"expiry\":\"2028-11-01 12:00:00 GMT\",\"secure\":true}]"
122
+ array = JSON.parse json
123
+
124
+ jar = Jar.json_create array
125
+ jar.get_cookies('https://localhost/').should have(1).items
126
+ end
127
+ it "should deserialize a JSON hash to a jar" do
128
+ json = "{\"cookies\":[{\"name\":\"foo\",\"value\":\"bar\",\"domain\":\"localhost.local\",\"path\":\"\\/\",\"created_at\":\"2009-09-11 12:51:03 -0600\",\"expiry\":\"2028-11-01 12:00:00 GMT\",\"secure\":true}]}"
129
+ hash = JSON.parse json
130
+
131
+ jar = Jar.json_create hash
132
+ jar.get_cookies('https://localhost/').should have(1).items
133
+ end
134
+
135
+ it "should automatically deserialize to a jar" do
136
+ json = "{\"json_class\":\"CookieJar::Jar\",\"cookies\":[{\"name\":\"foo\",\"value\":\"bar\",\"domain\":\"localhost.local\",\"path\":\"\\/\",\"created_at\":\"2009-09-11 12:51:03 -0600\",\"expiry\":\"2028-11-01 12:00:00 GMT\",\"secure\":true}]}"
137
+ jar = JSON.parse json
138
+ jar.get_cookies('https://localhost/').should have(1).items
139
+ end
140
+ end
141
+ rescue LoadError
142
+ it "does not appear the JSON library is installed" do
143
+ raise 'please install the JSON lirbary'
59
144
  end
60
145
  end
61
- end
146
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dwaite-cookiejar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Waite
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-11 00:00:00 -07:00
12
+ date: 2009-09-13 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,13 +23,15 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - lib/cookiejar/cookie.rb
26
- - lib/cookiejar/cookie_common.rb
26
+ - lib/cookiejar/cookie_validation.rb
27
27
  - lib/cookiejar/jar.rb
28
28
  - lib/cookiejar.rb
29
29
  - test/cookie_test.rb
30
+ - test/cookie_validation_test.rb
30
31
  - test/jar_test.rb
31
32
  has_rdoc: false
32
33
  homepage: http://alkaline-solutions.com
34
+ licenses:
33
35
  post_install_message:
34
36
  rdoc_options:
35
37
  - --title
@@ -51,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
53
  requirements: []
52
54
 
53
55
  rubyforge_project:
54
- rubygems_version: 1.2.0
56
+ rubygems_version: 1.3.5
55
57
  signing_key:
56
58
  specification_version: 3
57
59
  summary: Client-side HTTP Cookie library
@@ -1,14 +0,0 @@
1
- module CookieJar
2
- class CookieError < StandardError; end
3
- # Represents all cookie validation errors
4
- class InvalidCookieError < CookieError;
5
- attr_reader :messages
6
- def initialize message
7
- if message.is_a? Array
8
- @messages = message
9
- message = message.join ', '
10
- end
11
- super(message)
12
- end
13
- end
14
- end