diamond-mechanize 2.1 → 2.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 (147) hide show
  1. metadata +222 -167
  2. data/Rakefile +0 -49
  3. data/lib/mechanize/content_type_error.rb +0 -13
  4. data/lib/mechanize/cookie.rb +0 -232
  5. data/lib/mechanize/cookie_jar.rb +0 -194
  6. data/lib/mechanize/download.rb +0 -59
  7. data/lib/mechanize/element_matcher.rb +0 -36
  8. data/lib/mechanize/file.rb +0 -65
  9. data/lib/mechanize/file_connection.rb +0 -17
  10. data/lib/mechanize/file_request.rb +0 -26
  11. data/lib/mechanize/file_response.rb +0 -74
  12. data/lib/mechanize/file_saver.rb +0 -39
  13. data/lib/mechanize/form/button.rb +0 -6
  14. data/lib/mechanize/form/check_box.rb +0 -12
  15. data/lib/mechanize/form/field.rb +0 -54
  16. data/lib/mechanize/form/file_upload.rb +0 -21
  17. data/lib/mechanize/form/hidden.rb +0 -3
  18. data/lib/mechanize/form/image_button.rb +0 -19
  19. data/lib/mechanize/form/keygen.rb +0 -34
  20. data/lib/mechanize/form/multi_select_list.rb +0 -94
  21. data/lib/mechanize/form/option.rb +0 -50
  22. data/lib/mechanize/form/radio_button.rb +0 -55
  23. data/lib/mechanize/form/reset.rb +0 -3
  24. data/lib/mechanize/form/select_list.rb +0 -44
  25. data/lib/mechanize/form/submit.rb +0 -3
  26. data/lib/mechanize/form/text.rb +0 -3
  27. data/lib/mechanize/form/textarea.rb +0 -3
  28. data/lib/mechanize/form.rb +0 -543
  29. data/lib/mechanize/headers.rb +0 -23
  30. data/lib/mechanize/history.rb +0 -82
  31. data/lib/mechanize/http/agent.rb +0 -1004
  32. data/lib/mechanize/http/auth_challenge.rb +0 -59
  33. data/lib/mechanize/http/auth_realm.rb +0 -31
  34. data/lib/mechanize/http/content_disposition_parser.rb +0 -188
  35. data/lib/mechanize/http/www_authenticate_parser.rb +0 -155
  36. data/lib/mechanize/http.rb +0 -8
  37. data/lib/mechanize/monkey_patch.rb +0 -16
  38. data/lib/mechanize/page/base.rb +0 -7
  39. data/lib/mechanize/page/frame.rb +0 -27
  40. data/lib/mechanize/page/image.rb +0 -30
  41. data/lib/mechanize/page/label.rb +0 -20
  42. data/lib/mechanize/page/link.rb +0 -98
  43. data/lib/mechanize/page/meta_refresh.rb +0 -68
  44. data/lib/mechanize/page.rb +0 -440
  45. data/lib/mechanize/parser.rb +0 -173
  46. data/lib/mechanize/pluggable_parsers.rb +0 -144
  47. data/lib/mechanize/redirect_limit_reached_error.rb +0 -19
  48. data/lib/mechanize/redirect_not_get_or_head_error.rb +0 -21
  49. data/lib/mechanize/response_code_error.rb +0 -21
  50. data/lib/mechanize/response_read_error.rb +0 -27
  51. data/lib/mechanize/robots_disallowed_error.rb +0 -28
  52. data/lib/mechanize/test_case.rb +0 -663
  53. data/lib/mechanize/unauthorized_error.rb +0 -3
  54. data/lib/mechanize/unsupported_scheme_error.rb +0 -6
  55. data/lib/mechanize/util.rb +0 -101
  56. data/lib/mechanize.rb +0 -1079
  57. data/test/data/htpasswd +0 -1
  58. data/test/data/server.crt +0 -16
  59. data/test/data/server.csr +0 -12
  60. data/test/data/server.key +0 -15
  61. data/test/data/server.pem +0 -15
  62. data/test/htdocs/alt_text.html +0 -10
  63. data/test/htdocs/bad_form_test.html +0 -9
  64. data/test/htdocs/button.jpg +0 -0
  65. data/test/htdocs/canonical_uri.html +0 -9
  66. data/test/htdocs/dir with spaces/foo.html +0 -1
  67. data/test/htdocs/empty_form.html +0 -6
  68. data/test/htdocs/file_upload.html +0 -26
  69. data/test/htdocs/find_link.html +0 -41
  70. data/test/htdocs/form_multi_select.html +0 -16
  71. data/test/htdocs/form_multival.html +0 -37
  72. data/test/htdocs/form_no_action.html +0 -18
  73. data/test/htdocs/form_no_input_name.html +0 -16
  74. data/test/htdocs/form_order_test.html +0 -11
  75. data/test/htdocs/form_select.html +0 -16
  76. data/test/htdocs/form_set_fields.html +0 -14
  77. data/test/htdocs/form_test.html +0 -188
  78. data/test/htdocs/frame_referer_test.html +0 -10
  79. data/test/htdocs/frame_test.html +0 -30
  80. data/test/htdocs/google.html +0 -13
  81. data/test/htdocs/index.html +0 -6
  82. data/test/htdocs/link with space.html +0 -5
  83. data/test/htdocs/meta_cookie.html +0 -11
  84. data/test/htdocs/no_title_test.html +0 -6
  85. data/test/htdocs/noindex.html +0 -9
  86. data/test/htdocs/rails_3_encoding_hack_form_test.html +0 -27
  87. data/test/htdocs/relative/tc_relative_links.html +0 -21
  88. data/test/htdocs/robots.html +0 -8
  89. data/test/htdocs/robots.txt +0 -2
  90. data/test/htdocs/tc_bad_charset.html +0 -9
  91. data/test/htdocs/tc_bad_links.html +0 -5
  92. data/test/htdocs/tc_base_link.html +0 -8
  93. data/test/htdocs/tc_blank_form.html +0 -11
  94. data/test/htdocs/tc_charset.html +0 -6
  95. data/test/htdocs/tc_checkboxes.html +0 -19
  96. data/test/htdocs/tc_encoded_links.html +0 -5
  97. data/test/htdocs/tc_field_precedence.html +0 -11
  98. data/test/htdocs/tc_follow_meta.html +0 -8
  99. data/test/htdocs/tc_form_action.html +0 -48
  100. data/test/htdocs/tc_links.html +0 -19
  101. data/test/htdocs/tc_meta_in_body.html +0 -9
  102. data/test/htdocs/tc_pretty_print.html +0 -17
  103. data/test/htdocs/tc_referer.html +0 -16
  104. data/test/htdocs/tc_relative_links.html +0 -19
  105. data/test/htdocs/tc_textarea.html +0 -23
  106. data/test/htdocs/test_click.html +0 -11
  107. data/test/htdocs/unusual______.html +0 -5
  108. data/test/test_mechanize.rb +0 -1164
  109. data/test/test_mechanize_cookie.rb +0 -451
  110. data/test/test_mechanize_cookie_jar.rb +0 -483
  111. data/test/test_mechanize_download.rb +0 -43
  112. data/test/test_mechanize_file.rb +0 -61
  113. data/test/test_mechanize_file_connection.rb +0 -21
  114. data/test/test_mechanize_file_request.rb +0 -19
  115. data/test/test_mechanize_file_saver.rb +0 -21
  116. data/test/test_mechanize_form.rb +0 -875
  117. data/test/test_mechanize_form_check_box.rb +0 -38
  118. data/test/test_mechanize_form_encoding.rb +0 -114
  119. data/test/test_mechanize_form_field.rb +0 -63
  120. data/test/test_mechanize_form_file_upload.rb +0 -20
  121. data/test/test_mechanize_form_image_button.rb +0 -12
  122. data/test/test_mechanize_form_keygen.rb +0 -32
  123. data/test/test_mechanize_form_multi_select_list.rb +0 -84
  124. data/test/test_mechanize_form_option.rb +0 -55
  125. data/test/test_mechanize_form_radio_button.rb +0 -78
  126. data/test/test_mechanize_form_select_list.rb +0 -76
  127. data/test/test_mechanize_form_textarea.rb +0 -52
  128. data/test/test_mechanize_headers.rb +0 -35
  129. data/test/test_mechanize_history.rb +0 -103
  130. data/test/test_mechanize_http_agent.rb +0 -1225
  131. data/test/test_mechanize_http_auth_challenge.rb +0 -39
  132. data/test/test_mechanize_http_auth_realm.rb +0 -49
  133. data/test/test_mechanize_http_content_disposition_parser.rb +0 -118
  134. data/test/test_mechanize_http_www_authenticate_parser.rb +0 -146
  135. data/test/test_mechanize_link.rb +0 -80
  136. data/test/test_mechanize_page.rb +0 -118
  137. data/test/test_mechanize_page_encoding.rb +0 -182
  138. data/test/test_mechanize_page_frame.rb +0 -16
  139. data/test/test_mechanize_page_link.rb +0 -390
  140. data/test/test_mechanize_page_meta_refresh.rb +0 -127
  141. data/test/test_mechanize_parser.rb +0 -289
  142. data/test/test_mechanize_pluggable_parser.rb +0 -52
  143. data/test/test_mechanize_redirect_limit_reached_error.rb +0 -24
  144. data/test/test_mechanize_redirect_not_get_or_head_error.rb +0 -14
  145. data/test/test_mechanize_subclass.rb +0 -22
  146. data/test/test_mechanize_util.rb +0 -103
  147. data/test/test_multi_select.rb +0 -119
@@ -1,451 +0,0 @@
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
-
@@ -1,483 +0,0 @@
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