mechanize 2.0.1 → 2.1.pre.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mechanize might be problematic. Click here for more details.

Files changed (148) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG.rdoc +82 -0
  3. data/EXAMPLES.rdoc +1 -1
  4. data/FAQ.rdoc +9 -9
  5. data/Manifest.txt +35 -48
  6. data/README.rdoc +2 -1
  7. data/Rakefile +16 -3
  8. data/lib/mechanize.rb +809 -392
  9. data/lib/mechanize/content_type_error.rb +10 -11
  10. data/lib/mechanize/cookie.rb +193 -60
  11. data/lib/mechanize/cookie_jar.rb +39 -86
  12. data/lib/mechanize/download.rb +59 -0
  13. data/lib/mechanize/element_matcher.rb +1 -0
  14. data/lib/mechanize/file.rb +61 -76
  15. data/lib/mechanize/file_saver.rb +37 -35
  16. data/lib/mechanize/form.rb +475 -410
  17. data/lib/mechanize/form/button.rb +4 -7
  18. data/lib/mechanize/form/check_box.rb +10 -9
  19. data/lib/mechanize/form/field.rb +52 -42
  20. data/lib/mechanize/form/file_upload.rb +17 -19
  21. data/lib/mechanize/form/hidden.rb +3 -0
  22. data/lib/mechanize/form/image_button.rb +15 -16
  23. data/lib/mechanize/form/keygen.rb +34 -0
  24. data/lib/mechanize/form/multi_select_list.rb +20 -9
  25. data/lib/mechanize/form/option.rb +48 -47
  26. data/lib/mechanize/form/radio_button.rb +52 -45
  27. data/lib/mechanize/form/reset.rb +3 -0
  28. data/lib/mechanize/form/select_list.rb +10 -6
  29. data/lib/mechanize/form/submit.rb +3 -0
  30. data/lib/mechanize/form/text.rb +3 -0
  31. data/lib/mechanize/form/textarea.rb +3 -0
  32. data/lib/mechanize/headers.rb +17 -19
  33. data/lib/mechanize/history.rb +60 -61
  34. data/lib/mechanize/http.rb +5 -0
  35. data/lib/mechanize/http/agent.rb +485 -218
  36. data/lib/mechanize/http/auth_challenge.rb +59 -0
  37. data/lib/mechanize/http/auth_realm.rb +31 -0
  38. data/lib/mechanize/http/content_disposition_parser.rb +188 -0
  39. data/lib/mechanize/http/www_authenticate_parser.rb +155 -0
  40. data/lib/mechanize/monkey_patch.rb +14 -35
  41. data/lib/mechanize/page.rb +34 -2
  42. data/lib/mechanize/page/base.rb +6 -7
  43. data/lib/mechanize/page/frame.rb +5 -5
  44. data/lib/mechanize/page/image.rb +23 -23
  45. data/lib/mechanize/page/label.rb +16 -16
  46. data/lib/mechanize/page/link.rb +16 -0
  47. data/lib/mechanize/page/meta_refresh.rb +19 -7
  48. data/lib/mechanize/parser.rb +173 -0
  49. data/lib/mechanize/pluggable_parsers.rb +126 -83
  50. data/lib/mechanize/redirect_limit_reached_error.rb +16 -13
  51. data/lib/mechanize/redirect_not_get_or_head_error.rb +18 -16
  52. data/lib/mechanize/response_code_error.rb +16 -17
  53. data/lib/mechanize/robots_disallowed_error.rb +22 -23
  54. data/lib/mechanize/test_case.rb +659 -0
  55. data/lib/mechanize/unauthorized_error.rb +3 -0
  56. data/lib/mechanize/unsupported_scheme_error.rb +4 -6
  57. data/lib/mechanize/util.rb +0 -12
  58. data/test/htdocs/form_order_test.html +11 -0
  59. data/test/htdocs/form_test.html +2 -2
  60. data/test/htdocs/tc_links.html +1 -0
  61. data/test/test_mechanize.rb +367 -59
  62. data/test/test_mechanize_cookie.rb +69 -4
  63. data/test/test_mechanize_cookie_jar.rb +200 -124
  64. data/test/test_mechanize_download.rb +43 -0
  65. data/test/test_mechanize_file.rb +53 -45
  66. data/test/{test_mechanize_file_response.rb → test_mechanize_file_connection.rb} +2 -2
  67. data/test/test_mechanize_file_request.rb +2 -2
  68. data/test/test_mechanize_file_saver.rb +21 -0
  69. data/test/test_mechanize_form.rb +345 -46
  70. data/test/test_mechanize_form_check_box.rb +5 -4
  71. data/test/test_mechanize_form_encoding.rb +10 -16
  72. data/test/test_mechanize_form_field.rb +45 -3
  73. data/test/test_mechanize_form_file_upload.rb +20 -0
  74. data/test/test_mechanize_form_image_button.rb +2 -2
  75. data/test/test_mechanize_form_keygen.rb +32 -0
  76. data/test/test_mechanize_form_multi_select_list.rb +84 -0
  77. data/test/test_mechanize_form_option.rb +55 -0
  78. data/test/test_mechanize_form_radio_button.rb +78 -0
  79. data/test/test_mechanize_form_select_list.rb +76 -0
  80. data/test/test_mechanize_form_textarea.rb +8 -7
  81. data/test/{test_headers.rb → test_mechanize_headers.rb} +4 -2
  82. data/test/test_mechanize_history.rb +103 -0
  83. data/test/test_mechanize_http_agent.rb +525 -17
  84. data/test/test_mechanize_http_auth_challenge.rb +39 -0
  85. data/test/test_mechanize_http_auth_realm.rb +49 -0
  86. data/test/test_mechanize_http_content_disposition_parser.rb +118 -0
  87. data/test/test_mechanize_http_www_authenticate_parser.rb +146 -0
  88. data/test/test_mechanize_link.rb +10 -14
  89. data/test/test_mechanize_page.rb +118 -0
  90. data/test/test_mechanize_page_encoding.rb +48 -13
  91. data/test/test_mechanize_page_frame.rb +16 -0
  92. data/test/test_mechanize_page_link.rb +27 -19
  93. data/test/test_mechanize_page_meta_refresh.rb +26 -14
  94. data/test/test_mechanize_parser.rb +289 -0
  95. data/test/test_mechanize_pluggable_parser.rb +52 -0
  96. data/test/test_mechanize_redirect_limit_reached_error.rb +24 -0
  97. data/test/test_mechanize_redirect_not_get_or_head_error.rb +3 -7
  98. data/test/test_mechanize_subclass.rb +2 -2
  99. data/test/test_mechanize_util.rb +24 -13
  100. data/test/test_multi_select.rb +23 -22
  101. metadata +145 -114
  102. metadata.gz.sig +0 -0
  103. data/lib/mechanize/inspect.rb +0 -88
  104. data/test/helper.rb +0 -175
  105. data/test/htdocs/form_select_all.html +0 -16
  106. data/test/htdocs/form_select_none.html +0 -17
  107. data/test/htdocs/form_select_noopts.html +0 -10
  108. data/test/htdocs/iframe_test.html +0 -16
  109. data/test/htdocs/nofollow.html +0 -9
  110. data/test/htdocs/norobots.html +0 -8
  111. data/test/htdocs/rel_nofollow.html +0 -8
  112. data/test/htdocs/tc_base_images.html +0 -10
  113. data/test/htdocs/tc_images.html +0 -8
  114. data/test/htdocs/tc_no_attributes.html +0 -16
  115. data/test/htdocs/tc_radiobuttons.html +0 -17
  116. data/test/htdocs/test_bad_encoding.html +0 -52
  117. data/test/servlets.rb +0 -402
  118. data/test/ssl_server.rb +0 -48
  119. data/test/test_cookies.rb +0 -129
  120. data/test/test_form_action.rb +0 -52
  121. data/test/test_form_as_hash.rb +0 -59
  122. data/test/test_form_button.rb +0 -46
  123. data/test/test_frames.rb +0 -34
  124. data/test/test_history.rb +0 -118
  125. data/test/test_history_added.rb +0 -16
  126. data/test/test_html_unscape_forms.rb +0 -46
  127. data/test/test_if_modified_since.rb +0 -20
  128. data/test/test_images.rb +0 -19
  129. data/test/test_no_attributes.rb +0 -13
  130. data/test/test_option.rb +0 -18
  131. data/test/test_pluggable_parser.rb +0 -136
  132. data/test/test_post_form.rb +0 -37
  133. data/test/test_pretty_print.rb +0 -22
  134. data/test/test_radiobutton.rb +0 -75
  135. data/test/test_redirect_limit_reached.rb +0 -39
  136. data/test/test_referer.rb +0 -81
  137. data/test/test_relative_links.rb +0 -40
  138. data/test/test_request.rb +0 -13
  139. data/test/test_response_code.rb +0 -53
  140. data/test/test_robots.rb +0 -72
  141. data/test/test_save_file.rb +0 -48
  142. data/test/test_scheme.rb +0 -48
  143. data/test/test_select.rb +0 -119
  144. data/test/test_select_all.rb +0 -15
  145. data/test/test_select_none.rb +0 -15
  146. data/test/test_select_noopts.rb +0 -18
  147. data/test/test_set_fields.rb +0 -44
  148. data/test/test_ssl_server.rb +0 -20
