eric-mechanize 0.9.3.20090623142847

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. data/CHANGELOG.rdoc +504 -0
  2. data/EXAMPLES.rdoc +171 -0
  3. data/FAQ.rdoc +11 -0
  4. data/GUIDE.rdoc +122 -0
  5. data/LICENSE.rdoc +340 -0
  6. data/Manifest.txt +169 -0
  7. data/README.rdoc +60 -0
  8. data/Rakefile +43 -0
  9. data/examples/flickr_upload.rb +23 -0
  10. data/examples/mech-dump.rb +7 -0
  11. data/examples/proxy_req.rb +9 -0
  12. data/examples/rubyforge.rb +21 -0
  13. data/examples/spider.rb +11 -0
  14. data/lib/mechanize.rb +7 -0
  15. data/lib/www/mechanize.rb +619 -0
  16. data/lib/www/mechanize/chain.rb +34 -0
  17. data/lib/www/mechanize/chain/auth_headers.rb +80 -0
  18. data/lib/www/mechanize/chain/body_decoding_handler.rb +48 -0
  19. data/lib/www/mechanize/chain/connection_resolver.rb +78 -0
  20. data/lib/www/mechanize/chain/custom_headers.rb +23 -0
  21. data/lib/www/mechanize/chain/handler.rb +9 -0
  22. data/lib/www/mechanize/chain/header_resolver.rb +53 -0
  23. data/lib/www/mechanize/chain/parameter_resolver.rb +24 -0
  24. data/lib/www/mechanize/chain/post_connect_hook.rb +0 -0
  25. data/lib/www/mechanize/chain/pre_connect_hook.rb +22 -0
  26. data/lib/www/mechanize/chain/request_resolver.rb +32 -0
  27. data/lib/www/mechanize/chain/response_body_parser.rb +40 -0
  28. data/lib/www/mechanize/chain/response_header_handler.rb +50 -0
  29. data/lib/www/mechanize/chain/response_reader.rb +41 -0
  30. data/lib/www/mechanize/chain/ssl_resolver.rb +42 -0
  31. data/lib/www/mechanize/chain/uri_resolver.rb +77 -0
  32. data/lib/www/mechanize/content_type_error.rb +16 -0
  33. data/lib/www/mechanize/cookie.rb +72 -0
  34. data/lib/www/mechanize/cookie_jar.rb +191 -0
  35. data/lib/www/mechanize/file.rb +73 -0
  36. data/lib/www/mechanize/file_response.rb +62 -0
  37. data/lib/www/mechanize/file_saver.rb +39 -0
  38. data/lib/www/mechanize/form.rb +360 -0
  39. data/lib/www/mechanize/form/button.rb +8 -0
  40. data/lib/www/mechanize/form/check_box.rb +13 -0
  41. data/lib/www/mechanize/form/field.rb +28 -0
  42. data/lib/www/mechanize/form/file_upload.rb +24 -0
  43. data/lib/www/mechanize/form/image_button.rb +23 -0
  44. data/lib/www/mechanize/form/multi_select_list.rb +69 -0
  45. data/lib/www/mechanize/form/option.rb +51 -0
  46. data/lib/www/mechanize/form/radio_button.rb +38 -0
  47. data/lib/www/mechanize/form/select_list.rb +45 -0
  48. data/lib/www/mechanize/headers.rb +12 -0
  49. data/lib/www/mechanize/history.rb +67 -0
  50. data/lib/www/mechanize/inspect.rb +90 -0
  51. data/lib/www/mechanize/monkey_patch.rb +37 -0
  52. data/lib/www/mechanize/page.rb +181 -0
  53. data/lib/www/mechanize/page/base.rb +10 -0
  54. data/lib/www/mechanize/page/frame.rb +22 -0
  55. data/lib/www/mechanize/page/link.rb +50 -0
  56. data/lib/www/mechanize/page/meta.rb +51 -0
  57. data/lib/www/mechanize/pluggable_parsers.rb +103 -0
  58. data/lib/www/mechanize/redirect_limit_reached_error.rb +18 -0
  59. data/lib/www/mechanize/redirect_not_get_or_head_error.rb +20 -0
  60. data/lib/www/mechanize/response_code_error.rb +25 -0
  61. data/lib/www/mechanize/unsupported_scheme_error.rb +10 -0
  62. data/lib/www/mechanize/util.rb +76 -0
  63. data/mechanize.gemspec +41 -0
  64. data/test/chain/test_argument_validator.rb +14 -0
  65. data/test/chain/test_auth_headers.rb +25 -0
  66. data/test/chain/test_custom_headers.rb +18 -0
  67. data/test/chain/test_header_resolver.rb +28 -0
  68. data/test/chain/test_parameter_resolver.rb +35 -0
  69. data/test/chain/test_request_resolver.rb +29 -0
  70. data/test/chain/test_response_reader.rb +24 -0
  71. data/test/data/htpasswd +1 -0
  72. data/test/data/server.crt +16 -0
  73. data/test/data/server.csr +12 -0
  74. data/test/data/server.key +15 -0
  75. data/test/data/server.pem +15 -0
  76. data/test/helper.rb +129 -0
  77. data/test/htdocs/alt_text.html +10 -0
  78. data/test/htdocs/bad_form_test.html +9 -0
  79. data/test/htdocs/button.jpg +0 -0
  80. data/test/htdocs/empty_form.html +6 -0
  81. data/test/htdocs/file_upload.html +26 -0
  82. data/test/htdocs/find_link.html +41 -0
  83. data/test/htdocs/form_multi_select.html +16 -0
  84. data/test/htdocs/form_multival.html +37 -0
  85. data/test/htdocs/form_no_action.html +18 -0
  86. data/test/htdocs/form_no_input_name.html +16 -0
  87. data/test/htdocs/form_select.html +16 -0
  88. data/test/htdocs/form_select_all.html +16 -0
  89. data/test/htdocs/form_select_none.html +17 -0
  90. data/test/htdocs/form_select_noopts.html +10 -0
  91. data/test/htdocs/form_set_fields.html +14 -0
  92. data/test/htdocs/form_test.html +188 -0
  93. data/test/htdocs/frame_test.html +30 -0
  94. data/test/htdocs/google.html +13 -0
  95. data/test/htdocs/iframe_test.html +16 -0
  96. data/test/htdocs/index.html +6 -0
  97. data/test/htdocs/link with space.html +5 -0
  98. data/test/htdocs/meta_cookie.html +11 -0
  99. data/test/htdocs/no_title_test.html +6 -0
  100. data/test/htdocs/relative/tc_relative_links.html +21 -0
  101. data/test/htdocs/tc_bad_links.html +5 -0
  102. data/test/htdocs/tc_base_link.html +8 -0
  103. data/test/htdocs/tc_blank_form.html +11 -0
  104. data/test/htdocs/tc_checkboxes.html +19 -0
  105. data/test/htdocs/tc_encoded_links.html +5 -0
  106. data/test/htdocs/tc_follow_meta.html +8 -0
  107. data/test/htdocs/tc_form_action.html +48 -0
  108. data/test/htdocs/tc_links.html +18 -0
  109. data/test/htdocs/tc_no_attributes.html +16 -0
  110. data/test/htdocs/tc_pretty_print.html +17 -0
  111. data/test/htdocs/tc_radiobuttons.html +17 -0
  112. data/test/htdocs/tc_referer.html +10 -0
  113. data/test/htdocs/tc_relative_links.html +19 -0
  114. data/test/htdocs/tc_textarea.html +23 -0
  115. data/test/htdocs/unusual______.html +5 -0
  116. data/test/servlets.rb +365 -0
  117. data/test/ssl_server.rb +48 -0
  118. data/test/test_authenticate.rb +71 -0
  119. data/test/test_bad_links.rb +25 -0
  120. data/test/test_blank_form.rb +16 -0
  121. data/test/test_checkboxes.rb +61 -0
  122. data/test/test_content_type.rb +13 -0
  123. data/test/test_cookie_class.rb +338 -0
  124. data/test/test_cookie_jar.rb +362 -0
  125. data/test/test_cookies.rb +123 -0
  126. data/test/test_encoded_links.rb +20 -0
  127. data/test/test_errors.rb +49 -0
  128. data/test/test_follow_meta.rb +108 -0
  129. data/test/test_form_action.rb +52 -0
  130. data/test/test_form_as_hash.rb +61 -0
  131. data/test/test_form_button.rb +38 -0
  132. data/test/test_form_no_inputname.rb +15 -0
  133. data/test/test_forms.rb +564 -0
  134. data/test/test_frames.rb +25 -0
  135. data/test/test_get_headers.rb +52 -0
  136. data/test/test_gzipping.rb +22 -0
  137. data/test/test_hash_api.rb +45 -0
  138. data/test/test_history.rb +142 -0
  139. data/test/test_history_added.rb +16 -0
  140. data/test/test_html_unscape_forms.rb +39 -0
  141. data/test/test_if_modified_since.rb +20 -0
  142. data/test/test_keep_alive.rb +31 -0
  143. data/test/test_links.rb +120 -0
  144. data/test/test_mech.rb +268 -0
  145. data/test/test_mechanize_file.rb +47 -0
  146. data/test/test_meta.rb +65 -0
  147. data/test/test_multi_select.rb +106 -0
  148. data/test/test_no_attributes.rb +13 -0
  149. data/test/test_option.rb +18 -0
  150. data/test/test_page.rb +124 -0
  151. data/test/test_pluggable_parser.rb +145 -0
  152. data/test/test_post_form.rb +34 -0
  153. data/test/test_pretty_print.rb +22 -0
  154. data/test/test_radiobutton.rb +75 -0
  155. data/test/test_redirect_limit_reached.rb +41 -0
  156. data/test/test_redirect_verb_handling.rb +45 -0
  157. data/test/test_referer.rb +39 -0
  158. data/test/test_relative_links.rb +40 -0
  159. data/test/test_request.rb +13 -0
  160. data/test/test_response_code.rb +52 -0
  161. data/test/test_save_file.rb +48 -0
  162. data/test/test_scheme.rb +48 -0
  163. data/test/test_select.rb +106 -0
  164. data/test/test_select_all.rb +15 -0
  165. data/test/test_select_none.rb +15 -0
  166. data/test/test_select_noopts.rb +16 -0
  167. data/test/test_set_fields.rb +44 -0
  168. data/test/test_ssl_server.rb +20 -0
  169. data/test/test_subclass.rb +14 -0
  170. data/test/test_textarea.rb +45 -0
  171. data/test/test_upload.rb +109 -0
  172. data/test/test_verbs.rb +25 -0
  173. metadata +314 -0
