cookiejar 0.3.2 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/.travis.yml +17 -0
- data/Gemfile +2 -0
- data/LICENSE +1 -1
- data/README.markdown +20 -1
- data/Rakefile +11 -12
- data/_config.yml +1 -0
- data/cookiejar.gemspec +28 -0
- data/lib/cookiejar/cookie.rb +57 -63
- data/lib/cookiejar/cookie_validation.rb +105 -100
- data/lib/cookiejar/jar.rb +51 -49
- data/lib/cookiejar/version.rb +4 -0
- data/lib/cookiejar.rb +2 -1
- data/spec/cookie_spec.rb +90 -90
- data/spec/cookie_validation_spec.rb +147 -155
- data/spec/jar_spec.rb +107 -110
- data/spec/spec_helper.rb +5 -0
- metadata +55 -22
data/lib/cookiejar.rb
CHANGED
data/spec/cookie_spec.rb
CHANGED
@@ -1,176 +1,176 @@
|
|
1
|
-
|
2
|
-
require '
|
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
|
-
|
11
|
+
['PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
|
12
12
|
FOO_URL],
|
13
|
-
|
13
|
+
['SHIPPING=FEDEX; path=/foo',
|
14
14
|
FOO_URL],
|
15
|
-
|
15
|
+
['PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
|
16
16
|
FOO_URL],
|
17
|
-
|
18
|
-
AMMO_URL]]
|
17
|
+
['PART_NUMBER=RIDING_ROCKET_0023; path=/ammo',
|
18
|
+
AMMO_URL]].freeze
|
19
19
|
|
20
20
|
describe Cookie do
|
21
|
-
describe
|
22
|
-
it
|
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
|
-
|
25
|
+
Cookie.from_set_cookie url, header
|
26
26
|
end
|
27
27
|
end
|
28
|
-
it
|
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.
|
31
|
-
cookie.value.
|
30
|
+
expect(cookie.name).to eq 'foo'
|
31
|
+
expect(cookie.value).to eq 'bar'
|
32
32
|
end
|
33
|
-
it
|
33
|
+
it 'should normalize domain names' do
|
34
34
|
cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=Bar;domain=LoCaLHoSt.local'
|
35
|
-
cookie.domain.
|
35
|
+
expect(cookie.domain).to eq '.localhost.local'
|
36
36
|
end
|
37
|
-
it
|
37
|
+
it 'should accept non-normalized .local' do
|
38
38
|
cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=bar;domain=.local'
|
39
|
-
cookie.domain.
|
39
|
+
expect(cookie.domain).to eq '.local'
|
40
40
|
end
|
41
|
-
it
|
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.
|
44
|
-
cookie.secure.
|
43
|
+
expect(cookie.name).to eq 'GALX'
|
44
|
+
expect(cookie.secure).to be_truthy
|
45
45
|
end
|
46
46
|
end
|
47
|
-
describe
|
48
|
-
it
|
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.
|
51
|
-
cookie.value.
|
50
|
+
expect(cookie.name).to eq 'foo'
|
51
|
+
expect(cookie.value).to eq 'bar'
|
52
52
|
end
|
53
|
-
it
|
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.
|
55
|
+
expect(cookie.domain).to eq '.localhost.local'
|
56
56
|
end
|
57
|
-
it
|
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.
|
59
|
+
expect(cookie.domain).to eq '.local'
|
60
60
|
end
|
61
|
-
it
|
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.
|
64
|
-
cookie.path.
|
65
|
-
cookie.secure.
|
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
|
68
|
-
|
69
|
-
Cookie.from_set_cookie2 'https://www.google.com/a/blah',
|
70
|
-
|
71
|
-
|
67
|
+
it 'should fail on unquoted paths' do
|
68
|
+
expect {
|
69
|
+
Cookie.from_set_cookie2 'https://www.google.com/a/blah',
|
70
|
+
'GALX=RgmSftjnbPM;Path=/a/;Secure;Version=1'
|
71
|
+
}.to raise_error InvalidCookieError
|
72
72
|
end
|
73
|
-
it
|
73
|
+
it 'should accept quoted values' do
|
74
74
|
cookie = Cookie.from_set_cookie2 'http://localhost/', 'foo="bar";Version=1'
|
75
|
-
cookie.name.
|
76
|
-
cookie.value.
|
75
|
+
expect(cookie.name).to eq 'foo'
|
76
|
+
expect(cookie.value).to eq '"bar"'
|
77
77
|
end
|
78
|
-
it
|
78
|
+
it 'should accept poorly chosen names' do
|
79
79
|
cookie = Cookie.from_set_cookie2 'http://localhost/', 'Version=mine;Version=1'
|
80
|
-
cookie.name.
|
81
|
-
cookie.value.
|
80
|
+
expect(cookie.name).to eq 'Version'
|
81
|
+
expect(cookie.value).to eq 'mine'
|
82
82
|
end
|
83
|
-
it
|
84
|
-
|
83
|
+
it 'should accept quoted parameter values' do
|
84
|
+
Cookie.from_set_cookie2 'http://localhost/', 'foo=bar;Version="1"'
|
85
85
|
end
|
86
|
-
it
|
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.
|
89
|
-
cookie.
|
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.
|
92
|
+
expect(cookie).to_not be_session
|
93
93
|
|
94
94
|
cookie = Cookie.from_set_cookie2 'http://localhost/', 'f=b;Version=1'
|
95
|
-
cookie.
|
95
|
+
expect(cookie).to be_session
|
96
96
|
end
|
97
|
-
it
|
97
|
+
it 'should handle quotable quotes' do
|
98
98
|
cookie = Cookie.from_set_cookie2 'http://localhost/', 'f="\"";Version=1'
|
99
|
-
cookie.value.
|
99
|
+
expect(cookie.value).to eq '"\""'
|
100
100
|
end
|
101
|
-
it
|
101
|
+
it 'should handle quotable apostrophes' do
|
102
102
|
cookie = Cookie.from_set_cookie2 'http://localhost/', 'f="\;";Version=1'
|
103
|
-
cookie.value.
|
103
|
+
expect(cookie.value).to eq '"\;"'
|
104
104
|
end
|
105
105
|
end
|
106
106
|
describe '#decoded_value' do
|
107
|
-
it
|
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.
|
109
|
+
expect(cookie.decoded_value).to eq 'b'
|
110
110
|
end
|
111
|
-
it
|
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.
|
114
|
-
cookie.decoded_value.
|
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
|
118
|
+
it 'should handle a simple cookie' do
|
119
119
|
cookie = Cookie.from_set_cookie 'http://localhost/', 'f=b'
|
120
|
-
cookie.to_s.
|
121
|
-
cookie.to_s(1).
|
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
|
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).
|
125
|
+
expect(cookie.to_s(1)).to eq '$Version=1;f=b;$Path="/";$Domain=.local'
|
126
126
|
end
|
127
|
-
it
|
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).
|
129
|
+
expect(cookie.to_s(1)).to eq '$Version=1;f=b;$Path="/";$Port="80,443"'
|
130
130
|
end
|
131
|
-
it
|
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.
|
134
|
-
cookie.to_s(1).
|
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
|
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).
|
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
|
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?(
|
145
|
-
cookie.should_send?(
|
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
|
151
|
-
it
|
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.
|
154
|
+
expect(json).to be_a String
|
155
155
|
end
|
156
156
|
end
|
157
|
-
describe
|
158
|
-
it
|
159
|
-
json =
|
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
|
165
|
-
json =
|
166
|
-
c = JSON.parse json, :
|
167
|
-
c.
|
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
|
173
|
-
raise
|
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
|