@@ -1,4 +1,4 @@
1
- require "helper"
1
+ require 'mechanize/test_case'
2
2
 
3
3
  module Enumerable
4
4
  def combine
@@ -17,7 +17,7 @@ module Enumerable
17
17
  end
18
18
  end
19
19
 
20
- class TestMechanizeCookie < MiniTest::Unit::TestCase
20
+ class TestMechanizeCookie < Mechanize::TestCase
21
21
  def silently
22
22
  warn_level = $VERBOSE
23
23
  $VERBOSE = false
@@ -70,6 +70,31 @@ class TestMechanizeCookie < MiniTest::Unit::TestCase
70
70
  end
71
71
  end
72
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
+
73
98
  def test_parse_weird_cookie
74
99
  cookie = 'n/a, ASPSESSIONIDCSRRQDQR=FBLDGHPBNDJCPCGNCPAENELB; path=/'
75
100
  url = URI.parse('http://www.searchinnovation.com/')
@@ -128,7 +153,8 @@ class TestMechanizeCookie < MiniTest::Unit::TestCase
128
153
 
129
154
  cookie = Mechanize::Cookie.parse(url, cookie_str).first
130
155
 
131
- assert_equal '.example.com', cookie.domain
156
+ assert_equal 'example.com', cookie.domain
157
+ assert cookie.for_domain?
132
158
  end
