diamond-mechanize 2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +718 -0
- data/EXAMPLES.rdoc +187 -0
- data/FAQ.rdoc +11 -0
- data/GUIDE.rdoc +163 -0
- data/LICENSE.rdoc +20 -0
- data/Manifest.txt +159 -0
- data/README.rdoc +64 -0
- data/Rakefile +49 -0
- data/lib/mechanize.rb +1079 -0
- data/lib/mechanize/content_type_error.rb +13 -0
- data/lib/mechanize/cookie.rb +232 -0
- data/lib/mechanize/cookie_jar.rb +194 -0
- data/lib/mechanize/download.rb +59 -0
- data/lib/mechanize/element_matcher.rb +36 -0
- data/lib/mechanize/file.rb +65 -0
- data/lib/mechanize/file_connection.rb +17 -0
- data/lib/mechanize/file_request.rb +26 -0
- data/lib/mechanize/file_response.rb +74 -0
- data/lib/mechanize/file_saver.rb +39 -0
- data/lib/mechanize/form.rb +543 -0
- data/lib/mechanize/form/button.rb +6 -0
- data/lib/mechanize/form/check_box.rb +12 -0
- data/lib/mechanize/form/field.rb +54 -0
- data/lib/mechanize/form/file_upload.rb +21 -0
- data/lib/mechanize/form/hidden.rb +3 -0
- data/lib/mechanize/form/image_button.rb +19 -0
- data/lib/mechanize/form/keygen.rb +34 -0
- data/lib/mechanize/form/multi_select_list.rb +94 -0
- data/lib/mechanize/form/option.rb +50 -0
- data/lib/mechanize/form/radio_button.rb +55 -0
- data/lib/mechanize/form/reset.rb +3 -0
- data/lib/mechanize/form/select_list.rb +44 -0
- data/lib/mechanize/form/submit.rb +3 -0
- data/lib/mechanize/form/text.rb +3 -0
- data/lib/mechanize/form/textarea.rb +3 -0
- data/lib/mechanize/headers.rb +23 -0
- data/lib/mechanize/history.rb +82 -0
- data/lib/mechanize/http.rb +8 -0
- data/lib/mechanize/http/agent.rb +1004 -0
- data/lib/mechanize/http/auth_challenge.rb +59 -0
- data/lib/mechanize/http/auth_realm.rb +31 -0
- data/lib/mechanize/http/content_disposition_parser.rb +188 -0
- data/lib/mechanize/http/www_authenticate_parser.rb +155 -0
- data/lib/mechanize/monkey_patch.rb +16 -0
- data/lib/mechanize/page.rb +440 -0
- data/lib/mechanize/page/base.rb +7 -0
- data/lib/mechanize/page/frame.rb +27 -0
- data/lib/mechanize/page/image.rb +30 -0
- data/lib/mechanize/page/label.rb +20 -0
- data/lib/mechanize/page/link.rb +98 -0
- data/lib/mechanize/page/meta_refresh.rb +68 -0
- data/lib/mechanize/parser.rb +173 -0
- data/lib/mechanize/pluggable_parsers.rb +144 -0
- data/lib/mechanize/redirect_limit_reached_error.rb +19 -0
- data/lib/mechanize/redirect_not_get_or_head_error.rb +21 -0
- data/lib/mechanize/response_code_error.rb +21 -0
- data/lib/mechanize/response_read_error.rb +27 -0
- data/lib/mechanize/robots_disallowed_error.rb +28 -0
- data/lib/mechanize/test_case.rb +663 -0
- data/lib/mechanize/unauthorized_error.rb +3 -0
- data/lib/mechanize/unsupported_scheme_error.rb +6 -0
- data/lib/mechanize/util.rb +101 -0
- data/test/data/htpasswd +1 -0
- data/test/data/server.crt +16 -0
- data/test/data/server.csr +12 -0
- data/test/data/server.key +15 -0
- data/test/data/server.pem +15 -0
- data/test/htdocs/alt_text.html +10 -0
- data/test/htdocs/bad_form_test.html +9 -0
- data/test/htdocs/button.jpg +0 -0
- data/test/htdocs/canonical_uri.html +9 -0
- data/test/htdocs/dir with spaces/foo.html +1 -0
- data/test/htdocs/empty_form.html +6 -0
- data/test/htdocs/file_upload.html +26 -0
- data/test/htdocs/find_link.html +41 -0
- data/test/htdocs/form_multi_select.html +16 -0
- data/test/htdocs/form_multival.html +37 -0
- data/test/htdocs/form_no_action.html +18 -0
- data/test/htdocs/form_no_input_name.html +16 -0
- data/test/htdocs/form_order_test.html +11 -0
- data/test/htdocs/form_select.html +16 -0
- data/test/htdocs/form_set_fields.html +14 -0
- data/test/htdocs/form_test.html +188 -0
- data/test/htdocs/frame_referer_test.html +10 -0
- data/test/htdocs/frame_test.html +30 -0
- data/test/htdocs/google.html +13 -0
- data/test/htdocs/index.html +6 -0
- data/test/htdocs/link with space.html +5 -0
- data/test/htdocs/meta_cookie.html +11 -0
- data/test/htdocs/no_title_test.html +6 -0
- data/test/htdocs/noindex.html +9 -0
- data/test/htdocs/rails_3_encoding_hack_form_test.html +27 -0
- data/test/htdocs/relative/tc_relative_links.html +21 -0
- data/test/htdocs/robots.html +8 -0
- data/test/htdocs/robots.txt +2 -0
- data/test/htdocs/tc_bad_charset.html +9 -0
- data/test/htdocs/tc_bad_links.html +5 -0
- data/test/htdocs/tc_base_link.html +8 -0
- data/test/htdocs/tc_blank_form.html +11 -0
- data/test/htdocs/tc_charset.html +6 -0
- data/test/htdocs/tc_checkboxes.html +19 -0
- data/test/htdocs/tc_encoded_links.html +5 -0
- data/test/htdocs/tc_field_precedence.html +11 -0
- data/test/htdocs/tc_follow_meta.html +8 -0
- data/test/htdocs/tc_form_action.html +48 -0
- data/test/htdocs/tc_links.html +19 -0
- data/test/htdocs/tc_meta_in_body.html +9 -0
- data/test/htdocs/tc_pretty_print.html +17 -0
- data/test/htdocs/tc_referer.html +16 -0
- data/test/htdocs/tc_relative_links.html +19 -0
- data/test/htdocs/tc_textarea.html +23 -0
- data/test/htdocs/test_click.html +11 -0
- data/test/htdocs/unusual______.html +5 -0
- data/test/test_mechanize.rb +1164 -0
- data/test/test_mechanize_cookie.rb +451 -0
- data/test/test_mechanize_cookie_jar.rb +483 -0
- data/test/test_mechanize_download.rb +43 -0
- data/test/test_mechanize_file.rb +61 -0
- data/test/test_mechanize_file_connection.rb +21 -0
- data/test/test_mechanize_file_request.rb +19 -0
- data/test/test_mechanize_file_saver.rb +21 -0
- data/test/test_mechanize_form.rb +875 -0
- data/test/test_mechanize_form_check_box.rb +38 -0
- data/test/test_mechanize_form_encoding.rb +114 -0
- data/test/test_mechanize_form_field.rb +63 -0
- data/test/test_mechanize_form_file_upload.rb +20 -0
- data/test/test_mechanize_form_image_button.rb +12 -0
- data/test/test_mechanize_form_keygen.rb +32 -0
- data/test/test_mechanize_form_multi_select_list.rb +84 -0
- data/test/test_mechanize_form_option.rb +55 -0
- data/test/test_mechanize_form_radio_button.rb +78 -0
- data/test/test_mechanize_form_select_list.rb +76 -0
- data/test/test_mechanize_form_textarea.rb +52 -0
- data/test/test_mechanize_headers.rb +35 -0
- data/test/test_mechanize_history.rb +103 -0
- data/test/test_mechanize_http_agent.rb +1225 -0
- data/test/test_mechanize_http_auth_challenge.rb +39 -0
- data/test/test_mechanize_http_auth_realm.rb +49 -0
- data/test/test_mechanize_http_content_disposition_parser.rb +118 -0
- data/test/test_mechanize_http_www_authenticate_parser.rb +146 -0
- data/test/test_mechanize_link.rb +80 -0
- data/test/test_mechanize_page.rb +118 -0
- data/test/test_mechanize_page_encoding.rb +182 -0
- data/test/test_mechanize_page_frame.rb +16 -0
- data/test/test_mechanize_page_link.rb +390 -0
- data/test/test_mechanize_page_meta_refresh.rb +127 -0
- data/test/test_mechanize_parser.rb +289 -0
- data/test/test_mechanize_pluggable_parser.rb +52 -0
- data/test/test_mechanize_redirect_limit_reached_error.rb +24 -0
- data/test/test_mechanize_redirect_not_get_or_head_error.rb +14 -0
- data/test/test_mechanize_subclass.rb +22 -0
- data/test/test_mechanize_util.rb +103 -0
- data/test/test_multi_select.rb +119 -0
- metadata +216 -0
@@ -0,0 +1,451 @@
|
|
1
|
+
require 'mechanize/test_case'
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
def combine
|
5
|
+
masks = inject([[], 1]){|(ar, m), e| [ar << m, m << 1 ] }[0]
|
6
|
+
all = masks.inject(0){ |al, m| al|m }
|
7
|
+
|
8
|
+
result = []
|
9
|
+
for i in 1..all do
|
10
|
+
tmp = []
|
11
|
+
each_with_index do |e, idx|
|
12
|
+
tmp << e unless (masks[idx] & i) == 0
|
13
|
+
end
|
14
|
+
result << tmp
|
15
|
+
end
|
16
|
+
result
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class TestMechanizeCookie < Mechanize::TestCase
|
21
|
+
def silently
|
22
|
+
warn_level = $VERBOSE
|
23
|
+
$VERBOSE = false
|
24
|
+
res = yield
|
25
|
+
$VERBOSE = warn_level
|
26
|
+
res
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_parse_dates
|
30
|
+
url = URI.parse('http://localhost/')
|
31
|
+
|
32
|
+
yesterday = Time.now - 86400
|
33
|
+
|
34
|
+
dates = [ "14 Apr 89 03:20:12",
|
35
|
+
"14 Apr 89 03:20 GMT",
|
36
|
+
"Fri, 17 Mar 89 4:01:33",
|
37
|
+
"Fri, 17 Mar 89 4:01 GMT",
|
38
|
+
"Mon Jan 16 16:12 PDT 1989",
|
39
|
+
"Mon Jan 16 16:12 +0130 1989",
|
40
|
+
"6 May 1992 16:41-JST (Wednesday)",
|
41
|
+
#"22-AUG-1993 10:59:12.82",
|
42
|
+
"22-AUG-1993 10:59pm",
|
43
|
+
"22-AUG-1993 12:59am",
|
44
|
+
"22-AUG-1993 12:59 PM",
|
45
|
+
#"Friday, August 04, 1995 3:54 PM",
|
46
|
+
#"06/21/95 04:24:34 PM",
|
47
|
+
#"20/06/95 21:07",
|
48
|
+
"95-06-08 19:32:48 EDT",
|
49
|
+
]
|
50
|
+
|
51
|
+
dates.each do |date|
|
52
|
+
cookie = "PREF=1; expires=#{date}"
|
53
|
+
silently do
|
54
|
+
Mechanize::Cookie.parse(url, cookie) { |c|
|
55
|
+
assert c.expires, "Tried parsing: #{date}"
|
56
|
+
assert_equal(true, c.expires < yesterday)
|
57
|
+
}
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_parse_empty
|
63
|
+
cookie_str = 'a=b; ; c=d'
|
64
|
+
|
65
|
+
uri = URI.parse 'http://example'
|
66
|
+
|
67
|
+
Mechanize::Cookie.parse uri, cookie_str do |cookie|
|
68
|
+
assert_equal 'a', cookie.name
|
69
|
+
assert_equal 'b', cookie.value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_parse_no_space
|
74
|
+
cookie_str = "foo=bar;Expires=Sun, 06 Nov 2011 00:28:06 GMT;Path=/"
|
75
|
+
|
76
|
+
uri = URI.parse 'http://example'
|
77
|
+
|
78
|
+
Mechanize::Cookie.parse uri, cookie_str do |cookie|
|
79
|
+
assert_equal 'foo', cookie.name
|
80
|
+
assert_equal 'bar', cookie.value
|
81
|
+
assert_equal '/', cookie.path
|
82
|
+
assert_equal Time.at(1320539286), cookie.expires
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_parse_quoted
|
87
|
+
cookie_str =
|
88
|
+
"quoted=\"value\"; Expires=Sun, 06 Nov 2011 00:11:18 GMT; Path=/"
|
89
|
+
|
90
|
+
uri = URI.parse 'http://example'
|
91
|
+
|
92
|
+
Mechanize::Cookie.parse uri, cookie_str do |cookie|
|
93
|
+
assert_equal 'quoted', cookie.name
|
94
|
+
assert_equal '"value"', cookie.value
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_parse_weird_cookie
|
99
|
+
cookie = 'n/a, ASPSESSIONIDCSRRQDQR=FBLDGHPBNDJCPCGNCPAENELB; path=/'
|
100
|
+
url = URI.parse('http://www.searchinnovation.com/')
|
101
|
+
Mechanize::Cookie.parse(url, cookie) { |c|
|
102
|
+
assert_equal('ASPSESSIONIDCSRRQDQR', c.name)
|
103
|
+
assert_equal('FBLDGHPBNDJCPCGNCPAENELB', c.value)
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_double_semicolon
|
108
|
+
double_semi = 'WSIDC=WEST;; domain=.williams-sonoma.com; path=/'
|
109
|
+
url = URI.parse('http://williams-sonoma.com/')
|
110
|
+
Mechanize::Cookie.parse(url, double_semi) { |cookie|
|
111
|
+
assert_equal('WSIDC', cookie.name)
|
112
|
+
assert_equal('WEST', cookie.value)
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_parse_bad_version
|
117
|
+
bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Version=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
|
118
|
+
url = URI.parse('http://localhost/')
|
119
|
+
Mechanize::Cookie.parse(url, bad_cookie) { |cookie|
|
120
|
+
assert_nil(cookie.version)
|
121
|
+
}
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_parse_bad_max_age
|
125
|
+
bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Max-Age=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
|
126
|
+
url = URI.parse('http://localhost/')
|
127
|
+
Mechanize::Cookie.parse(url, bad_cookie) { |cookie|
|
128
|
+
assert_nil(cookie.max_age)
|
129
|
+
}
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_parse_date_fail
|
133
|
+
url = URI.parse('http://localhost/')
|
134
|
+
|
135
|
+
dates = [
|
136
|
+
"20/06/95 21:07",
|
137
|
+
]
|
138
|
+
|
139
|
+
silently do
|
140
|
+
dates.each do |date|
|
141
|
+
cookie = "PREF=1; expires=#{date}"
|
142
|
+
Mechanize::Cookie.parse(url, cookie) { |c|
|
143
|
+
assert_equal(true, c.expires.nil?)
|
144
|
+
}
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_parse_domain_dot
|
150
|
+
url = URI.parse('http://host.example.com/')
|
151
|
+
|
152
|
+
cookie_str = 'a=b; domain=.example.com'
|
153
|
+
|
154
|
+
cookie = Mechanize::Cookie.parse(url, cookie_str).first
|
155
|
+
|
156
|
+
assert_equal 'example.com', cookie.domain
|
157
|
+
assert cookie.for_domain?
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_parse_domain_no_dot
|
161
|
+
url = URI.parse('http://host.example.com/')
|
162
|
+
|
163
|
+
cookie_str = 'a=b; domain=example.com'
|
164
|
+
|
165
|
+
cookie = Mechanize::Cookie.parse(url, cookie_str).first
|
166
|
+
|
167
|
+
assert_equal 'example.com', cookie.domain
|
168
|
+
assert cookie.for_domain?
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_parse_domain_none
|
172
|
+
url = URI.parse('http://example.com/')
|
173
|
+
|
174
|
+
cookie_str = 'a=b;'
|
175
|
+
|
176
|
+
cookie = Mechanize::Cookie.parse(url, cookie_str).first
|
177
|
+
|
178
|
+
assert_equal 'example.com', cookie.domain
|
179
|
+
assert !cookie.for_domain?
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_parse_expires_session
|
183
|
+
cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Max-Age=1.2; Content-type=text/html; Domain=192.168.6.196; expires=;'
|
184
|
+
|
185
|
+
url = URI.parse('http://localhost/')
|
186
|
+
|
187
|
+
cookie = Mechanize::Cookie.parse(url, cookie).first
|
188
|
+
|
189
|
+
assert cookie.session
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_parse_many
|
193
|
+
url = URI 'http://example/'
|
194
|
+
cookie_str =
|
195
|
+
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
196
|
+
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
197
|
+
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
198
|
+
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/; HttpOnly, " \
|
199
|
+
"expired=doh; Expires=Fri, 04 Nov 2011 00:29:51 GMT; Path=/, " \
|
200
|
+
"a_path=some_path; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/some_path, " \
|
201
|
+
"no_path=no_path; Expires=Sun, 06 Nov 2011 00:29:51 GMT, no_expires=nope; Path=/"
|
202
|
+
|
203
|
+
cookies = Mechanize::Cookie.parse url, cookie_str
|
204
|
+
assert_equal 8, cookies.length
|
205
|
+
|
206
|
+
name = cookies.find { |c| c.name == 'name' }
|
207
|
+
assert_equal "Aaron", name.value
|
208
|
+
assert_equal "/", name.path
|
209
|
+
assert_equal Time.at(1320539391), name.expires
|
210
|
+
|
211
|
+
a_path = cookies.find { |c| c.name == 'a_path' }
|
212
|
+
assert_equal "some_path", a_path.value
|
213
|
+
assert_equal "/some_path", a_path.path
|
214
|
+
assert_equal Time.at(1320539391), a_path.expires
|
215
|
+
|
216
|
+
no_expires = cookies.find { |c| c.name == 'no_expires' }
|
217
|
+
assert_equal "nope", no_expires.value
|
218
|
+
assert_equal "/", no_expires.path
|
219
|
+
assert_nil no_expires.expires
|
220
|
+
|
221
|
+
no_path = cookies.find { |c| c.name == 'no_path' }
|
222
|
+
assert_equal "no_path", no_path.value
|
223
|
+
assert_equal "/", no_path.path
|
224
|
+
assert_equal Time.at(1320539391), no_path.expires
|
225
|
+
|
226
|
+
assert cookies.find { |c| c.name == 'expired' }
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_parse_valid_cookie
|
230
|
+
url = URI.parse('http://rubyforge.org/')
|
231
|
+
cookie_params = {}
|
232
|
+
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
|
233
|
+
cookie_params['path'] = 'path=/'
|
234
|
+
cookie_params['domain'] = 'domain=.rubyforge.org'
|
235
|
+
cookie_params['httponly'] = 'HttpOnly'
|
236
|
+
cookie_value = '12345%7D=ASDFWEE345%3DASda'
|
237
|
+
|
238
|
+
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
|
239
|
+
|
240
|
+
cookie_params.keys.combine.each do |c|
|
241
|
+
cookie_text = "#{cookie_value}; "
|
242
|
+
c.each_with_index do |key, idx|
|
243
|
+
if idx == (c.length - 1)
|
244
|
+
cookie_text << "#{cookie_params[key]}"
|
245
|
+
else
|
246
|
+
cookie_text << "#{cookie_params[key]}; "
|
247
|
+
end
|
248
|
+
end
|
249
|
+
cookie = nil
|
250
|
+
Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
|
251
|
+
|
252
|
+
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
|
253
|
+
assert_equal('/', cookie.path)
|
254
|
+
|
255
|
+
# if expires was set, make sure we parsed it
|
256
|
+
if c.find { |k| k == 'expires' }
|
257
|
+
assert_equal(expires, cookie.expires)
|
258
|
+
else
|
259
|
+
assert_nil(cookie.expires)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_parse_valid_cookie_empty_value
|
265
|
+
url = URI.parse('http://rubyforge.org/')
|
266
|
+
cookie_params = {}
|
267
|
+
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
|
268
|
+
cookie_params['path'] = 'path=/'
|
269
|
+
cookie_params['domain'] = 'domain=.rubyforge.org'
|
270
|
+
cookie_params['httponly'] = 'HttpOnly'
|
271
|
+
cookie_value = '12345%7D='
|
272
|
+
|
273
|
+
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
|
274
|
+
|
275
|
+
cookie_params.keys.combine.each do |c|
|
276
|
+
cookie_text = "#{cookie_value}; "
|
277
|
+
c.each_with_index do |key, idx|
|
278
|
+
if idx == (c.length - 1)
|
279
|
+
cookie_text << "#{cookie_params[key]}"
|
280
|
+
else
|
281
|
+
cookie_text << "#{cookie_params[key]}; "
|
282
|
+
end
|
283
|
+
end
|
284
|
+
cookie = nil
|
285
|
+
Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
|
286
|
+
|
287
|
+
assert_equal('12345%7D=', cookie.to_s)
|
288
|
+
assert_equal('', cookie.value)
|
289
|
+
assert_equal('/', cookie.path)
|
290
|
+
|
291
|
+
# if expires was set, make sure we parsed it
|
292
|
+
if c.find { |k| k == 'expires' }
|
293
|
+
assert_equal(expires, cookie.expires)
|
294
|
+
else
|
295
|
+
assert_nil(cookie.expires)
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
# If no path was given, use the one from the URL
|
301
|
+
def test_cookie_using_url_path
|
302
|
+
url = URI.parse('http://rubyforge.org/login.php')
|
303
|
+
cookie_params = {}
|
304
|
+
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
|
305
|
+
cookie_params['path'] = 'path=/'
|
306
|
+
cookie_params['domain'] = 'domain=.rubyforge.org'
|
307
|
+
cookie_params['httponly'] = 'HttpOnly'
|
308
|
+
cookie_value = '12345%7D=ASDFWEE345%3DASda'
|
309
|
+
|
310
|
+
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
|
311
|
+
|
312
|
+
cookie_params.keys.combine.each do |c|
|
313
|
+
next if c.find { |k| k == 'path' }
|
314
|
+
cookie_text = "#{cookie_value}; "
|
315
|
+
c.each_with_index do |key, idx|
|
316
|
+
if idx == (c.length - 1)
|
317
|
+
cookie_text << "#{cookie_params[key]}"
|
318
|
+
else
|
319
|
+
cookie_text << "#{cookie_params[key]}; "
|
320
|
+
end
|
321
|
+
end
|
322
|
+
cookie = nil
|
323
|
+
Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
|
324
|
+
|
325
|
+
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
|
326
|
+
assert_equal('/', cookie.path)
|
327
|
+
|
328
|
+
# if expires was set, make sure we parsed it
|
329
|
+
if c.find { |k| k == 'expires' }
|
330
|
+
assert_equal(expires, cookie.expires)
|
331
|
+
else
|
332
|
+
assert_nil(cookie.expires)
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
# Test using secure cookies
|
338
|
+
def test_cookie_with_secure
|
339
|
+
url = URI.parse('http://rubyforge.org/')
|
340
|
+
cookie_params = {}
|
341
|
+
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
|
342
|
+
cookie_params['path'] = 'path=/'
|
343
|
+
cookie_params['domain'] = 'domain=.rubyforge.org'
|
344
|
+
cookie_params['secure'] = 'secure'
|
345
|
+
cookie_value = '12345%7D=ASDFWEE345%3DASda'
|
346
|
+
|
347
|
+
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
|
348
|
+
|
349
|
+
cookie_params.keys.combine.each do |c|
|
350
|
+
next unless c.find { |k| k == 'secure' }
|
351
|
+
cookie_text = "#{cookie_value}; "
|
352
|
+
c.each_with_index do |key, idx|
|
353
|
+
if idx == (c.length - 1)
|
354
|
+
cookie_text << "#{cookie_params[key]}"
|
355
|
+
else
|
356
|
+
cookie_text << "#{cookie_params[key]}; "
|
357
|
+
end
|
358
|
+
end
|
359
|
+
cookie = nil
|
360
|
+
Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
|
361
|
+
|
362
|
+
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
|
363
|
+
assert_equal('/', cookie.path)
|
364
|
+
assert_equal(true, cookie.secure)
|
365
|
+
|
366
|
+
# if expires was set, make sure we parsed it
|
367
|
+
if c.find { |k| k == 'expires' }
|
368
|
+
assert_equal(expires, cookie.expires)
|
369
|
+
else
|
370
|
+
assert_nil(cookie.expires)
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_parse_cookie_no_spaces
|
376
|
+
url = URI.parse('http://rubyforge.org/')
|
377
|
+
cookie_params = {}
|
378
|
+
cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
|
379
|
+
cookie_params['path'] = 'path=/'
|
380
|
+
cookie_params['domain'] = 'domain=.rubyforge.org'
|
381
|
+
cookie_params['httponly'] = 'HttpOnly'
|
382
|
+
cookie_value = '12345%7D=ASDFWEE345%3DASda'
|
383
|
+
|
384
|
+
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
|
385
|
+
|
386
|
+
cookie_params.keys.combine.each do |c|
|
387
|
+
cookie_text = "#{cookie_value};"
|
388
|
+
c.each_with_index do |key, idx|
|
389
|
+
if idx == (c.length - 1)
|
390
|
+
cookie_text << "#{cookie_params[key]}"
|
391
|
+
else
|
392
|
+
cookie_text << "#{cookie_params[key]};"
|
393
|
+
end
|
394
|
+
end
|
395
|
+
cookie = nil
|
396
|
+
Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
|
397
|
+
|
398
|
+
assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
|
399
|
+
assert_equal('/', cookie.path)
|
400
|
+
|
401
|
+
# if expires was set, make sure we parsed it
|
402
|
+
if c.find { |k| k == 'expires' }
|
403
|
+
assert_equal(expires, cookie.expires)
|
404
|
+
else
|
405
|
+
assert_nil(cookie.expires)
|
406
|
+
end
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_new
|
411
|
+
cookie = Mechanize::Cookie.new('key', 'value')
|
412
|
+
assert_equal 'key', cookie.name
|
413
|
+
assert_equal 'value', cookie.value
|
414
|
+
assert_equal nil, cookie.expires
|
415
|
+
|
416
|
+
# Minimum unit for the expires attribute is second
|
417
|
+
expires = Time.at((Time.now + 3600).to_i)
|
418
|
+
|
419
|
+
cookie = Mechanize::Cookie.new('key', 'value', :expires => expires.dup)
|
420
|
+
assert_equal 'key', cookie.name
|
421
|
+
assert_equal 'value', cookie.value
|
422
|
+
assert_equal expires, cookie.expires
|
423
|
+
|
424
|
+
cookie = Mechanize::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup)
|
425
|
+
assert_equal 'key', cookie.name
|
426
|
+
assert_equal 'value', cookie.value
|
427
|
+
assert_equal expires, cookie.expires
|
428
|
+
end
|
429
|
+
|
430
|
+
def test_domain=
|
431
|
+
url = URI.parse('http://host.dom.example.com:8080/')
|
432
|
+
|
433
|
+
cookie_str = 'a=b; domain=Example.Com'
|
434
|
+
cookie = Mechanize::Cookie.parse(url, cookie_str).first
|
435
|
+
assert 'example.com', cookie.domain
|
436
|
+
|
437
|
+
cookie.domain = DomainName(url.host)
|
438
|
+
assert 'host.dom.example.com', cookie.domain
|
439
|
+
|
440
|
+
cookie.domain = 'Dom.example.com'
|
441
|
+
assert 'dom.example.com', cookie.domain
|
442
|
+
|
443
|
+
cookie.domain = Object.new.tap { |o|
|
444
|
+
def o.to_str
|
445
|
+
'Example.com'
|
446
|
+
end
|
447
|
+
}
|
448
|
+
assert 'example.com', cookie.domain
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
@@ -0,0 +1,483 @@
|
|
1
|
+
require 'mechanize/test_case'
|
2
|
+
|
3
|
+
class TestMechanizeCookieJar < Mechanize::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
|
8
|
+
@jar = Mechanize::CookieJar.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def cookie_values(options = {})
|
12
|
+
{
|
13
|
+
:name => 'Foo',
|
14
|
+
:value => 'Bar',
|
15
|
+
:path => '/',
|
16
|
+
:expires => Time.now + (10 * 86400),
|
17
|
+
:for_domain => true,
|
18
|
+
:domain => 'rubyforge.org'
|
19
|
+
}.merge(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_two_cookies_same_domain_and_name_different_paths
|
23
|
+
url = URI 'http://rubyforge.org/'
|
24
|
+
|
25
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
26
|
+
@jar.add(url, cookie)
|
27
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:path => '/onetwo')))
|
28
|
+
|
29
|
+
assert_equal(1, @jar.cookies(url).length)
|
30
|
+
assert_equal 2, @jar.cookies(URI('http://rubyforge.org/onetwo')).length
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_domain_case
|
34
|
+
url = URI 'http://rubyforge.org/'
|
35
|
+
|
36
|
+
# Add one cookie with an expiration date in the future
|
37
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
38
|
+
@jar.add(url, cookie)
|
39
|
+
assert_equal(1, @jar.cookies(url).length)
|
40
|
+
|
41
|
+
@jar.add(url, Mechanize::Cookie.new(
|
42
|
+
cookie_values(:domain => 'RuByForge.Org', :name => 'aaron')))
|
43
|
+
|
44
|
+
assert_equal(2, @jar.cookies(url).length)
|
45
|
+
|
46
|
+
url2 = URI 'http://RuByFoRgE.oRg/'
|
47
|
+
assert_equal(2, @jar.cookies(url2).length)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_host_only
|
51
|
+
url = URI.parse('http://rubyforge.org/')
|
52
|
+
|
53
|
+
@jar.add(url, Mechanize::Cookie.new(
|
54
|
+
cookie_values(:domain => 'rubyforge.org', :for_domain => false)))
|
55
|
+
|
56
|
+
assert_equal(1, @jar.cookies(url).length)
|
57
|
+
|
58
|
+
assert_equal(1, @jar.cookies(URI('http://RubyForge.org/')).length)
|
59
|
+
|
60
|
+
assert_equal(1, @jar.cookies(URI('https://RubyForge.org/')).length)
|
61
|
+
|
62
|
+
assert_equal(0, @jar.cookies(URI('http://www.rubyforge.org/')).length)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_empty_value
|
66
|
+
values = cookie_values(:value => "")
|
67
|
+
url = URI 'http://rubyforge.org/'
|
68
|
+
|
69
|
+
# Add one cookie with an expiration date in the future
|
70
|
+
cookie = Mechanize::Cookie.new(values)
|
71
|
+
@jar.add(url, cookie)
|
72
|
+
assert_equal(1, @jar.cookies(url).length)
|
73
|
+
|
74
|
+
@jar.add url, Mechanize::Cookie.new(values.merge(:domain => 'RuByForge.Org',
|
75
|
+
:name => 'aaron'))
|
76
|
+
|
77
|
+
assert_equal(2, @jar.cookies(url).length)
|
78
|
+
|
79
|
+
url2 = URI 'http://RuByFoRgE.oRg/'
|
80
|
+
assert_equal(2, @jar.cookies(url2).length)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_add_future_cookies
|
84
|
+
url = URI 'http://rubyforge.org/'
|
85
|
+
|
86
|
+
# Add one cookie with an expiration date in the future
|
87
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
88
|
+
@jar.add(url, cookie)
|
89
|
+
assert_equal(1, @jar.cookies(url).length)
|
90
|
+
|
91
|
+
# Add the same cookie, and we should still only have one
|
92
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values))
|
93
|
+
assert_equal(1, @jar.cookies(url).length)
|
94
|
+
|
95
|
+
# Make sure we can get the cookie from different paths
|
96
|
+
assert_equal(1, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
97
|
+
|
98
|
+
# Make sure we can't get the cookie from different domains
|
99
|
+
assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_add_multiple_cookies
|
103
|
+
url = URI 'http://rubyforge.org/'
|
104
|
+
|
105
|
+
# Add one cookie with an expiration date in the future
|
106
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
107
|
+
@jar.add(url, cookie)
|
108
|
+
assert_equal(1, @jar.cookies(url).length)
|
109
|
+
|
110
|
+
# Add the same cookie, and we should still only have one
|
111
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
|
112
|
+
assert_equal(2, @jar.cookies(url).length)
|
113
|
+
|
114
|
+
# Make sure we can get the cookie from different paths
|
115
|
+
assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
116
|
+
|
117
|
+
# Make sure we can't get the cookie from different domains
|
118
|
+
assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_add_rejects_cookies_that_do_not_contain_an_embedded_dot
|
122
|
+
url = URI 'http://rubyforge.org/'
|
123
|
+
|
124
|
+
tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.org'))
|
125
|
+
@jar.add(url, tld_cookie)
|
126
|
+
single_dot_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.'))
|
127
|
+
@jar.add(url, single_dot_cookie)
|
128
|
+
|
129
|
+
assert_equal(0, @jar.cookies(url).length)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_fall_back_rules_for_local_domains
|
133
|
+
url = URI 'http://www.example.local'
|
134
|
+
|
135
|
+
tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.local'))
|
136
|
+
@jar.add(url, tld_cookie)
|
137
|
+
|
138
|
+
assert_equal(0, @jar.cookies(url).length)
|
139
|
+
|
140
|
+
sld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.example.local'))
|
141
|
+
@jar.add(url, sld_cookie)
|
142
|
+
|
143
|
+
assert_equal(1, @jar.cookies(url).length)
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_add_makes_exception_for_localhost
|
147
|
+
url = URI 'http://localhost'
|
148
|
+
|
149
|
+
tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => 'localhost'))
|
150
|
+
@jar.add(url, tld_cookie)
|
151
|
+
|
152
|
+
assert_equal(1, @jar.cookies(url).length)
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_add_cookie_for_the_parent_domain
|
156
|
+
url = URI 'http://x.foo.com'
|
157
|
+
|
158
|
+
cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
|
159
|
+
@jar.add(url, cookie)
|
160
|
+
|
161
|
+
assert_equal(1, @jar.cookies(url).length)
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_add_does_not_reject_cookies_from_a_nested_subdomain
|
165
|
+
url = URI 'http://y.x.foo.com'
|
166
|
+
|
167
|
+
cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
|
168
|
+
@jar.add(url, cookie)
|
169
|
+
|
170
|
+
assert_equal(1, @jar.cookies(url).length)
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_cookie_without_leading_dot_does_not_cause_substring_match
|
174
|
+
url = URI 'http://arubyforge.org/'
|
175
|
+
|
176
|
+
cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
|
177
|
+
@jar.add(url, cookie)
|
178
|
+
|
179
|
+
assert_equal(0, @jar.cookies(url).length)
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_cookie_without_leading_dot_matches_subdomains
|
183
|
+
url = URI 'http://admin.rubyforge.org/'
|
184
|
+
|
185
|
+
cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
|
186
|
+
@jar.add(url, cookie)
|
187
|
+
|
188
|
+
assert_equal(1, @jar.cookies(url).length)
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_cookies_with_leading_dot_match_subdomains
|
192
|
+
url = URI 'http://admin.rubyforge.org/'
|
193
|
+
|
194
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
|
195
|
+
|
196
|
+
assert_equal(1, @jar.cookies(url).length)
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_cookies_with_leading_dot_match_parent_domains
|
200
|
+
url = URI 'http://rubyforge.org/'
|
201
|
+
|
202
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
|
203
|
+
|
204
|
+
assert_equal(1, @jar.cookies(url).length)
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_cookies_with_leading_dot_match_parent_domains_exactly
|
208
|
+
url = URI 'http://arubyforge.org/'
|
209
|
+
|
210
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
|
211
|
+
|
212
|
+
assert_equal(0, @jar.cookies(url).length)
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
|
216
|
+
url = URI 'http://192.168.0.1/'
|
217
|
+
|
218
|
+
cookie = Mechanize::Cookie.new(cookie_values(:domain => '192.168.0.1'))
|
219
|
+
@jar.add(url, cookie)
|
220
|
+
|
221
|
+
assert_equal(1, @jar.cookies(url).length)
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_cookie_for_ipv4_address_does_not_cause_subdomain_match
|
225
|
+
url = URI 'http://192.168.0.1/'
|
226
|
+
|
227
|
+
cookie = Mechanize::Cookie.new(cookie_values(:domain => '.0.1'))
|
228
|
+
@jar.add(url, cookie)
|
229
|
+
|
230
|
+
assert_equal(0, @jar.cookies(url).length)
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
|
234
|
+
url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
|
235
|
+
|
236
|
+
cookie = Mechanize::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]'))
|
237
|
+
@jar.add(url, cookie)
|
238
|
+
|
239
|
+
assert_equal(1, @jar.cookies(url).length)
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_cookies_dot
|
243
|
+
url = URI 'http://www.host.example/'
|
244
|
+
|
245
|
+
@jar.add(url,
|
246
|
+
Mechanize::Cookie.new(cookie_values(:domain => 'www.host.example')))
|
247
|
+
|
248
|
+
url = URI 'http://wwwxhost.example/'
|
249
|
+
assert_equal(0, @jar.cookies(url).length)
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_clear_bang
|
253
|
+
url = URI 'http://rubyforge.org/'
|
254
|
+
|
255
|
+
# Add one cookie with an expiration date in the future
|
256
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
257
|
+
@jar.add(url, cookie)
|
258
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
|
259
|
+
assert_equal(2, @jar.cookies(url).length)
|
260
|
+
|
261
|
+
@jar.clear!
|
262
|
+
|
263
|
+
assert_equal(0, @jar.cookies(url).length)
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_save_cookies_yaml
|
267
|
+
url = URI 'http://rubyforge.org/'
|
268
|
+
|
269
|
+
# Add one cookie with an expiration date in the future
|
270
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
271
|
+
s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
|
272
|
+
:expires => nil,
|
273
|
+
:session => true))
|
274
|
+
|
275
|
+
@jar.add(url, cookie)
|
276
|
+
@jar.add(url, s_cookie)
|
277
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
|
278
|
+
|
279
|
+
assert_equal(3, @jar.cookies(url).length)
|
280
|
+
|
281
|
+
in_tmpdir do
|
282
|
+
@jar.save_as("cookies.yml")
|
283
|
+
|
284
|
+
jar = Mechanize::CookieJar.new
|
285
|
+
jar.load("cookies.yml")
|
286
|
+
assert_equal(2, jar.cookies(url).length)
|
287
|
+
end
|
288
|
+
|
289
|
+
assert_equal(3, @jar.cookies(url).length)
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_save_cookies_cookiestxt
|
293
|
+
url = URI 'http://rubyforge.org/'
|
294
|
+
|
295
|
+
# Add one cookie with an expiration date in the future
|
296
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
297
|
+
s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
|
298
|
+
:expires => nil,
|
299
|
+
:session => true))
|
300
|
+
|
301
|
+
@jar.add(url, cookie)
|
302
|
+
@jar.add(url, s_cookie)
|
303
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
|
304
|
+
|
305
|
+
assert_equal(3, @jar.cookies(url).length)
|
306
|
+
|
307
|
+
in_tmpdir do
|
308
|
+
@jar.save_as("cookies.txt", :cookiestxt)
|
309
|
+
|
310
|
+
jar = Mechanize::CookieJar.new
|
311
|
+
jar.load("cookies.txt", :cookiestxt) # HACK test the format
|
312
|
+
assert_equal(2, jar.cookies(url).length)
|
313
|
+
end
|
314
|
+
|
315
|
+
assert_equal(3, @jar.cookies(url).length)
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_expire_cookies
|
319
|
+
url = URI 'http://rubyforge.org/'
|
320
|
+
|
321
|
+
# Add one cookie with an expiration date in the future
|
322
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
323
|
+
@jar.add(url, cookie)
|
324
|
+
assert_equal(1, @jar.cookies(url).length)
|
325
|
+
|
326
|
+
# Add a second cookie
|
327
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
|
328
|
+
assert_equal(2, @jar.cookies(url).length)
|
329
|
+
|
330
|
+
# Make sure we can get the cookie from different paths
|
331
|
+
assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
332
|
+
|
333
|
+
# Expire the first cookie
|
334
|
+
@jar.add(url, Mechanize::Cookie.new(
|
335
|
+
cookie_values(:expires => Time.now - (10 * 86400))))
|
336
|
+
assert_equal(1, @jar.cookies(url).length)
|
337
|
+
|
338
|
+
# Expire the second cookie
|
339
|
+
@jar.add(url, Mechanize::Cookie.new(
|
340
|
+
cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
|
341
|
+
assert_equal(0, @jar.cookies(url).length)
|
342
|
+
end
|
343
|
+
|
344
|
+
def test_session_cookies
|
345
|
+
values = cookie_values(:expires => nil)
|
346
|
+
url = URI 'http://rubyforge.org/'
|
347
|
+
|
348
|
+
# Add one cookie with an expiration date in the future
|
349
|
+
cookie = Mechanize::Cookie.new(values)
|
350
|
+
@jar.add(url, cookie)
|
351
|
+
assert_equal(1, @jar.cookies(url).length)
|
352
|
+
|
353
|
+
# Add a second cookie
|
354
|
+
@jar.add(url, Mechanize::Cookie.new(values.merge(:name => 'Baz')))
|
355
|
+
assert_equal(2, @jar.cookies(url).length)
|
356
|
+
|
357
|
+
# Make sure we can get the cookie from different paths
|
358
|
+
assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
359
|
+
|
360
|
+
# Expire the first cookie
|
361
|
+
@jar.add(url, Mechanize::Cookie.new(values.merge(:expires => Time.now - (10 * 86400))))
|
362
|
+
assert_equal(1, @jar.cookies(url).length)
|
363
|
+
|
364
|
+
# Expire the second cookie
|
365
|
+
@jar.add(url, Mechanize::Cookie.new(
|
366
|
+
values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
|
367
|
+
assert_equal(0, @jar.cookies(url).length)
|
368
|
+
|
369
|
+
# When given a URI with a blank path, CookieJar#cookies should return
|
370
|
+
# cookies with the path '/':
|
371
|
+
url = URI 'http://rubyforge.org'
|
372
|
+
assert_equal '', url.path
|
373
|
+
assert_equal(0, @jar.cookies(url).length)
|
374
|
+
# Now add a cookie with the path set to '/':
|
375
|
+
@jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'has_root_path',
|
376
|
+
:path => '/')))
|
377
|
+
assert_equal(1, @jar.cookies(url).length)
|
378
|
+
end
|
379
|
+
|
380
|
+
def test_paths
|
381
|
+
values = cookie_values(:path => "/login", :expires => nil)
|
382
|
+
url = URI 'http://rubyforge.org/login'
|
383
|
+
|
384
|
+
# Add one cookie with an expiration date in the future
|
385
|
+
cookie = Mechanize::Cookie.new(values)
|
386
|
+
@jar.add(url, cookie)
|
387
|
+
assert_equal(1, @jar.cookies(url).length)
|
388
|
+
|
389
|
+
# Add a second cookie
|
390
|
+
@jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz' )))
|
391
|
+
assert_equal(2, @jar.cookies(url).length)
|
392
|
+
|
393
|
+
# Make sure we don't get the cookie in a different path
|
394
|
+
assert_equal(0, @jar.cookies(URI('http://rubyforge.org/hello')).length)
|
395
|
+
assert_equal(0, @jar.cookies(URI('http://rubyforge.org/')).length)
|
396
|
+
|
397
|
+
# Expire the first cookie
|
398
|
+
@jar.add(url, Mechanize::Cookie.new(values.merge( :expires => Time.now - (10 * 86400))))
|
399
|
+
assert_equal(1, @jar.cookies(url).length)
|
400
|
+
|
401
|
+
# Expire the second cookie
|
402
|
+
@jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz',
|
403
|
+
:expires => Time.now - (10 * 86400))))
|
404
|
+
assert_equal(0, @jar.cookies(url).length)
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_save_and_read_cookiestxt
|
408
|
+
url = URI 'http://rubyforge.org/'
|
409
|
+
|
410
|
+
# Add one cookie with an expiration date in the future
|
411
|
+
cookie = Mechanize::Cookie.new(cookie_values)
|
412
|
+
@jar.add(url, cookie)
|
413
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
|
414
|
+
assert_equal(2, @jar.cookies(url).length)
|
415
|
+
|
416
|
+
in_tmpdir do
|
417
|
+
@jar.save_as("cookies.txt", :cookiestxt)
|
418
|
+
@jar.clear!
|
419
|
+
|
420
|
+
@jar.load("cookies.txt", :cookiestxt)
|
421
|
+
end
|
422
|
+
|
423
|
+
assert_equal(2, @jar.cookies(url).length)
|
424
|
+
end
|
425
|
+
|
426
|
+
def test_save_and_read_cookiestxt_with_session_cookies
|
427
|
+
url = URI 'http://rubyforge.org/'
|
428
|
+
|
429
|
+
@jar.add(url, Mechanize::Cookie.new(cookie_values(:expires => nil)))
|
430
|
+
|
431
|
+
in_tmpdir do
|
432
|
+
@jar.save_as("cookies.txt", :cookiestxt)
|
433
|
+
@jar.clear!
|
434
|
+
|
435
|
+
@jar.load("cookies.txt", :cookiestxt)
|
436
|
+
end
|
437
|
+
|
438
|
+
assert_equal(1, @jar.cookies(url).length)
|
439
|
+
assert_nil @jar.cookies(url).first.expires
|
440
|
+
end
|
441
|
+
|
442
|
+
def test_save_and_read_expired_cookies
|
443
|
+
url = URI 'http://rubyforge.org/'
|
444
|
+
|
445
|
+
@jar.jar['rubyforge.org'] = {}
|
446
|
+
|
447
|
+
|
448
|
+
@jar.add url, Mechanize::Cookie.new(cookie_values)
|
449
|
+
|
450
|
+
# HACK no asertion
|
451
|
+
end
|
452
|
+
|
453
|
+
def test_ssl_cookies
|
454
|
+
# thanks to michal "ocher" ochman for reporting the bug responsible for this test.
|
455
|
+
values = cookie_values(:expires => nil)
|
456
|
+
values_ssl = values.merge(:name => 'Baz', :domain => "#{values[:domain]}:443")
|
457
|
+
url = URI 'https://rubyforge.org/login'
|
458
|
+
|
459
|
+
cookie = Mechanize::Cookie.new(values)
|
460
|
+
@jar.add(url, cookie)
|
461
|
+
assert_equal(1, @jar.cookies(url).length, "did not handle SSL cookie")
|
462
|
+
|
463
|
+
cookie = Mechanize::Cookie.new(values_ssl)
|
464
|
+
@jar.add(url, cookie)
|
465
|
+
assert_equal(2, @jar.cookies(url).length, "did not handle SSL cookie with :443")
|
466
|
+
end
|
467
|
+
|
468
|
+
def test_secure_cookie
|
469
|
+
nurl = URI 'http://rubyforge.org/login'
|
470
|
+
surl = URI 'https://rubyforge.org/login'
|
471
|
+
|
472
|
+
ncookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo1'))
|
473
|
+
scookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo2', :secure => true))
|
474
|
+
|
475
|
+
@jar.add(nurl, ncookie)
|
476
|
+
@jar.add(nurl, scookie)
|
477
|
+
@jar.add(surl, ncookie)
|
478
|
+
@jar.add(surl, scookie)
|
479
|
+
|
480
|
+
assert_equal('Foo1', @jar.cookies(nurl).map { |c| c.name }.sort.join(' ') )
|
481
|
+
assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
|
482
|
+
end
|
483
|
+
end
|