cookiejar 0.3.2 → 0.3.3

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.
@@ -56,7 +56,7 @@ module CookieJar
56
56
  # @param [String] cookie_header_value the contents of the Set-Cookie
57
57
  # @return [Cookie] which was created and stored
58
58
  # @raise [InvalidCookieError] if the cookie header did not validate
59
- def set_cookie request_uri, cookie_header_values
59
+ def set_cookie(request_uri, cookie_header_values)
60
60
  cookie_header_values.split(/, (?=[\w]+=)/).each do |cookie_header_value|
61
61
  cookie = Cookie.from_set_cookie request_uri, cookie_header_value
62
62
  add_cookie cookie
@@ -70,7 +70,7 @@ module CookieJar
70
70
  # @param [String] cookie_header_value the contents of the Set-Cookie2
71
71
  # @return [Cookie] which was created and stored
72
72
  # @raise [InvalidCookieError] if the cookie header did not validate
73
- def set_cookie2 request_uri, cookie_header_value
73
+ def set_cookie2(request_uri, cookie_header_value)
74
74
  cookie = Cookie.from_set_cookie2 request_uri, cookie_header_value
75
75
  add_cookie cookie
76
76
  end
@@ -87,7 +87,7 @@ module CookieJar
87
87
  # @return [Array<Cookie>,nil] the cookies created, or nil if none found.
88
88
  # @raise [InvalidCookieError] if one of the cookie headers contained
89
89
  # invalid formatting or data
90
- def set_cookies_from_headers request_uri, http_headers
90
+ def set_cookies_from_headers(request_uri, http_headers)
91
91
  set_cookie_key = http_headers.keys.detect { |k| /\ASet-Cookie\Z/i.match k }
92
92
  cookies = gather_header_values http_headers[set_cookie_key] do |value|
93
93
  begin
@@ -123,7 +123,7 @@ module CookieJar
123
123
  #
124
124
  # @param [Cookie] cookie a pre-existing cookie object
125
125
  # @return [Cookie] the cookie added to the store
126
- def add_cookie cookie
126
+ def add_cookie(cookie)
127
127
  domain_paths = find_or_add_domain_for_cookie cookie
128
128
  add_cookie_to_path domain_paths, cookie
129
129
  cookie
@@ -149,7 +149,7 @@ module CookieJar
149
149
  # @param [Array] a options controlling output JSON text
150
150
  # (usually a State and a depth)
151
151
  # @return [String] JSON representation of object data