133
159
 
134
160
  def test_parse_domain_no_dot
@@ -138,7 +164,8 @@ class TestMechanizeCookie < MiniTest::Unit::TestCase
138
164
 
139
165
  cookie = Mechanize::Cookie.parse(url, cookie_str).first
140
166
 
141
- assert_equal '.example.com', cookie.domain
167
+ assert_equal 'example.com', cookie.domain
168
+ assert cookie.for_domain?
142
169
  end
143
170
 
144
171
  def test_parse_domain_none
@@ -149,6 +176,7 @@ class TestMechanizeCookie < MiniTest::Unit::TestCase
149
176
  cookie = Mechanize::Cookie.parse(url, cookie_str).first
150
177
 
151
178
  assert_equal 'example.com', cookie.domain
179
+ assert !cookie.for_domain?
152
180
  end
153
181
 
154
182
  def test_parse_expires_session
@@ -161,6 +189,43 @@ class TestMechanizeCookie < MiniTest::Unit::TestCase
161
189
  assert cookie.session
162
190
  end
163
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
+
164
229
  def test_parse_valid_cookie
165
230
  url = URI.parse('http://rubyforge.org/')
166
231
  cookie_params = {}
@@ -1,19 +1,11 @@
1
- require 'helper'
2
- require 'tmpdir'
1
+ require 'mechanize/test_case'
3
2
 
4
- class TestMechanizeCookieJar < MiniTest::Unit::TestCase
3
+ class TestMechanizeCookieJar < Mechanize::TestCase
5
4
 
6
5
  def setup
7
- @jar = Mechanize::CookieJar.new
8
- @tmpdir = Dir.mktmpdir
9
-
10
- @orig_dir = Dir.pwd
11
- Dir.chdir @tmpdir
12
- end
6
+ super
13
7
 
14
- def teardown
15
- Dir.chdir @orig_dir
16
- FileUtils.remove_entry_secure @tmpdir
8
+ @jar = Mechanize::CookieJar.new
17
9
  end
18
10
 
19
11
  def cookie_values(options = {})
@@ -22,185 +14,242 @@ class TestMechanizeCookieJar < MiniTest::Unit::TestCase
22
14
  :value => 'Bar',
23
15
  :path => '/',
24
16
  :expires => Time.now + (10 * 86400),
17
+ :for_domain => true,
25
18
  :domain => 'rubyforge.org'
26
19
  }.merge(options)
27
20
  end
28
21
 
29
- def cookie_from_hash(hash)
30
- c = Mechanize::Cookie.new(hash[:name], hash[:value])
31
- hash.each { |k,v|
32
- next if k == :name || k == :value
33
- c.send("#{k}=", v)
34
- }
35
- c
36
- end
37
-
38
22
  def test_two_cookies_same_domain_and_name_different_paths
39
- url = URI.parse('http://rubyforge.org/')
23
+ url = URI 'http://rubyforge.org/'
40
24
 
41
- cookie = cookie_from_hash(cookie_values)
25
+ cookie = Mechanize::Cookie.new(cookie_values)
42
26
  @jar.add(url, cookie)
43
- @jar.add(url, cookie_from_hash(cookie_values(:path => '/onetwo')))
27
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:path => '/onetwo')))
44
28
 
45
29
  assert_equal(1, @jar.cookies(url).length)
46
- assert_equal 2, @jar.cookies(URI.parse('http://rubyforge.org/onetwo')).length
30
+ assert_equal 2, @jar.cookies(URI('http://rubyforge.org/onetwo')).length
47
31
  end
48
32
 
49
33
  def test_domain_case
50
- url = URI.parse('http://rubyforge.org/')
34
+ url = URI 'http://rubyforge.org/'
51
35
 
