mechanize 2.14.0 → 2.14.2

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.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -1
  3. data/README.md +7 -0
  4. data/SECURITY.md +11 -0
  5. data/lib/mechanize/directory_saver.rb +2 -1
  6. data/lib/mechanize/file_saver.rb +2 -0
  7. data/lib/mechanize/http/agent.rb +83 -29
  8. data/lib/mechanize/parser.rb +33 -3
  9. data/lib/mechanize/test_case.rb +2 -9
  10. data/lib/mechanize/version.rb +1 -1
  11. data/lib/mechanize.rb +12 -2
  12. data/test/htdocs/frame_test.html +8 -6
  13. data/test/htdocs/relative/tc_relative_links.html +1 -1
  14. metadata +7 -174
  15. data/.autotest +0 -6
  16. data/.github/dependabot.yml +0 -11
  17. data/.github/workflows/ci.yml +0 -43
  18. data/.github/workflows/upstream.yml +0 -51
  19. data/.gitignore +0 -15
  20. data/.yardopts +0 -8
  21. data/Gemfile +0 -12
  22. data/Rakefile +0 -56
  23. data/examples/flickr_upload.rb +0 -22
  24. data/examples/latest_user_agents.rb +0 -100
  25. data/examples/mech-dump.rb +0 -5
  26. data/examples/proxy_req.rb +0 -7
  27. data/examples/rubygems.rb +0 -23
  28. data/examples/spider.rb +0 -22
  29. data/examples/wikipedia_links_to_philosophy.rb +0 -158
  30. data/mechanize.gemspec +0 -74
  31. data/test/data/htpasswd +0 -1
  32. data/test/data/server.crt +0 -16
  33. data/test/data/server.csr +0 -12
  34. data/test/data/server.key +0 -15
  35. data/test/data/server.pem +0 -15
  36. data/test/test_mechanize.rb +0 -1382
  37. data/test/test_mechanize_cookie.rb +0 -559
  38. data/test/test_mechanize_cookie_jar.rb +0 -615
  39. data/test/test_mechanize_directory_saver.rb +0 -60
  40. data/test/test_mechanize_download.rb +0 -102
  41. data/test/test_mechanize_element_not_found_error.rb +0 -16
  42. data/test/test_mechanize_file.rb +0 -118
  43. data/test/test_mechanize_file_connection.rb +0 -40
  44. data/test/test_mechanize_file_request.rb +0 -26
  45. data/test/test_mechanize_file_response.rb +0 -48
  46. data/test/test_mechanize_file_saver.rb +0 -22
  47. data/test/test_mechanize_form.rb +0 -1025
  48. data/test/test_mechanize_form_check_box.rb +0 -49
  49. data/test/test_mechanize_form_encoding.rb +0 -110
  50. data/test/test_mechanize_form_field.rb +0 -78
  51. data/test/test_mechanize_form_file_upload.rb +0 -21
  52. data/test/test_mechanize_form_image_button.rb +0 -13
  53. data/test/test_mechanize_form_keygen.rb +0 -34
  54. data/test/test_mechanize_form_multi_select_list.rb +0 -98
  55. data/test/test_mechanize_form_option.rb +0 -60
  56. data/test/test_mechanize_form_radio_button.rb +0 -96
  57. data/test/test_mechanize_form_select_list.rb +0 -81
  58. data/test/test_mechanize_form_textarea.rb +0 -53
  59. data/test/test_mechanize_headers.rb +0 -36
  60. data/test/test_mechanize_history.rb +0 -104
  61. data/test/test_mechanize_http_agent.rb +0 -1915
  62. data/test/test_mechanize_http_auth_challenge.rb +0 -64
  63. data/test/test_mechanize_http_auth_realm.rb +0 -56
  64. data/test/test_mechanize_http_auth_store.rb +0 -233
  65. data/test/test_mechanize_http_content_disposition_parser.rb +0 -167
  66. data/test/test_mechanize_http_www_authenticate_parser.rb +0 -204
  67. data/test/test_mechanize_image.rb +0 -9
  68. data/test/test_mechanize_link.rb +0 -171
  69. data/test/test_mechanize_page.rb +0 -295
  70. data/test/test_mechanize_page_encoding.rb +0 -215
  71. data/test/test_mechanize_page_frame.rb +0 -17
  72. data/test/test_mechanize_page_image.rb +0 -191
  73. data/test/test_mechanize_page_link.rb +0 -377
  74. data/test/test_mechanize_page_meta_refresh.rb +0 -151
  75. data/test/test_mechanize_parser.rb +0 -316
  76. data/test/test_mechanize_pluggable_parser.rb +0 -68
  77. data/test/test_mechanize_redirect_limit_reached_error.rb +0 -25
  78. data/test/test_mechanize_redirect_not_get_or_head_error.rb +0 -15
  79. data/test/test_mechanize_response_read_error.rb +0 -29
  80. data/test/test_mechanize_subclass.rb +0 -23
  81. data/test/test_mechanize_util.rb +0 -144
  82. data/test/test_mechanize_xml_file.rb +0 -30
  83. data/test/test_multi_select.rb +0 -120