@@ -0,0 +1,362 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class CookieJarTest < Test::Unit::TestCase
4
+ def cookie_from_hash(hash)
5
+ c = WWW::Mechanize::Cookie.new(hash[:name], hash[:value])
6
+ hash.each { |k,v|
7
+ next if k == :name || k == :value
8
+ c.send("#{k}=", v)
9
+ }
10
+ c
11
+ end
12
+
13
+ def test_two_cookies_same_domain_and_name_different_paths
14
+ values = { :name => 'Foo',
15
+ :value => 'Bar',
16
+ :path => '/',
17
+ :expires => Time.now + (10 * 86400),
18
+ :domain => 'rubyforge.org'
19
+ }
20
+
21
+ url = URI.parse('http://rubyforge.org/')
22
+
23
+ jar = WWW::Mechanize::CookieJar.new
24
+ cookie = cookie_from_hash(values)
25
+ jar.add(url, cookie)
26
+ jar.add(url, cookie_from_hash(values.merge(:path => '/onetwo')))
27
+
28
+ assert_equal(1, jar.cookies(url).length)
29
+ assert_equal 2, jar.cookies(URI.parse('http://rubyforge.org/onetwo')).length
30
+ end
31
+
32
+ def test_domain_case
33
+ values = { :name => 'Foo',
34
+ :value => 'Bar',
35
+ :path => '/',
36
+ :expires => Time.now + (10 * 86400),
37
+ :domain => 'rubyforge.org'
38
+ }
39
+ url = URI.parse('http://rubyforge.org/')
40
+
41
+ jar = WWW::Mechanize::CookieJar.new
42
+ assert_equal(0, jar.cookies(url).length)
43
+
44
+ # Add one cookie with an expiration date in the future
45
+ cookie = cookie_from_hash(values)
46
+ jar.add(url, cookie)
47
+ assert_equal(1, jar.cookies(url).length)
48
+
49
+ jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
50
+ :name => 'aaron'
51
+ ) ) )
52
+
53
+ assert_equal(2, jar.cookies(url).length)
54
+
55
+ url2 = URI.parse('http://RuByFoRgE.oRg/')
56
+ assert_equal(2, jar.cookies(url2).length)
57
+ end
58
+
59
+ def test_empty_value
60
+ values = { :name => 'Foo',
61
+ :value => '',
62
+ :path => '/',
63
+ :expires => Time.now + (10 * 86400),
64
+ :domain => 'rubyforge.org'
65
+ }
66
+ url = URI.parse('http://rubyforge.org/')
67
+
68
+ jar = WWW::Mechanize::CookieJar.new
69
+ assert_equal(0, jar.cookies(url).length)
70
+
71
+ # Add one cookie with an expiration date in the future
72
+ cookie = cookie_from_hash(values)
73
+ jar.add(url, cookie)
74
+ assert_equal(1, jar.cookies(url).length)
75
+
76
+ jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
77
+ :name => 'aaron'
78
+ ) ) )
79
+
80
+ assert_equal(2, jar.cookies(url).length)
81
+
82
+ url2 = URI.parse('http://RuByFoRgE.oRg/')
83
+ assert_equal(2, jar.cookies(url2).length)
84
+ end
85
+
86
+ def test_add_future_cookies
87
+ values = { :name => 'Foo',
88
+ :value => 'Bar',
89
+ :path => '/',
90
+ :expires => Time.now + (10 * 86400),
91
+ :domain => 'rubyforge.org'
92
+ }
93
+ url = URI.parse('http://rubyforge.org/')
94
+
95
+ jar = WWW::Mechanize::CookieJar.new
96
+ assert_equal(0, jar.cookies(url).length)
97
+
98
+ # Add one cookie with an expiration date in the future
99
+ cookie = cookie_from_hash(values)
100
+ jar.add(url, cookie)
101
+ assert_equal(1, jar.cookies(url).length)
102
+
103
+ # Add the same cookie, and we should still only have one
104
+ jar.add(url, cookie_from_hash(values))
105
+ assert_equal(1, jar.cookies(url).length)
106
+
107
+ # Make sure we can get the cookie from different paths
108
+ assert_equal(1, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
109
+
110
+ # Make sure we can't get the cookie from different domains
111
+ assert_equal(0, jar.cookies(URI.parse('http://google.com/')).length)
112
+ end
113
+
114
+ def test_add_multiple_cookies
115
+ values = { :name => 'Foo',
116
+ :value => 'Bar',
117
+ :path => '/',
118
+ :expires => Time.now + (10 * 86400),
119
+ :domain => 'rubyforge.org'
120
+ }
121
+ url = URI.parse('http://rubyforge.org/')
122
+
123
+ jar = WWW::Mechanize::CookieJar.new
124
+ assert_equal(0, jar.cookies(url).length)
125
+
126
+ # Add one cookie with an expiration date in the future
127
+ cookie = cookie_from_hash(values)
128
+ jar.add(url, cookie)
129
+ assert_equal(1, jar.cookies(url).length)
130
+
131
+ # Add the same cookie, and we should still only have one
132
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
133
+ assert_equal(2, jar.cookies(url).length)
134
+
135
+ # Make sure we can get the cookie from different paths
136
+ assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
137
+
138
+ # Make sure we can't get the cookie from different domains
139
+ assert_equal(0, jar.cookies(URI.parse('http://google.com/')).length)
140
+ end
141
+
142
+ def test_clear_cookies
143
+ values = { :name => 'Foo',
144
+ :value => 'Bar',
145
+ :path => '/',
146
+ :expires => Time.now + (10 * 86400),
147
+ :domain => 'rubyforge.org'
148
+ }
149
+ url = URI.parse('http://rubyforge.org/')
150
+
151
+ jar = WWW::Mechanize::CookieJar.new
152
+ assert_equal(0, jar.cookies(url).length)
153
+
154
+ # Add one cookie with an expiration date in the future
155
+ cookie = cookie_from_hash(values)
156
+ jar.add(url, cookie)
157
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
158
+ assert_equal(2, jar.cookies(url).length)
159
+
160
+ jar.clear!
161
+
162
+ assert_equal(0, jar.cookies(url).length)
163
+ end
164
+
165
+ def test_save_cookies
166
+ values = { :name => 'Foo',
167
+ :value => 'Bar',
168
+ :path => '/',
169
+ :expires => Time.now + (10 * 86400),
170
+ :domain => 'rubyforge.org'
171
+ }
172
+ url = URI.parse('http://rubyforge.org/')
173
+
174
+ jar = WWW::Mechanize::CookieJar.new
175
+ assert_equal(0, jar.cookies(url).length)
176
+
177
+ # Add one cookie with an expiration date in the future
178
+ cookie = cookie_from_hash(values)
179
+ jar.add(url, cookie)
180
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
181
+ assert_equal(2, jar.cookies(url).length)
182
+
183
+ jar.save_as("cookies.yml")
184
+ jar.clear!
185
+ assert_equal(0, jar.cookies(url).length)
186
+
187
+ jar.load("cookies.yml")
188
+ assert_equal(2, jar.cookies(url).length)
189
+ FileUtils.rm("cookies.yml")
190
+ end
191
+
192
+ def test_expire_cookies
193
+ values = { :name => 'Foo',
194
+ :value => 'Bar',
195
+ :path => '/',
196
+ :expires => Time.now + (10 * 86400),
197
+ :domain => 'rubyforge.org'
198
+ }
199
+ url = URI.parse('http://rubyforge.org/')
200
+
201
+ jar = WWW::Mechanize::CookieJar.new
202
+ assert_equal(0, jar.cookies(url).length)
203
+
204
+ # Add one cookie with an expiration date in the future
205
+ cookie = cookie_from_hash(values)
206
+ jar.add(url, cookie)
207
+ assert_equal(1, jar.cookies(url).length)
208
+
209
+ # Add a second cookie
210
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
211
+ assert_equal(2, jar.cookies(url).length)
212
+
213
+ # Make sure we can get the cookie from different paths
214
+ assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
215
+
216
+ # Expire the first cookie
217
+ jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
218
+ assert_equal(1, jar.cookies(url).length)
219
+
220
+ # Expire the second cookie
221
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
222
+ :expires => Time.now - (10 * 86400))))
223
+ assert_equal(0, jar.cookies(url).length)
224
+ end
225
+
226
+ def test_session_cookies
227
+ values = { :name => 'Foo',
228
+ :value => 'Bar',
229
+ :path => '/',
230
+ :expires => nil,
231
+ :domain => 'rubyforge.org'
232
+ }
233
+ url = URI.parse('http://rubyforge.org/')
234
+
235
+ jar = WWW::Mechanize::CookieJar.new
236
+ assert_equal(0, jar.cookies(url).length)
237
+
238
+ # Add one cookie with an expiration date in the future
239
+ cookie = cookie_from_hash(values)
240
+ jar.add(url, cookie)
241
+ assert_equal(1, jar.cookies(url).length)
242
+
243
+ # Add a second cookie
244
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
245
+ assert_equal(2, jar.cookies(url).length)
246
+
247
+ # Make sure we can get the cookie from different paths
248
+ assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
249
+
250
+ # Expire the first cookie
251
+ jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
252
+ assert_equal(1, jar.cookies(url).length)
253
+
254
+ # Expire the second cookie
255
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
256
+ :expires => Time.now - (10 * 86400))))
257
+ assert_equal(0, jar.cookies(url).length)
258
+
259
+ # When given a URI with a blank path, CookieJar#cookies should return
260
+ # cookies with the path '/':
261
+ url = URI.parse('http://rubyforge.org')
262
+ assert_equal '', url.path
263
+ assert_equal(0, jar.cookies(url).length)
264
+ # Now add a cookie with the path set to '/':
265
+ jar.add(url, cookie_from_hash(values.merge( :name => 'has_root_path',
266
+ :path => '/')))
267
+ assert_equal(1, jar.cookies(url).length)
268
+ end
269
+
270
+ def test_paths
271
+ values = { :name => 'Foo',
272
+ :value => 'Bar',
273
+ :path => '/login',
274
+ :expires => nil,
275
+ :domain => 'rubyforge.org'
276
+ }
277
+ url = URI.parse('http://rubyforge.org/login')
278
+
279
+ jar = WWW::Mechanize::CookieJar.new
280
+ assert_equal(0, jar.cookies(url).length)
281
+
282
+ # Add one cookie with an expiration date in the future
283
+ cookie = cookie_from_hash(values)
284
+ jar.add(url, cookie)
285
+ assert_equal(1, jar.cookies(url).length)
286
+
287
+ # Add a second cookie
288
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
289
+ assert_equal(2, jar.cookies(url).length)
290
+
291
+ # Make sure we don't get the cookie in a different path
292
+ assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/hello')).length)
293
+ assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/')).length)
294
+
295
+ # Expire the first cookie
296
+ jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
297
+ assert_equal(1, jar.cookies(url).length)
298
+
299
+ # Expire the second cookie
300
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
301
+ :expires => Time.now - (10 * 86400))))
302
+ assert_equal(0, jar.cookies(url).length)
303
+ end
304
+
305
+
306
+ def test_save_and_read_cookiestxt
307
+ values = { :name => 'Foo',
308
+ :value => 'Bar',
309
+ :path => '/',
310
+ :expires => Time.now + (10 * 86400),
311
+ :domain => 'rubyforge.org'
312
+ }
313
+ url = URI.parse('http://rubyforge.org/')
314
+
315
+ jar = WWW::Mechanize::CookieJar.new
316
+ assert_equal(0, jar.cookies(url).length)
317
+
318
+ # Add one cookie with an expiration date in the future
319
+ cookie = cookie_from_hash(values)
320
+ jar.add(url, cookie)
321
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
322
+ assert_equal(2, jar.cookies(url).length)
323
+
324
+ jar.save_as("cookies.txt", :cookiestxt)
325
+ jar.clear!
326
+ assert_equal(0, jar.cookies(url).length)
327
+
328
+ jar.load("cookies.txt", :cookiestxt)
329
+ assert_equal(2, jar.cookies(url).length)
330
+
331
+ FileUtils.rm("cookies.txt")
332
+ end
333
+
334
+ def test_ssl_cookies
335
+ # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
336
+ values = { :name => 'Foo',
337
+ :value => 'Bar',
338
+ :path => '/login',
339
+ :expires => nil,
340
+ :domain => 'rubyforge.org'
341
+ }
342
+ values_ssl = { :name => 'Foo',
343
+ :value => 'Bar',
344
+ :path => '/login',
345
+ :expires => nil,
346
+ :domain => 'rubyforge.org:443'
347
+ }
348
+ url = URI.parse('https://rubyforge.org/login')
349
+
350
+ jar = WWW::Mechanize::CookieJar.new
351
+ assert_equal(0, jar.cookies(url).length)
352
+
353
+ cookie = cookie_from_hash(values)
354
+ jar.add(url, cookie)
355
+ assert_equal(1, jar.cookies(url).length, "did not handle SSL cookie")
356
+
357
+ cookie = cookie_from_hash(values_ssl)
358
+ jar.add(url, cookie)
359
+ assert_equal(2, jar.cookies(url).length, "did not handle SSL cookie with :443")
360
+ end
361
+
362
+ end
@@ -0,0 +1,123 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class CookiesMechTest < Test::Unit::TestCase
4
+ def setup
5
+ @agent = WWW::Mechanize.new
6
+ end
7
+
8
+ def test_meta_tag_cookies
9
+ cookies = @agent.cookies.length
10
+ page = @agent.get("http://localhost/meta_cookie.html")
11
+ assert_equal(cookies + 1, @agent.cookies.length)
12
+ end
13
+
14
+ def test_send_cookies
15
+ page = @agent.get("http://localhost/many_cookies")
16
+ page = @agent.get("http://localhost/send_cookies")
17
+ assert_equal(3, page.links.length)
18
+ assert_not_nil(page.links.find { |l| l.text == "name:Aaron" })
19
+ assert_not_nil(page.links.find { |l| l.text == "no_expires:nope" })
20
+ end
21
+
22
+ def test_no_space_cookies
23
+ page = @agent.get("http://localhost/one_cookie_no_space")
24
+ assert_equal(1, @agent.cookies.length)
25
+ foo_cookie = @agent.cookies.find { |k| k.name == 'foo' }
26
+ assert_not_nil(foo_cookie, 'Foo cookie was nil')
27
+ assert_equal('bar', foo_cookie.value)
28
+ assert_equal('/', foo_cookie.path)
29
+ assert_equal(true, Time.now < foo_cookie.expires)
30
+ end
31
+
32
+ def test_many_cookies_as_string
33
+ page = @agent.get("http://localhost/many_cookies_as_string")
34
+ assert_equal(4, @agent.cookies.length)
35
+
36
+ name_cookie = @agent.cookies.find { |k| k.name == "name" }
37
+ assert_not_nil(name_cookie, "Name cookie was nil")
38
+ assert_equal("Aaron", name_cookie.value)
39
+ assert_equal("/", name_cookie.path)
40
+ assert_equal(true, Time.now < name_cookie.expires)
41
+
42
+ expired_cookie = @agent.cookies.find { |k| k.name == "expired" }
43
+ assert_nil(expired_cookie, "Expired cookie was not nil")
44
+
45
+ no_exp_cookie = @agent.cookies.find { |k| k.name == "no_expires" }
46
+ assert_not_nil(no_exp_cookie, "No expires cookie is nil")
47
+ assert_equal("nope", no_exp_cookie.value)
48
+ assert_equal("/", no_exp_cookie.path)
49
+ assert_nil(no_exp_cookie.expires)
50
+
51
+ path_cookie = @agent.cookies.find { |k| k.name == "a_path" }
52
+ assert_not_nil(path_cookie, "Path cookie is nil")
53
+ assert_equal("some_path", path_cookie.value)
54
+ assert_equal(true, Time.now < path_cookie.expires)
55
+
56
+ no_path_cookie = @agent.cookies.find { |k| k.name == "no_path" }
57
+ assert_not_nil(no_path_cookie, "No path cookie is nil")
58
+ assert_equal("no_path", no_path_cookie.value)
59
+ assert_equal("/", no_path_cookie.path)
60
+ assert_equal(true, Time.now < no_path_cookie.expires)
61
+ end
62
+
63
+ def test_many_cookies
64
+ page = @agent.get("http://localhost/many_cookies")
65
+ assert_equal(4, @agent.cookies.length)
66
+
67
+ name_cookie = @agent.cookies.find { |k| k.name == "name" }
68
+ assert_not_nil(name_cookie, "Name cookie was nil")
69
+ assert_equal("Aaron", name_cookie.value)
70
+ assert_equal("/", name_cookie.path)
71
+ assert_equal(true, Time.now < name_cookie.expires)
72
+
73
+ expired_cookie = @agent.cookies.find { |k| k.name == "expired" }
74
+ assert_nil(expired_cookie, "Expired cookie was not nil")
75
+
76
+ no_exp_cookie = @agent.cookies.find { |k| k.name == "no_expires" }
77
+ assert_not_nil(no_exp_cookie, "No expires cookie is nil")
78
+ assert_equal("nope", no_exp_cookie.value)
79
+ assert_equal("/", no_exp_cookie.path)
80
+ assert_nil(no_exp_cookie.expires)
81
+
82
+ path_cookie = @agent.cookies.find { |k| k.name == "a_path" }
83
+ assert_not_nil(path_cookie, "Path cookie is nil")
84
+ assert_equal("some_path", path_cookie.value)
85
+ assert_equal(true, Time.now < path_cookie.expires)
86
+
87
+ no_path_cookie = @agent.cookies.find { |k| k.name == "no_path" }
88
+ assert_not_nil(no_path_cookie, "No path cookie is nil")
89
+ assert_equal("no_path", no_path_cookie.value)
90
+ assert_equal("/", no_path_cookie.path)
91
+ assert_equal(true, Time.now < no_path_cookie.expires)
92
+ end
93
+
94
+ def test_get_cookie
95
+ assert_equal(true,
96
+ @agent.cookie_jar.empty?(
97
+ URI::parse("http://localhost/one_cookie")))
98
+
99
+ assert_equal(0, @agent.cookies.length)
100
+
101
+ page = @agent.get("http://localhost/one_cookie")
102
+ assert_equal(1, @agent.cookies.length)
103
+
104
+ cookie = @agent.cookies.first
105
+ assert_equal("foo", cookie.name)
106
+ assert_equal("bar", cookie.value)
107
+ assert_equal("/", cookie.path)
108
+ assert_equal("localhost", cookie.domain)
109
+
110
+ assert_equal(false,
111
+ @agent.cookie_jar.empty?(
112
+ URI::parse("http://localhost/one_cookie")))
113
+ page = @agent.get("http://localhost/one_cookie")
114
+
115
+ assert_equal(1, @agent.cookies.length)
116
+
117
+ cookie = @agent.cookies.first
118
+ assert_equal("foo", cookie.name)
119
+ assert_equal("bar", cookie.value)
120
+ assert_equal("/", cookie.path)
121
+ assert_equal("localhost", cookie.domain)
122
+ end
123
+ end