52
36
  # Add one cookie with an expiration date in the future
53
- cookie = cookie_from_hash(cookie_values)
37
+ cookie = Mechanize::Cookie.new(cookie_values)
54
38
  @jar.add(url, cookie)
55
39
  assert_equal(1, @jar.cookies(url).length)
56
40
 
57
- @jar.add(url, cookie_from_hash(
41
+ @jar.add(url, Mechanize::Cookie.new(
58
42
  cookie_values(:domain => 'RuByForge.Org', :name => 'aaron')))
59
43
 
60
44
  assert_equal(2, @jar.cookies(url).length)
61
45
 
62
- url2 = URI.parse('http://RuByFoRgE.oRg/')
46
+ url2 = URI 'http://RuByFoRgE.oRg/'
63
47
  assert_equal(2, @jar.cookies(url2).length)
64
48
  end
65
49
 
50
+ def test_no_domain_case
51
+ url = URI.parse('http://www.rubyforge.org/')
52
+
53
+ @jar.add(url, Mechanize::Cookie.new(
54
+ cookie_values(:domain => 'rubyforge.org', :for_domain => false)))
55
+
56
+ assert_equal(0, @jar.cookies(url).length)
57
+ end
58
+
66
59
  def test_empty_value
67
60
  values = cookie_values(:value => "")
68
- url = URI.parse('http://rubyforge.org/')
61
+ url = URI 'http://rubyforge.org/'
69
62
 
70
63
  # Add one cookie with an expiration date in the future
71
- cookie = cookie_from_hash(values)
64
+ cookie = Mechanize::Cookie.new(values)
72
65
  @jar.add(url, cookie)
73
66
  assert_equal(1, @jar.cookies(url).length)
74
67
 
75
- @jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
76
- :name => 'aaron'
77
- ) ) )
68
+ @jar.add url, Mechanize::Cookie.new(values.merge(:domain => 'RuByForge.Org',
69
+ :name => 'aaron'))
78
70
 
79
71
  assert_equal(2, @jar.cookies(url).length)
80
72
 
81
- url2 = URI.parse('http://RuByFoRgE.oRg/')
73
+ url2 = URI 'http://RuByFoRgE.oRg/'
82
74
  assert_equal(2, @jar.cookies(url2).length)
83
75
  end
84
76
 
85
77
  def test_add_future_cookies
86
- url = URI.parse('http://rubyforge.org/')
78
+ url = URI 'http://rubyforge.org/'
87
79
 
88
80
  # Add one cookie with an expiration date in the future
89
- cookie = cookie_from_hash(cookie_values)
81
+ cookie = Mechanize::Cookie.new(cookie_values)
90
82
  @jar.add(url, cookie)
91
83
  assert_equal(1, @jar.cookies(url).length)
92
84
 
93
85
  # Add the same cookie, and we should still only have one
94
- @jar.add(url, cookie_from_hash(cookie_values))
86
+ @jar.add(url, Mechanize::Cookie.new(cookie_values))
95
87
  assert_equal(1, @jar.cookies(url).length)
96
88
 
97
89
  # Make sure we can get the cookie from different paths
98
- assert_equal(1, @jar.cookies(URI.parse('http://rubyforge.org/login')).length)
90
+ assert_equal(1, @jar.cookies(URI('http://rubyforge.org/login')).length)
99
91
 
100
92
  # Make sure we can't get the cookie from different domains
101
- assert_equal(0, @jar.cookies(URI.parse('http://google.com/')).length)
93
+ assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
102
94
  end
103
95
 
104
96
  def test_add_multiple_cookies
105
- url = URI.parse('http://rubyforge.org/')
97
+ url = URI 'http://rubyforge.org/'
106
98
 
107
99
  # Add one cookie with an expiration date in the future
108
- cookie = cookie_from_hash(cookie_values)
100
+ cookie = Mechanize::Cookie.new(cookie_values)
109
101
  @jar.add(url, cookie)
110
102
  assert_equal(1, @jar.cookies(url).length)
111
103
 
112
104
  # Add the same cookie, and we should still only have one
113
- @jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
105
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
114
106
  assert_equal(2, @jar.cookies(url).length)
115
107
 
116
108
  # Make sure we can get the cookie from different paths
117
- assert_equal(2, @jar.cookies(URI.parse('http://rubyforge.org/login')).length)
109
+ assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
118
110
 
119
111
  # Make sure we can't get the cookie from different domains
120
- assert_equal(0, @jar.cookies(URI.parse('http://google.com/')).length)
112
+ assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
121
113
  end
122
114
 
123
115
  def test_add_rejects_cookies_that_do_not_contain_an_embedded_dot
124
- url = URI.parse('http://rubyforge.org/')
116
+ url = URI 'http://rubyforge.org/'
125
117
 