152
- def to_json *a
152
+ def to_json(*a)
153
153
  {
154
154
  'json_class' => self.class.name,
155
155
  'cookies' => to_a.to_json(*a)
@@ -160,17 +160,13 @@ module CookieJar
160
160
  #
161
161
  # @param o [Hash] the expanded JSON object
162
162
  # @return [CookieJar] a new CookieJar instance
163
- def self.json_create o
164
- if o.is_a? String
165
- o = JSON.parse(o)
166
- end
167
- if o.is_a? Hash
168
- o = o['cookies']
169
- end
163
+ def self.json_create(o)
164
+ o = JSON.parse(o) if o.is_a? String
165
+ o = o['cookies'] if o.is_a? Hash
170
166
  cookies = o.inject([]) do |result, cookie_json|
171
167
  result << (Cookie.json_create cookie_json)
172
168
  end
173
- self.from_a cookies
169
+ from_a cookies
174
170
  end
175
171
 
176
172
  # Create a new Jar from an array of Cookie objects. Expired cookies
@@ -179,7 +175,7 @@ module CookieJar
179
175
  #
180
176
  # @param [Array<Cookie>] cookies array of cookie objects
181
177
  # @return [CookieJar] a new CookieJar instance
182
- def self.from_a cookies
178
+ def self.from_a(cookies)
183
179
  jar = new
184
180
  cookies.each do |cookie|
185
181
  jar.add_cookie cookie
@@ -192,10 +188,10 @@ module CookieJar
192
188
  #
193
189
  # @param session [Boolean] whether session cookies should be expired,
194
190
  # or just cookies past their expiration date.
195
- def expire_cookies session = false
196
- @domains.delete_if do |domain, paths|
197
- paths.delete_if do |path, cookies|
198
- cookies.delete_if do |cookie_name, cookie|
191
+ def expire_cookies(session = false)
192
+ @domains.delete_if do |_domain, paths|
193
+ paths.delete_if do |_path, cookies|
194
+ cookies.delete_if do |_cookie_name, cookie|
199
195
  cookie.expired? || (session && cookie.session?)
200
196
  end
201
197
  cookies.empty?
@@ -209,27 +205,36 @@ module CookieJar
209
205
  # otherwise unordered.
210
206
  #
211
207
  # @param [String, URI] request_uri the address the HTTP request will be
212
- # sent to
208
+ # sent to. This must be a full URI, i.e. must include the protocol,
209
+ # if you pass digi.ninja it will fail to find the domain, you must pass
210
+ # http://digi.ninja
213
211
  # @param [Hash] opts options controlling returned cookies
214
- # @option opts [Boolean] :script (false) Cookies marked HTTP-only will be ignored
215
- # if true
212
+ # @option opts [Boolean] :script (false) Cookies marked HTTP-only will be
213
+ # ignored if true
216
214
  # @return [Array<Cookie>] cookies which should be sent in the HTTP request
217
- def get_cookies request_uri, opts = { }
215
+ def get_cookies(request_uri, opts = {})
218
216
  uri = to_uri request_uri
219
217
  hosts = Cookie.compute_search_domains uri
220
218
 
219
+ return [] if hosts.nil?
220
+
221
+ path = if uri.path == ''
222
+ '/'
223
+ else
224
+ uri.path
225
+ end
226
+
221
227
  results = []
222
228
  hosts.each do |host|
223
229
  domain = find_domain host
224
- domain.each do |path, cookies|
225
- if uri.path.start_with? path
226
- results += cookies.values.select do |cookie|
227
- cookie.should_send? uri, opts[:script]
228
- end
230
+ domain.each do |apath, cookies|
231
+ next unless path.start_with? apath
232
+ results += cookies.values.select do |cookie|
233
+ cookie.should_send? uri, opts[:script]
229
234
  end
230
235
  end
231
236
  end
232
- #Sort by path length, longest first
237
+ # Sort by path length, longest first
233
238
  results.sort do |lhs, rhs|
234
239
  rhs.path.length <=> lhs.path.length
235
240
  end
@@ -242,22 +247,19 @@ module CookieJar
242
247
  # @param [String, URI] request_uri the address the HTTP request will be
243
248
  # sent to
244
249
  # @param [Hash] opts options controlling returned cookies
245
- # @option opts [Boolean] :script (false) Cookies marked HTTP-only will be ignored
246
- # if true
250
+ # @option opts [Boolean] :script (false) Cookies marked HTTP-only will be
251
+ # ignored if true
247
252
  # @return String value of the Cookie header which should be sent on the
248
253
  # HTTP request
249
- def get_cookie_header request_uri, opts = { }
254
+ def get_cookie_header(request_uri, opts = {})
250
255
  cookies = get_cookies request_uri, opts
251
- version = 0
252
- ver = [[],[]]
256
+ ver = [[], []]
253
257
  cookies.each do |cookie|
254
258
  ver[cookie.version] << cookie
255
259
  end
256
- if (ver[1].empty?)
260
+ if ver[1].empty?
257
261
  # can do a netscape-style cookie header, relish the opportunity
258
- cookies.map do |cookie|
259
- cookie.to_s
260
- end.join ";"
262
+ cookies.map(&:to_s).join ';'
261
263
  else
262
264
  # build a RFC 2965-style cookie header. Split the cookies into
263
265
  # version 0 and 1 groups so that we can reuse the '$Version' header
@@ -265,46 +267,46 @@ module CookieJar
265
267
  unless ver[0].empty?
266
268
  result << '$Version=0;'
267
269
  result << ver[0].map do |cookie|
268
- (cookie.to_s 1,false)
270
+ (cookie.to_s 1, false)
269
271
  end.join(';')
270
272
  # separate version 0 and 1 with a comma
271
273
  result << ','
272
274
  end
273
275
  result << '$Version=1;'
274
276
  ver[1].map do |cookie|
275
- result << (cookie.to_s 1,false)
277
+ result << (cookie.to_s 1, false)
276
278
  end
277
279
  result
278
280
  end
279
281
  end
280
282
 
281
- protected
283
+ protected
282
284
 
283
- def gather_header_values http_header_value, &block
285
+ def gather_header_values(http_header_value, &_block)
284
286
  result = []
285
287
  if http_header_value.is_a? Array
286
288
  http_header_value.each do |value|
287
- result << block.call(value)
289
+ result << yield(value)
288
290
  end
289
291
  elsif http_header_value.is_a? String
290
- result << block.call(http_header_value)
292
+ result << yield(http_header_value)
291
293
  end
292
294
  result.compact
293
295
  end
294
296
 
295
- def to_uri request_uri
296
- (request_uri.is_a? URI)? request_uri : (URI.parse request_uri)
297
+ def to_uri(request_uri)
298
+ (request_uri.is_a? URI) ? request_uri : (URI.parse request_uri)
297
299
  end
298
300
 
299
- def find_domain host
301
+ def find_domain(host)
300
302
  @domains[host] || {}
301
303
  end
302
304
 
303
- def find_or_add_domain_for_cookie cookie
305
+ def find_or_add_domain_for_cookie(cookie)
304
306
  @domains[cookie.domain] ||= {}
305
307
  end
306
308
 
307
- def add_cookie_to_path paths, cookie
309
+ def add_cookie_to_path(paths, cookie)
308
310
  path_entry = (paths[cookie.path] ||= {})
309
311
  path_entry[cookie.name] = cookie
310
312
  end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module CookieJar
3
+ VERSION = '0.3.3'.freeze
4
+ end
@@ -1,176 +1,176 @@
1
- require 'cookiejar'
2
- require 'rubygems'
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
3
 
4
4
  include CookieJar
5
5
 
6
- FOO_URL = 'http://localhost/foo'
7
- AMMO_URL = 'http://localhost/ammo'
6
+ FOO_URL = 'http://localhost/foo'.freeze
7
+ AMMO_URL = 'http://localhost/ammo'.freeze
8
8
  NETSCAPE_SPEC_SET_COOKIE_HEADERS =
9
- [['CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT',
9
+ [['CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT',
10
10
  FOO_URL],
11
- ['PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
11
+ ['PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
12
12
  FOO_URL],
13
- ['SHIPPING=FEDEX; path=/foo',
13
+ ['SHIPPING=FEDEX; path=/foo',
14
14
  FOO_URL],
15
- ['PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
15
+ ['PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
16
16
  FOO_URL],
17
- ['PART_NUMBER=RIDING_ROCKET_0023; path=/ammo',
18
- AMMO_URL]]
17
+ ['PART_NUMBER=RIDING_ROCKET_0023; path=/ammo',
18
+ AMMO_URL]].freeze
19
19
 
20
20
  describe Cookie do
21
- describe "#from_set_cookie" do
22
- it "should handle cookies from the netscape spec" do
21
+ describe '#from_set_cookie' do
22
+ it 'should handle cookies from the netscape spec' do
23
23
  NETSCAPE_SPEC_SET_COOKIE_HEADERS.each do |value|
24
24
  header, url = *value
25
- cookie = Cookie.from_set_cookie url, header
25
+ Cookie.from_set_cookie url, header
26
26
  end
27
27
  end
28
- it "should give back the input names and values" do
28
+ it 'should give back the input names and values' do
29
29
  cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=bar'
30
- cookie.name.should == 'foo'
31
- cookie.value.should == 'bar'
30
+ expect(cookie.name).to eq 'foo'
31
+ expect(cookie.value).to eq 'bar'
32
32
  end
33
- it "should normalize domain names" do
33
+ it 'should normalize domain names' do
34
34
  cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=Bar;domain=LoCaLHoSt.local'
35
- cookie.domain.should == '.localhost.local'
35
+ expect(cookie.domain).to eq '.localhost.local'
36
36
  end
37
- it "should accept non-normalized .local" do
37
+ it 'should accept non-normalized .local' do
38
38
  cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=bar;domain=.local'
39
- cookie.domain.should == '.local'
39
+ expect(cookie.domain).to eq '.local'
40
40
  end
41
- it "should accept secure cookies" do
41
+ it 'should accept secure cookies' do
42
42
  cookie = Cookie.from_set_cookie 'https://www.google.com/a/blah', 'GALX=RgmSftjnbPM;Path=/a/;Secure'
43
- cookie.name.should == 'GALX'
44
- cookie.secure.should be_true
43
+ expect(cookie.name).to eq 'GALX'
44
+ expect(cookie.secure).to be_truthy
45
45
  end
46
46
  end
47
- describe "#from_set_cookie2" do
48
- it "should give back the input names and values" do
47
+ describe '#from_set_cookie2' do
48
+ it 'should give back the input names and values' do
49
49
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'foo=bar;Version=1'
50
- cookie.name.should == 'foo'
51
- cookie.value.should == 'bar'
50
+ expect(cookie.name).to eq 'foo'
51
+ expect(cookie.value).to eq 'bar'
52
52
  end
53
- it "should normalize domain names" do
53
+ it 'should normalize domain names' do
54
54
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'foo=Bar;domain=LoCaLHoSt.local;Version=1'
55
- cookie.domain.should == '.localhost.local'
55
+ expect(cookie.domain).to eq '.localhost.local'
56
56
  end
57
- it "should accept non-normalized .local" do
57
+ it 'should accept non-normalized .local' do
58
58
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'foo=bar;domain=.local;Version=1'
59
- cookie.domain.should == '.local'
59
+ expect(cookie.domain).to eq '.local'
60
60
  end
61
- it "should accept secure cookies" do
61
+ it 'should accept secure cookies' do
62
62
  cookie = Cookie.from_set_cookie2 'https://www.google.com/a/blah', 'GALX=RgmSftjnbPM;Path="/a/";Secure;Version=1'
63
- cookie.name.should == 'GALX'
64
- cookie.path.should == '/a/'
65
- cookie.secure.should be_true
63
+ expect(cookie.name).to eq 'GALX'
64
+ expect(cookie.path).to eq '/a/'
65
+ expect(cookie.secure).to be_truthy
66
66
  end
67
- it "should fail on unquoted paths" do
68
- lambda do
69
- Cookie.from_set_cookie2 'https://www.google.com/a/blah',
70
- 'GALX=RgmSftjnbPM;Path=/a/;Secure;Version=1'
71
- end.should raise_error InvalidCookieError
67
+ it 'should fail on unquoted paths' do
68
+ expect(lambda do
69
+ Cookie.from_set_cookie2 'https://www.google.com/a/blah',
70
+ 'GALX=RgmSftjnbPM;Path=/a/;Secure;Version=1'
71
+ end).to raise_error InvalidCookieError
72
72
  end
73
- it "should accept quoted values" do
73
+ it 'should accept quoted values' do
74
74
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'foo="bar";Version=1'
75
- cookie.name.should == 'foo'
76
- cookie.value.should == '"bar"'
75
+ expect(cookie.name).to eq 'foo'
76
+ expect(cookie.value).to eq '"bar"'
77
77
  end
78
- it "should accept poorly chosen names" do
78
+ it 'should accept poorly chosen names' do
79
79
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'Version=mine;Version=1'
80
- cookie.name.should == 'Version'
81
- cookie.value.should == 'mine'
80
+ expect(cookie.name).to eq 'Version'
81
+ expect(cookie.value).to eq 'mine'
82
82
  end
83
- it "should accept quoted parameter values" do
84
- cookie = Cookie.from_set_cookie2 'http://localhost/', 'foo=bar;Version="1"'
83
+ it 'should accept quoted parameter values' do
84
+ Cookie.from_set_cookie2 'http://localhost/', 'foo=bar;Version="1"'
85
85
  end
86
- it "should honor the discard and max-age parameters" do
86
+ it 'should honor the discard and max-age parameters' do
87
87
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;max-age=100;discard;Version=1'
88
- cookie.should be_session
89
- cookie.should_not be_expired
90
-
88
+ expect(cookie).to be_session
89
+ expect(cookie).to_not be_expired
90
+
91
91
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;max-age=100;Version=1'
92
- cookie.should_not be_session
92
+ expect(cookie).to_not be_session
93
93
 
94
94
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;Version=1'
95
- cookie.should be_session
95
+ expect(cookie).to be_session
96
96
  end
97
- it "should handle quotable quotes" do
97
+ it 'should handle quotable quotes' do
98
98
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f="\"";Version=1'
99
- cookie.value.should eql '"\""'
99
+ expect(cookie.value).to eq '"\""'
100
100
  end
101
- it "should handle quotable apostrophes" do
101
+ it 'should handle quotable apostrophes' do
102
102
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f="\;";Version=1'
103
- cookie.value.should eql '"\;"'
103
+ expect(cookie.value).to eq '"\;"'
104
104
  end
105
105
  end
106
106
  describe '#decoded_value' do
107
- it "should leave normal values alone" do
107
+ it 'should leave normal values alone' do
108
108
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;Version=1'
109
- cookie.decoded_value.should eql 'b'
109
+ expect(cookie.decoded_value).to eq 'b'
110
110
  end
111
- it "should attempt to unencode quoted values" do
111
+ it 'should attempt to unencode quoted values' do
112
112
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f="\"b";Version=1'
113
- cookie.value.should eql '"\"b"'
114
- cookie.decoded_value.should eql '"b'
113
+ expect(cookie.value).to eq '"\"b"'
114
+ expect(cookie.decoded_value).to eq '"b'
115
115
  end
116
116
  end
117
117
  describe '#to_s' do
118
- it "should handle a simple cookie" do
118
+ it 'should handle a simple cookie' do
119
119
  cookie = Cookie.from_set_cookie 'http://localhost/', 'f=b'
120
- cookie.to_s.should == 'f=b'
121
- cookie.to_s(1).should == '$Version=0;f=b;$Path="/"'
120
+ expect(cookie.to_s).to eq 'f=b'
121
+ expect(cookie.to_s(1)).to eq '$Version=0;f=b;$Path="/"'
122
122
  end
123
- it "should report an explicit domain" do
123
+ it 'should report an explicit domain' do
124
124
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;Version=1;Domain=.local'
125
- cookie.to_s(1).should == '$Version=1;f=b;$Path="/";$Domain=.local'
125
+ expect(cookie.to_s(1)).to eq '$Version=1;f=b;$Path="/";$Domain=.local'
126
126
  end
127
- it "should return specified ports" do
127
+ it 'should return specified ports' do
128
128
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;Version=1;Port="80,443"'
129
- cookie.to_s(1).should == '$Version=1;f=b;$Path="/";$Port="80,443"'
129
+ expect(cookie.to_s(1)).to eq '$Version=1;f=b;$Path="/";$Port="80,443"'
130
130
  end
131
- it "should handle specified paths" do
131
+ it 'should handle specified paths' do
132
132
  cookie = Cookie.from_set_cookie 'http://localhost/bar/', 'f=b;path=/bar/'
133
- cookie.to_s.should == 'f=b'
134
- cookie.to_s(1).should == '$Version=0;f=b;$Path="/bar/"'
133
+ expect(cookie.to_s).to eq 'f=b'
134
+ expect(cookie.to_s(1)).to eq '$Version=0;f=b;$Path="/bar/"'
135
135
  end
136
- it "should omit $Version header when asked" do
136
+ it 'should omit $Version header when asked' do
137
137
  cookie = Cookie.from_set_cookie 'http://localhost/', 'f=b'
138
- cookie.to_s(1,false).should == 'f=b;$Path="/"'
138
+ expect(cookie.to_s(1, false)).to eq 'f=b;$Path="/"'
139
139
  end
140
140
  end
141
141
  describe '#should_send?' do
142
- it "should not send if ports do not match" do
142
+ it 'should not send if ports do not match' do
143
143
  cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;Version=1;Port="80"'
144
- cookie.should_send?("http://localhost/", false).should be_true
145
- cookie.should_send?("https://localhost/", false).should be_false
144
+ expect(cookie.should_send?('http://localhost/', false)).to be_truthy
145
+ expect(cookie.should_send?('https://localhost/', false)).to be_falsey
146
146
  end
147
147
  end
148
148
  begin
149
149
  require 'json'
150
- describe ".to_json" do
151
- it "should serialize a cookie to JSON" do
150
+ describe '.to_json' do
151
+ it 'should serialize a cookie to JSON' do
152
152
  c = Cookie.from_set_cookie 'https://localhost/', 'foo=bar;secure;expires=Fri, September 11 2009 18:10:00 -0700'
153
153
  json = c.to_json
154
- json.should be_a String
154
+ expect(json).to be_a String
155
155
  end
156
156
  end
157
- describe ".json_create" do
158
- it "should deserialize JSON to a cookie" do
159
- json = "{\"name\":\"foo\",\"value\":\"bar\",\"domain\":\"localhost.local\",\"path\":\"\\/\",\"created_at\":\"2009-09-11 12:51:03 -0600\",\"expiry\":\"2009-09-11 19:10:00 -0600\",\"secure\":true}"
157
+ describe '.json_create' do
158
+ it 'should deserialize JSON to a cookie' do
159
+ json = '{"name":"foo","value":"bar","domain":"localhost.local","path":"\\/","created_at":"2009-09-11 12:51:03 -0600","expiry":"2009-09-11 19:10:00 -0600","secure":true}'
160
160
  hash = JSON.parse json
161
161
  c = Cookie.json_create hash
162
162
  CookieValidation.validate_cookie 'https://localhost/', c
163
163
  end
164
- it "should automatically deserialize to a cookie" do
165
- json = "{\"json_class\":\"CookieJar::Cookie\",\"name\":\"foo\",\"value\":\"bar\",\"domain\":\"localhost.local\",\"path\":\"\\/\",\"created_at\":\"2009-09-11 12:51:03 -0600\",\"expiry\":\"2009-09-11 19:10:00 -0600\",\"secure\":true}"
166
- c = JSON.parse json, :create_additions => true
167
- c.should be_a Cookie
164
+ it 'should automatically deserialize to a cookie' do
165
+ json = '{"json_class":"CookieJar::Cookie","name":"foo","value":"bar","domain":"localhost.local","path":"\\/","created_at":"2009-09-11 12:51:03 -0600","expiry":"2009-09-11 19:10:00 -0600","secure":true}'
166
+ c = JSON.parse json, create_additions: true
167
+ expect(c).to be_a Cookie
168
168
  CookieValidation.validate_cookie 'https://localhost/', c
169
169
  end
170
170
  end
171
171
  rescue LoadError
172
- it "does not appear the JSON library is installed" do
173
- raise "please install the JSON library"
172
+ it 'does not appear the JSON library is installed' do
173
+ raise 'please install the JSON library'
174
174
  end
175
175
  end
176
176
  end