@@ -1,615 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'mechanize/test_case'
3
- require 'fileutils'
4
-
5
- class TestMechanizeCookieJar < Mechanize::TestCase
6
-
7
- def setup
8
- super
9
-
10
- @jar = Mechanize::CookieJar.new
11
-
12
- @jar.extend Minitest::Assertions
13
-
14
- def @jar.add(*args)
15
- capture_io { super }
16
- end
17
-
18
- def @jar.jar(*args)
19
- result = nil
20
- capture_io { result = super }
21
- result
22
- end
23
-
24
- def @jar.save_as(*args)
25
- result = nil
26
- capture_io { result = super }
27
- result
28
- end
29
-
30
- def @jar.clear!(*args)
31
- result = nil
32
- capture_io { result = super }
33
- result
34
- end
35
- end
36
-
37
- def cookie_values(options = {})
38
- {
39
- :name => 'Foo',
40
- :value => 'Bar',
41
- :path => '/',
42
- :expires => Time.now + (10 * 86400),
43
- :for_domain => true,
44
- :domain => 'rubygems.org'
45
- }.merge(options)
46
- end
47
-
48
- def test_two_cookies_same_domain_and_name_different_paths
49
- url = URI 'http://rubygems.org/'
50
-
51
- cookie = Mechanize::Cookie.new(cookie_values)
52
- @jar.add(url, cookie)
53
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:path => '/onetwo')))
54
-
55
- assert_equal(1, @jar.cookies(url).length)
56
- assert_equal 2, @jar.cookies(URI('http://rubygems.org/onetwo')).length
57
- end
58
-
59
- def test_domain_case
60
- url = URI 'http://rubygems.org/'
61
-
62
- # Add one cookie with an expiration date in the future
63
- cookie = Mechanize::Cookie.new(cookie_values)
64
- @jar.add(url, cookie)
65
- assert_equal(1, @jar.cookies(url).length)
66
-
67
- @jar.add(url, Mechanize::Cookie.new(
68
- cookie_values(:domain => 'rubygems.Org', :name => 'aaron')))
69
-
70
- assert_equal(2, @jar.cookies(url).length)
71
-
72
- url2 = URI 'http://rubygems.oRg/'
73
- assert_equal(2, @jar.cookies(url2).length)
74
- end
75
-
76
- def test_host_only
77
- url = URI.parse('http://rubygems.org/')
78
-
79
- @jar.add(url, Mechanize::Cookie.new(
80
- cookie_values(:domain => 'rubygems.org', :for_domain => false)))
81
-
82
- assert_equal(1, @jar.cookies(url).length)
83
-
84
- assert_equal(1, @jar.cookies(URI('http://rubygems.org/')).length)
85
-
86
- assert_equal(1, @jar.cookies(URI('https://rubygems.org/')).length)
87
-
88
- assert_equal(0, @jar.cookies(URI('http://www.rubygems.org/')).length)
89
- end
90
-
91
- def test_empty_value
92
- values = cookie_values(:value => "")
93
- url = URI 'http://rubygems.org/'
94
-
95
- # Add one cookie with an expiration date in the future
96
- cookie = Mechanize::Cookie.new(values)
97
- @jar.add(url, cookie)
98
- assert_equal(1, @jar.cookies(url).length)
99
-
100
- @jar.add url, Mechanize::Cookie.new(values.merge(:domain => 'rubygems.Org',
101
- :name => 'aaron'))
102
-
103
- assert_equal(2, @jar.cookies(url).length)
104
-
105
- url2 = URI 'http://rubygems.oRg/'
106
- assert_equal(2, @jar.cookies(url2).length)
107
- end
108
-
109
- def test_add_future_cookies
110
- url = URI 'http://rubygems.org/'
111
-
112
- # Add one cookie with an expiration date in the future
113
- cookie = Mechanize::Cookie.new(cookie_values)
114
- @jar.add(url, cookie)
115
- assert_equal(1, @jar.cookies(url).length)
116
-
117
- # Add the same cookie, and we should still only have one
118
- @jar.add(url, Mechanize::Cookie.new(cookie_values))
119
- assert_equal(1, @jar.cookies(url).length)
120
-
121
- # Make sure we can get the cookie from different paths
122
- assert_equal(1, @jar.cookies(URI('http://rubygems.org/login')).length)
123
-
124
- # Make sure we can't get the cookie from different domains
125
- assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
126
- end
127
-
128
- def test_add_multiple_cookies
129
- url = URI 'http://rubygems.org/'
130
-
131
- # Add one cookie with an expiration date in the future
132
- cookie = Mechanize::Cookie.new(cookie_values)
133
- @jar.add(url, cookie)
134
- assert_equal(1, @jar.cookies(url).length)
135
-
136
- # Add the same cookie, and we should still only have one
137
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
138
- assert_equal(2, @jar.cookies(url).length)
139
-
140
- # Make sure we can get the cookie from different paths
141
- assert_equal(2, @jar.cookies(URI('http://rubygems.org/login')).length)
142
-
143
- # Make sure we can't get the cookie from different domains
144
- assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
145
- end
146
-
147
- def test_add_rejects_cookies_that_do_not_contain_an_embedded_dot
148
- url = URI 'http://rubygems.org/'
149
-
150
- tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.org'))
151
- @jar.add(url, tld_cookie)
152
- # single dot domain is now treated as no domain
153
- # single_dot_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.'))
154
- # @jar.add(url, single_dot_cookie)
155
-
156
- assert_equal(0, @jar.cookies(url).length)
157
- end
158
-
159
- def test_fall_back_rules_for_local_domains
160
- url = URI 'http://www.example.local'
161
-
162
- tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.local'))
163
- @jar.add(url, tld_cookie)
164
-
165
- assert_equal(0, @jar.cookies(url).length)
166
-
167
- sld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.example.local'))
168
- @jar.add(url, sld_cookie)
169
-
170
- assert_equal(1, @jar.cookies(url).length)
171
- end
172
-
173
- def test_add_makes_exception_for_localhost
174
- url = URI 'http://localhost'
175
-
176
- tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => 'localhost'))
177
- @jar.add(url, tld_cookie)
178
-
179
- assert_equal(1, @jar.cookies(url).length)
180
- end
181
-
182
- def test_add_cookie_for_the_parent_domain
183
- url = URI 'http://x.foo.com'
184
-
185
- cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
186
- @jar.add(url, cookie)
187
-
188
- assert_equal(1, @jar.cookies(url).length)
189
- end
190
-
191
- def test_add_does_not_reject_cookies_from_a_nested_subdomain
192
- url = URI 'http://y.x.foo.com'
193
-
194
- cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
195
- @jar.add(url, cookie)
196
-
197
- assert_equal(1, @jar.cookies(url).length)
198
- end
199
-
200
- def test_cookie_without_leading_dot_does_not_cause_substring_match
201
- url = URI 'http://arubygems.org/'
202
-
203
- cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubygems.org'))
204
- @jar.add(url, cookie)
205
-
206
- assert_equal(0, @jar.cookies(url).length)
207
- end
208
-
209
- def test_cookie_without_leading_dot_matches_subdomains
210
- url = URI 'http://admin.rubygems.org/'
211
-
212
- cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubygems.org'))
213
- @jar.add(url, cookie)
214
-
215
- assert_equal(1, @jar.cookies(url).length)
216
- end
217
-
218
- def test_cookies_with_leading_dot_match_subdomains
219
- url = URI 'http://admin.rubygems.org/'
220
-
221
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubygems.org')))
222
-
223
- assert_equal(1, @jar.cookies(url).length)
224
- end
225
-
226
- def test_cookies_with_leading_dot_match_parent_domains
227
- url = URI 'http://rubygems.org/'
228
-
229
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubygems.org')))
230
-
231
- assert_equal(1, @jar.cookies(url).length)
232
- end
233
-
234
- def test_cookies_with_leading_dot_match_parent_domains_exactly
235
- url = URI 'http://arubygems.org/'
236
-
237
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubygems.org')))
238
-
239
- assert_equal(0, @jar.cookies(url).length)
240
- end
241
-
242
- def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
243
- url = URI 'http://192.168.0.1/'
244
-
245
- cookie = Mechanize::Cookie.new(cookie_values(:domain => '192.168.0.1'))
246
- @jar.add(url, cookie)
247
-
248
- assert_equal(1, @jar.cookies(url).length)
249
- end
250
-
251
- def test_cookie_for_ipv4_address_does_not_cause_subdomain_match
252
- url = URI 'http://192.168.0.1/'
253
-
254
- cookie = Mechanize::Cookie.new(cookie_values(:domain => '.0.1'))
255
- @jar.add(url, cookie)
256
-
257
- assert_equal(0, @jar.cookies(url).length)
258
- end
259
-
260
- def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
261
- url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
262
-
263
- cookie = Mechanize::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]'))
264
- @jar.add(url, cookie)
265
-
266
- assert_equal(1, @jar.cookies(url).length)
267
- end
268
-
269
- def test_cookies_dot
270
- url = URI 'http://www.host.example/'
271
-
272
- @jar.add(url,
273
- Mechanize::Cookie.new(cookie_values(:domain => 'www.host.example')))
274
-
275
- url = URI 'http://wwwxhost.example/'
276
- assert_equal(0, @jar.cookies(url).length)
277
- end
278
-
279
- def test_clear_bang
280
- url = URI 'http://rubygems.org/'
281
-
282
- # Add one cookie with an expiration date in the future
283
- cookie = Mechanize::Cookie.new(cookie_values)
284
- @jar.add(url, cookie)
285
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
286
- assert_equal(2, @jar.cookies(url).length)
287
-
288
- @jar.clear!
289
-
290
- assert_equal(0, @jar.cookies(url).length)
291
- end
292
-
293
- def test_save_cookies_yaml
294
- url = URI 'http://rubygems.org/'
295
-
296
- # Add one cookie with an expiration date in the future
297
- cookie = Mechanize::Cookie.new(cookie_values)
298
- s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
299
- :expires => nil))
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
- value = @jar.save_as("cookies.yml")
309
- assert_same @jar, value
310
-
311
- jar = Mechanize::CookieJar.new
312
- jar.load("cookies.yml")
313
- assert_equal(2, jar.cookies(url).length)
314
- end
315
-
316
- assert_equal(3, @jar.cookies(url).length)
317
- end
318
-
319
- def test_save_session_cookies_yaml
320
- url = URI 'http://rubygems.org/'
321
-
322
- # Add one cookie with an expiration date in the future
323
- cookie = Mechanize::Cookie.new(cookie_values)
324
- s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
325
- :expires => nil))
326
-
327
- @jar.add(url, cookie)
328
- @jar.add(url, s_cookie)
329
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
330
-
331
- assert_equal(3, @jar.cookies(url).length)
332
-
333
- in_tmpdir do
334
- @jar.save_as("cookies.yml", :format => :yaml, :session => true)
335
-
336
- jar = Mechanize::CookieJar.new
337
- jar.load("cookies.yml")
338
- assert_equal(3, jar.cookies(url).length)
339
- end
340
-
341
- assert_equal(3, @jar.cookies(url).length)
342
- end
343
-
344
-
345
- def test_save_cookies_cookiestxt
346
- url = URI 'http://rubygems.org/'
347
-
348
- # Add one cookie with an expiration date in the future
349
- cookie = Mechanize::Cookie.new(cookie_values)
350
- s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
351
- :expires => nil))
352
-
353
- @jar.add(url, cookie)
354
- @jar.add(url, s_cookie)
355
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
356
-
357
- assert_equal(3, @jar.cookies(url).length)
358
-
359
- in_tmpdir do
360
- @jar.save_as("cookies.txt", :cookiestxt)
361
-
362
- assert_match(/\A# (?:Netscape )?HTTP Cookie File$/, File.read("cookies.txt"))
363
-
364
- jar = Mechanize::CookieJar.new
365
- jar.load("cookies.txt", :cookiestxt)
366
- assert_equal(2, jar.cookies(url).length)
367
- end
368
-
369
- in_tmpdir do
370
- @jar.save_as("cookies.txt", :cookiestxt, :session => true)
371
-
372
- assert_match(/\A# (?:Netscape )?HTTP Cookie File$/, File.read("cookies.txt"))
373
-
374
- jar = Mechanize::CookieJar.new
375
- jar.load("cookies.txt", :cookiestxt)
376
- assert_equal(3, jar.cookies(url).length)
377
- end
378
-
379
- assert_equal(3, @jar.cookies(url).length)
380
- end
381
-
382
- def test_expire_cookies
383
- url = URI 'http://rubygems.org/'
384
-
385
- # Add one cookie with an expiration date in the future
386
- cookie = Mechanize::Cookie.new(cookie_values)
387
- @jar.add(url, cookie)
388
- assert_equal(1, @jar.cookies(url).length)
389
-
390
- # Add a second cookie
391
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
392
- assert_equal(2, @jar.cookies(url).length)
393
-
394
- # Make sure we can get the cookie from different paths
395
- assert_equal(2, @jar.cookies(URI('http://rubygems.org/login')).length)
396
-
397
- # Expire the first cookie
398
- @jar.add(url, Mechanize::Cookie.new(
399
- cookie_values(:expires => Time.now - (10 * 86400))))
400
- assert_equal(1, @jar.cookies(url).length)
401
-
402
- # Expire the second cookie
403
- @jar.add(url, Mechanize::Cookie.new(
404
- cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
405
- assert_equal(0, @jar.cookies(url).length)
406
- end
407
-
408
- def test_session_cookies
409
- values = cookie_values(:expires => nil)
410
- url = URI 'http://rubygems.org/'
411
-
412
- # Add one cookie with an expiration date in the future
413
- cookie = Mechanize::Cookie.new(values)
414
- @jar.add(url, cookie)
415
- assert_equal(1, @jar.cookies(url).length)
416
-
417
- # Add a second cookie
418
- @jar.add(url, Mechanize::Cookie.new(values.merge(:name => 'Baz')))
419
- assert_equal(2, @jar.cookies(url).length)
420
-
421
- # Make sure we can get the cookie from different paths
422
- assert_equal(2, @jar.cookies(URI('http://rubygems.org/login')).length)
423
-
424
- # Expire the first cookie
425
- @jar.add(url, Mechanize::Cookie.new(values.merge(:expires => Time.now - (10 * 86400))))
426
- assert_equal(1, @jar.cookies(url).length)
427
-
428
- # Expire the second cookie
429
- @jar.add(url, Mechanize::Cookie.new(
430
- values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
431
- assert_equal(0, @jar.cookies(url).length)
432
-
433
- # When given a URI with a blank path, CookieJar#cookies should return
434
- # cookies with the path '/':
435
- url = URI 'http://rubygems.org'
436
- assert_equal '', url.path
437
- assert_equal(0, @jar.cookies(url).length)
438
- # Now add a cookie with the path set to '/':
439
- @jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'has_root_path',
440
- :path => '/')))
441
- assert_equal(1, @jar.cookies(url).length)
442
- end
443
-
444
- def test_paths
445
- values = cookie_values(:path => "/login", :expires => nil)
446
- url = URI 'http://rubygems.org/login'
447
-
448
- # Add one cookie with an expiration date in the future
449
- cookie = Mechanize::Cookie.new(values)
450
- @jar.add(url, cookie)
451
- assert_equal(1, @jar.cookies(url).length)
452
-
453
- # Add a second cookie
454
- @jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz' )))
455
- assert_equal(2, @jar.cookies(url).length)
456
-
457
- # Make sure we don't get the cookie in a different path
458
- assert_equal(0, @jar.cookies(URI('http://rubygems.org/hello')).length)
459
- assert_equal(0, @jar.cookies(URI('http://rubygems.org/')).length)
460
-
461
- # Expire the first cookie
462
- @jar.add(url, Mechanize::Cookie.new(values.merge( :expires => Time.now - (10 * 86400))))
463
- assert_equal(1, @jar.cookies(url).length)
464
-
465
- # Expire the second cookie
466
- @jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz',
467
- :expires => Time.now - (10 * 86400))))
468
- assert_equal(0, @jar.cookies(url).length)
469
- end
470
-
471
- def test_save_and_read_cookiestxt
472
- url = URI 'http://rubygems.org/'
473
-
474
- # Add one cookie with an expiration date in the future
475
- cookie = Mechanize::Cookie.new(cookie_values)
476
- @jar.add(url, cookie)
477
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
478
- assert_equal(2, @jar.cookies(url).length)
479
-
480
- in_tmpdir do
481
- @jar.save_as("cookies.txt", :cookiestxt)
482
- @jar.clear!
483
-
484
- @jar.load("cookies.txt", :cookiestxt)
485
- end
486
-
487
- assert_equal(2, @jar.cookies(url).length)
488
- end
489
-
490
- def test_save_and_read_cookiestxt_with_session_cookies
491
- url = URI 'http://rubygems.org/'
492
-
493
- @jar.add(url, Mechanize::Cookie.new(cookie_values(:expires => nil)))
494
-
495
- in_tmpdir do
496
- @jar.save_as("cookies.txt", :cookiestxt)
497
- @jar.clear!
498
-
499
- @jar.load("cookies.txt", :cookiestxt)
500
- end
501
-
502
- assert_equal(0, @jar.cookies(url).length)
503
- end
504
-
505
- def test_prevent_command_injection_when_saving
506
- skip if windows?
507
- url = URI 'http://rubygems.org/'
508
- path = '| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\''
509
-
510
- @jar.add(url, Mechanize::Cookie.new(cookie_values))
511
-
512
- in_tmpdir do
513
- @jar.save_as(path, :cookiestxt)
514
- assert_equal(false, File.exist?('vul.txt'))
515
- end
516
- end
517
-
518
- def test_prevent_command_injection_when_loading
519
- skip if windows?
520
- url = URI 'http://rubygems.org/'
521
- path = '| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\''
522
-
523
- @jar.add(url, Mechanize::Cookie.new(cookie_values))
524
-
525
- in_tmpdir do
526
- @jar.save_as("cookies.txt", :cookiestxt)
527
- @jar.clear!
528
-
529
- assert_raises Errno::ENOENT do
530
- @jar.load(path, :cookiestxt)
531
- end
532
- assert_equal(false, File.exist?('vul.txt'))
533
- end
534
- end
535
-
536
- def test_save_and_read_expired_cookies
537
- url = URI 'http://rubygems.org/'
538
-
539
- @jar.jar['rubygems.org'] = {}
540
-
541
-
542
- @jar.add url, Mechanize::Cookie.new(cookie_values)
543
-
544
- # HACK no assertion
545
- end
546
-
547
- def test_ssl_cookies
548
- # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
549
- values = cookie_values(:expires => nil)
550
- values_ssl = values.merge(:name => 'Baz', :domain => "#{values[:domain]}:443")
551
- url = URI 'https://rubygems.org/login'
552
-
553
- cookie = Mechanize::Cookie.new(values)
554
- @jar.add(url, cookie)
555
- assert_equal(1, @jar.cookies(url).length, "did not handle SSL cookie")
556
-
557
- cookie = Mechanize::Cookie.new(values_ssl)
558
- @jar.add(url, cookie)
559
- assert_equal(2, @jar.cookies(url).length, "did not handle SSL cookie with :443")
560
- end
561
-
562
- def test_secure_cookie
563
- nurl = URI 'http://rubygems.org/login'
564
- surl = URI 'https://rubygems.org/login'
565
-
566
- ncookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo1'))
567
- scookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo2', :secure => true))
568
-
569
- @jar.add(nurl, ncookie)
570
- @jar.add(nurl, scookie)
571
- @jar.add(surl, ncookie)
572
- @jar.add(surl, scookie)
573
-
574
- assert_equal('Foo1', @jar.cookies(nurl).map { |c| c.name }.sort.join(' ') )
575
- assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
576
- end
577
-
578
- def test_save_cookies_cookiestxt_subdomain
579
- top_url = URI 'http://rubygems.org/'
580
- subdomain_url = URI 'http://admin.rubygems.org/'
581
-
582
- # cookie1 is for *.rubygems.org; cookie2 is only for rubygems.org, no subdomains
583
- cookie1 = Mechanize::Cookie.new(cookie_values)
584
- cookie2 = Mechanize::Cookie.new(cookie_values(:name => 'Boo', :for_domain => false))
585
-
586
- @jar.add(top_url, cookie1)
587
- @jar.add(top_url, cookie2)
588
-
589
- assert_equal(2, @jar.cookies(top_url).length)
590
- assert_equal(1, @jar.cookies(subdomain_url).length)
591
-
592
- in_tmpdir do
593
- @jar.save_as("cookies.txt", :cookiestxt)
594
-
595
- jar = Mechanize::CookieJar.new
596
- jar.load("cookies.txt", :cookiestxt) # HACK test the format
597
- assert_equal(2, jar.cookies(top_url).length)
598
- assert_equal(1, jar.cookies(subdomain_url).length)
599
-
600
- # Check that we actually wrote the file correctly (not just that we were
601
- # able to read what we wrote):
602
- #
603
- # * Cookies that only match exactly the domain specified must not have a
604
- # leading dot, and must have FALSE as the second field.
605
- # * Cookies that match subdomains may have a leading dot, and must have
606
- # TRUE as the second field.
607
- cookies_txt = File.readlines("cookies.txt")
608
- assert_equal(1, cookies_txt.grep( /^rubygems\.org\tFALSE/ ).length)
609
- assert_equal(1, cookies_txt.grep( /^\.rubygems\.org\tTRUE/ ).length)
610
- end
611
-
612
- assert_equal(2, @jar.cookies(top_url).length)
613
- assert_equal(1, @jar.cookies(subdomain_url).length)
614
- end
615
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'mechanize/test_case'
3
-
4
- class TestMechanizeDirectorySaver < Mechanize::TestCase
5
-
6
- def setup
7
- super
8
-
9
- @uri = URI 'http://example/relative/tc_relative_links.html'
10
- @io = StringIO.new 'hello world'
11
- end
12
-
13
- def test_self_save_to
14
- in_tmpdir do
15
- saver = Mechanize::DirectorySaver.save_to 'dir'
16
-
17
- saver.new @uri, nil, @io, 200
18
-
19
- assert File.exist? 'dir/tc_relative_links.html'
20
- refute File.exist? 'dir/relative'
21
- end
22
- end
23
-
24
- def test_self_save_to_cd
25
- in_tmpdir do
26
- saver = Mechanize::DirectorySaver.save_to 'dir'
27
-
28
- FileUtils.mkdir 'other'
29
-
30
- Dir.chdir 'other' do
31
- saver.new @uri, nil, @io, 200
32
- end
33
-
34
- assert File.exist? 'dir/tc_relative_links.html'
35
- refute File.exist? 'dir/relative'
36
- end
37
- end
38
-
39
- def test_with_decode_filename
40
- in_tmpdir do
41
- saver = Mechanize::DirectorySaver.save_to 'dir', :decode_filename => true
42
- uri = URI 'http://example.com/foo+bar.html'
43
- saver.new uri, nil, @io, 200
44
-
45
- assert File.exist? 'dir/foo bar.html'
46
- end
47
- end
48
-
49
- def test_initialize_no_save_dir
50
- in_tmpdir do
51
- e = assert_raises Mechanize::Error do
52
- Mechanize::DirectorySaver.new @uri, nil, @io, 200
53
- end
54
-
55
- assert_match %r%no save directory specified%, e.message
56
- end
57
- end
58
-
59
- end
60
-