126
- tld_cookie = cookie_from_hash(cookie_values(:domain => '.org'))
118
+ tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.org'))
127
119
  @jar.add(url, tld_cookie)
128
- single_dot_cookie = cookie_from_hash(cookie_values(:domain => '.'))
120
+ single_dot_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.'))
129
121
  @jar.add(url, single_dot_cookie)
130
122
 
131
123
  assert_equal(0, @jar.cookies(url).length)
132
124
  end
133
125
 
134
- def test_add_makes_exception_for_local_tld
135
- url = URI.parse('http://example.local')
126
+ def test_fall_back_rules_for_local_domains
127
+ url = URI 'http://www.example.local'
136
128
 
137
- tld_cookie = cookie_from_hash(cookie_values(:domain => '.local'))
129
+ tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.local'))
138
130
  @jar.add(url, tld_cookie)
139
131
 
132
+ assert_equal(0, @jar.cookies(url).length)
133
+
134
+ sld_cookie = Mechanize::Cookie.new(cookie_values(:domain => '.example.local'))
135
+ @jar.add(url, sld_cookie)
136
+
140
137
  assert_equal(1, @jar.cookies(url).length)
141
138
  end
142
139
 
143
140
  def test_add_makes_exception_for_localhost
144
- url = URI.parse('http://localhost')
141
+ url = URI 'http://localhost'
145
142
 
146
- tld_cookie = cookie_from_hash(cookie_values(:domain => 'localhost'))
143
+ tld_cookie = Mechanize::Cookie.new(cookie_values(:domain => 'localhost'))
147
144
  @jar.add(url, tld_cookie)
148
145
 
149
146
  assert_equal(1, @jar.cookies(url).length)
150
147
  end
151
148
 
152
149
  def test_add_cookie_for_the_parent_domain
153
- url = URI.parse('http://x.foo.com')
150
+ url = URI 'http://x.foo.com'
154
151
 
155
- cookie = cookie_from_hash(cookie_values(:domain => '.foo.com'))
152
+ cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
156
153
  @jar.add(url, cookie)
157
154
 
158
155
  assert_equal(1, @jar.cookies(url).length)
159
156
  end
160
157
 
161
- def test_add_rejects_cookies_from_a_nested_subdomain
162
- url = URI.parse('http://y.x.foo.com')
158
+ def test_add_does_not_reject_cookies_from_a_nested_subdomain
159
+ url = URI 'http://y.x.foo.com'
163
160
 
164
- cookie = cookie_from_hash(cookie_values(:domain => '.foo.com'))
161
+ cookie = Mechanize::Cookie.new(cookie_values(:domain => '.foo.com'))
165
162
  @jar.add(url, cookie)
166
163
 
167
- assert_equal(0, @jar.cookies(url).length)
164
+ assert_equal(1, @jar.cookies(url).length)
168
165
  end
169
166
 
170
- def test_cookie_without_leading_dot_does_not_match_subdomains
171
- url = URI.parse('http://admin.rubyforge.org/')
167
+ def test_cookie_without_leading_dot_does_not_cause_substring_match
168
+ url = URI 'http://arubyforge.org/'
172
169
 
173
- cookie = cookie_from_hash(cookie_values(:domain => 'rubyforge.org'))
170
+ cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
174
171
  @jar.add(url, cookie)
175
172
 
176
173
  assert_equal(0, @jar.cookies(url).length)
177
174
  end
178
175
 
176
+ def test_cookie_without_leading_dot_matches_subdomains
177
+ url = URI 'http://admin.rubyforge.org/'
178
+
179
+ cookie = Mechanize::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
180
+ @jar.add(url, cookie)
181
+
182
+ assert_equal(1, @jar.cookies(url).length)
183
+ end
184
+
179
185
  def test_cookies_with_leading_dot_match_subdomains
180
- url = URI.parse('http://admin.rubyforge.org/')
186
+ url = URI 'http://admin.rubyforge.org/'
187
+
188
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
189
+
190
+ assert_equal(1, @jar.cookies(url).length)
191
+ end
192
+
193
+ def test_cookies_with_leading_dot_match_parent_domains
194
+ url = URI 'http://rubyforge.org/'
195
+
196
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
197
+
198
+ assert_equal(1, @jar.cookies(url).length)
199
+ end
200
+
201
+ def test_cookies_with_leading_dot_match_parent_domains_exactly
202
+ url = URI 'http://arubyforge.org/'
203
+
204
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
205
+
206
+ assert_equal(0, @jar.cookies(url).length)
207
+ end
208
+
209
+ def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
210
+ url = URI 'http://192.168.0.1/'
211
+
212
+ cookie = Mechanize::Cookie.new(cookie_values(:domain => '192.168.0.1'))
213
+ @jar.add(url, cookie)
214
+
215
+ assert_equal(1, @jar.cookies(url).length)
216
+ end
217
+
218
+ def test_cookie_for_ipv4_address_does_not_cause_subdomain_match
219
+ url = URI 'http://192.168.0.1/'
220
+
221
+ cookie = Mechanize::Cookie.new(cookie_values(:domain => '.0.1'))
222
+ @jar.add(url, cookie)
181
223
 
182
- @jar.add(url, cookie_from_hash(cookie_values(:domain => '.rubyforge.org')))
224
+ assert_equal(0, @jar.cookies(url).length)
225
+ end
226
+
227
+ def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
228
+ url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
229
+
230
+ cookie = Mechanize::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]'))
231
+ @jar.add(url, cookie)
183
232
 
184
233
  assert_equal(1, @jar.cookies(url).length)
185
234
  end
186
235
 
187
236
  def test_cookies_dot
188
- url = URI.parse('http://www.host.example/')
237
+ url = URI 'http://www.host.example/'
189
238
 
190
239
  @jar.add(url,
191
- cookie_from_hash(cookie_values(:domain => 'www.host.example')))
240
+ Mechanize::Cookie.new(cookie_values(:domain => 'www.host.example')))
192
241
 
193
- url = URI.parse('http://wwwxhost.example/')
242
+ url = URI 'http://wwwxhost.example/'
194
243
  assert_equal(0, @jar.cookies(url).length)
195
244
  end
196
245
 
197
246
  def test_clear_bang
198
- url = URI.parse('http://rubyforge.org/')
247
+ url = URI 'http://rubyforge.org/'
199
248
 
200
249
  # Add one cookie with an expiration date in the future
201
- cookie = cookie_from_hash(cookie_values)
250
+ cookie = Mechanize::Cookie.new(cookie_values)
202
251
  @jar.add(url, cookie)
203
- @jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
252
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
204
253
  assert_equal(2, @jar.cookies(url).length)
205
254
 
206
255
  @jar.clear!
@@ -209,177 +258,188 @@ class TestMechanizeCookieJar < MiniTest::Unit::TestCase
209
258
  end
210
259
 
211
260
  def test_save_cookies_yaml
212
- url = URI.parse('http://rubyforge.org/')
261
+ url = URI 'http://rubyforge.org/'
213
262
 
214
263
  # Add one cookie with an expiration date in the future
215
- cookie = cookie_from_hash(cookie_values)
216
- s_cookie = cookie_from_hash(cookie_values(:name => 'Bar',
264
+ cookie = Mechanize::Cookie.new(cookie_values)
265
+ s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
217
266
  :expires => nil,
218
267
  :session => true))
219
268
 
220
269
  @jar.add(url, cookie)
221
270
  @jar.add(url, s_cookie)
222
- @jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
271
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
223
272
 
224
273
  assert_equal(3, @jar.cookies(url).length)
225
274
 
226
- @jar.save_as("cookies.yml")
275
+ in_tmpdir do
276
+ @jar.save_as("cookies.yml")
227
277
 
228
- jar = Mechanize::CookieJar.new
229
- jar.load("cookies.yml")
230
- assert_equal(2, jar.cookies(url).length)
278
+ jar = Mechanize::CookieJar.new
279
+ jar.load("cookies.yml")
280
+ assert_equal(2, jar.cookies(url).length)
281
+ end
231
282
 
232
283
  assert_equal(3, @jar.cookies(url).length)
233
284
  end
234
285
 
235
286
  def test_save_cookies_cookiestxt
236
- url = URI.parse('http://rubyforge.org/')
287
+ url = URI 'http://rubyforge.org/'
237
288
 
238
289
  # Add one cookie with an expiration date in the future
239
- cookie = cookie_from_hash(cookie_values)
240
- s_cookie = cookie_from_hash(cookie_values(:name => 'Bar',
290
+ cookie = Mechanize::Cookie.new(cookie_values)
291
+ s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
241
292
  :expires => nil,
242
293
  :session => true))
243
294
 
244
295
  @jar.add(url, cookie)
245
296
  @jar.add(url, s_cookie)
246
- @jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
297
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
247
298
 
248
299
  assert_equal(3, @jar.cookies(url).length)
249
300
 
250
- @jar.save_as("cookies.txt", :cookiestxt)
301
+ in_tmpdir do
302
+ @jar.save_as("cookies.txt", :cookiestxt)
251
303
 
252
- jar = Mechanize::CookieJar.new
253
- jar.load("cookies.txt", :cookiestxt) # HACK test the format
254
- assert_equal(2, jar.cookies(url).length)
304
+ jar = Mechanize::CookieJar.new
305
+ jar.load("cookies.txt", :cookiestxt) # HACK test the format
306
+ assert_equal(2, jar.cookies(url).length)
307
+ end
255
308
 
256
309
  assert_equal(3, @jar.cookies(url).length)
257
310
  end
258
311
 
259
312
  def test_expire_cookies
260
- url = URI.parse('http://rubyforge.org/')
313
+ url = URI 'http://rubyforge.org/'
261
314
 
262
315
  # Add one cookie with an expiration date in the future
263
- cookie = cookie_from_hash(cookie_values)
316
+ cookie = Mechanize::Cookie.new(cookie_values)
264
317
  @jar.add(url, cookie)
265
318
  assert_equal(1, @jar.cookies(url).length)
266
319
 
267
320
  # Add a second cookie
268
- @jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
321
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
269
322
  assert_equal(2, @jar.cookies(url).length)
270
323
 
271
324
  # Make sure we can get the cookie from different paths
272
- assert_equal(2, @jar.cookies(URI.parse('http://rubyforge.org/login')).length)
325
+ assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
273
326
 
274
327
  # Expire the first cookie
275
- @jar.add(url, cookie_from_hash(
328
+ @jar.add(url, Mechanize::Cookie.new(
276
329
  cookie_values(:expires => Time.now - (10 * 86400))))
277
330
  assert_equal(1, @jar.cookies(url).length)
278
331
 
279
332
  # Expire the second cookie
280
- @jar.add(url, cookie_from_hash(
333
+ @jar.add(url, Mechanize::Cookie.new(
281
334
  cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
282
335
  assert_equal(0, @jar.cookies(url).length)
283
336
  end
284
337
 
285
338
  def test_session_cookies
286
339
  values = cookie_values(:expires => nil)
287
- url = URI.parse('http://rubyforge.org/')
340
+ url = URI 'http://rubyforge.org/'
288
341
 
289
342
  # Add one cookie with an expiration date in the future
290
- cookie = cookie_from_hash(values)
343
+ cookie = Mechanize::Cookie.new(values)
291
344
  @jar.add(url, cookie)
292
345
  assert_equal(1, @jar.cookies(url).length)
293
346
 
294
347
  # Add a second cookie
295
- @jar.add(url, cookie_from_hash(values.merge(:name => 'Baz')))
348
+ @jar.add(url, Mechanize::Cookie.new(values.merge(:name => 'Baz')))
296
349
  assert_equal(2, @jar.cookies(url).length)
297
350
 
298
351
  # Make sure we can get the cookie from different paths
299
- assert_equal(2, @jar.cookies(URI.parse('http://rubyforge.org/login')).length)
352
+ assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
300
353
 
301
354
  # Expire the first cookie
302
- @jar.add(url, cookie_from_hash(values.merge(:expires => Time.now - (10 * 86400))))
355
+ @jar.add(url, Mechanize::Cookie.new(values.merge(:expires => Time.now - (10 * 86400))))
303
356
  assert_equal(1, @jar.cookies(url).length)
304
357
 
305
358
  # Expire the second cookie
306
- @jar.add(url, cookie_from_hash(
359
+ @jar.add(url, Mechanize::Cookie.new(
307
360
  values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
308
361
  assert_equal(0, @jar.cookies(url).length)
309
362
 
310
363
  # When given a URI with a blank path, CookieJar#cookies should return
311
364
  # cookies with the path '/':
312
- url = URI.parse('http://rubyforge.org')
365
+ url = URI 'http://rubyforge.org'
313
366
  assert_equal '', url.path
314
367
  assert_equal(0, @jar.cookies(url).length)
315
368
  # Now add a cookie with the path set to '/':
316
- @jar.add(url, cookie_from_hash(values.merge( :name => 'has_root_path',
369
+ @jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'has_root_path',
317
370
  :path => '/')))
318
371
  assert_equal(1, @jar.cookies(url).length)
319
372
  end
320
373
 
321
374
  def test_paths
322
375
  values = cookie_values(:path => "/login", :expires => nil)
323
- url = URI.parse('http://rubyforge.org/login')
376
+ url = URI 'http://rubyforge.org/login'
324
377
 
325
378
  # Add one cookie with an expiration date in the future
326
- cookie = cookie_from_hash(values)
379
+ cookie = Mechanize::Cookie.new(values)
327
380
  @jar.add(url, cookie)
328
381
  assert_equal(1, @jar.cookies(url).length)
329
382
 
330
383
  # Add a second cookie
331
- @jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
384
+ @jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz' )))
332
385
  assert_equal(2, @jar.cookies(url).length)
333
386
 
334
387
  # Make sure we don't get the cookie in a different path
335
- assert_equal(0, @jar.cookies(URI.parse('http://rubyforge.org/hello')).length)
336
- assert_equal(0, @jar.cookies(URI.parse('http://rubyforge.org/')).length)
388
+ assert_equal(0, @jar.cookies(URI('http://rubyforge.org/hello')).length)
389
+ assert_equal(0, @jar.cookies(URI('http://rubyforge.org/')).length)
337
390
 
338
391
  # Expire the first cookie
339
- @jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
392
+ @jar.add(url, Mechanize::Cookie.new(values.merge( :expires => Time.now - (10 * 86400))))
340
393
  assert_equal(1, @jar.cookies(url).length)
341
394
 
342
395
  # Expire the second cookie
343
- @jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
396
+ @jar.add(url, Mechanize::Cookie.new(values.merge( :name => 'Baz',
344
397
  :expires => Time.now - (10 * 86400))))
345
398
  assert_equal(0, @jar.cookies(url).length)
346
399
  end
347
400
 
348
401
  def test_save_and_read_cookiestxt
349
- url = URI.parse('http://rubyforge.org/')
402
+ url = URI 'http://rubyforge.org/'
350
403
 
351
404
  # Add one cookie with an expiration date in the future
352
- cookie = cookie_from_hash(cookie_values)
405
+ cookie = Mechanize::Cookie.new(cookie_values)
353
406
  @jar.add(url, cookie)
354
- @jar.add(url, cookie_from_hash(cookie_values(:name => 'Baz')))
407
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
355
408
  assert_equal(2, @jar.cookies(url).length)
356
409
 
357
- @jar.save_as("cookies.txt", :cookiestxt)
358
- @jar.clear!
410
+ in_tmpdir do
411
+ @jar.save_as("cookies.txt", :cookiestxt)
412
+ @jar.clear!
413
+
414
+ @jar.load("cookies.txt", :cookiestxt)
415
+ end
359
416
 
360
- @jar.load("cookies.txt", :cookiestxt)
361
417
  assert_equal(2, @jar.cookies(url).length)
362
418
  end
363
419
 
364
420
  def test_save_and_read_cookiestxt_with_session_cookies
365
- url = URI.parse('http://rubyforge.org/')
421
+ url = URI 'http://rubyforge.org/'
366
422
 
367
- @jar.add(url, cookie_from_hash(cookie_values(:expires => nil)))
368
- @jar.save_as("cookies.txt", :cookiestxt)
369
- @jar.clear!
423
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:expires => nil)))
424
+
425
+ in_tmpdir do
426
+ @jar.save_as("cookies.txt", :cookiestxt)
427
+ @jar.clear!
428
+
429
+ @jar.load("cookies.txt", :cookiestxt)
430
+ end
370
431
 
371
- @jar.load("cookies.txt", :cookiestxt)
372
432
  assert_equal(1, @jar.cookies(url).length)
373
433
  assert_nil @jar.cookies(url).first.expires
374
434
  end
375
435
 
376
436
  def test_save_and_read_expired_cookies
377
- url = URI.parse('http://rubyforge.org/')
437
+ url = URI 'http://rubyforge.org/'
378
438
 
379
439
  @jar.jar['rubyforge.org'] = {}
380
440
 
381
441
 
382
- @jar.add url, cookie_from_hash(cookie_values)
442
+ @jar.add url, Mechanize::Cookie.new(cookie_values)
383
443
 
384
444
  # HACK no asertion
385
445
  end
@@ -387,15 +447,31 @@ class TestMechanizeCookieJar < MiniTest::Unit::TestCase
387
447
  def test_ssl_cookies
388
448
  # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
389
449
  values = cookie_values(:expires => nil)
390
- values_ssl = values.merge(:domain => "#{values[:domain]}:443")
391
- url = URI.parse('https://rubyforge.org/login')
450
+ values_ssl = values.merge(:name => 'Baz', :domain => "#{values[:domain]}:443")
451
+ url = URI 'https://rubyforge.org/login'
392
452
 
393
- cookie = cookie_from_hash(values)
453
+ cookie = Mechanize::Cookie.new(values)
394
454
  @jar.add(url, cookie)
395
455
  assert_equal(1, @jar.cookies(url).length, "did not handle SSL cookie")
396
456
 
397
- cookie = cookie_from_hash(values_ssl)
457
+ cookie = Mechanize::Cookie.new(values_ssl)
398
458
  @jar.add(url, cookie)
399
459
  assert_equal(2, @jar.cookies(url).length, "did not handle SSL cookie with :443")
400
460
  end
461
+
462
+ def test_secure_cookie
463
+ nurl = URI 'http://rubyforge.org/login'
464
+ surl = URI 'https://rubyforge.org/login'
465
+
466
+ ncookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo1'))
467
+ scookie = Mechanize::Cookie.new(cookie_values(:name => 'Foo2', :secure => true))
468
+
469
+ @jar.add(nurl, ncookie)
470
+ @jar.add(nurl, scookie)
471
+ @jar.add(surl, ncookie)
472
+ @jar.add(surl, scookie)
473
+
474
+ assert_equal('Foo1', @jar.cookies(nurl).map { |c| c.name }.sort.join(' ') )
475
+ assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
476
+ end
401
477
  end