mechanize-ntlm 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. data/CHANGELOG.rdoc +480 -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 +44 -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-ntlm.rb +7 -0
  15. data/lib/www/mechanize.rb +582 -0
  16. data/lib/www/mechanize/chain.rb +34 -0
  17. data/lib/www/mechanize/chain/auth_headers.rb +82 -0
  18. data/lib/www/mechanize/chain/body_decoding_handler.rb +43 -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 +48 -0
  23. data/lib/www/mechanize/chain/parameter_resolver.rb +23 -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 +51 -0
  29. data/lib/www/mechanize/chain/response_reader.rb +41 -0
  30. data/lib/www/mechanize/chain/ssl_resolver.rb +36 -0
  31. data/lib/www/mechanize/chain/uri_resolver.rb +73 -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 +359 -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 +145 -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 +10 -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/lib/www/ntlm-http/lib/net/ntlm_http.rb +854 -0
  64. data/mechanize.gemspec +24 -0
  65. data/test/chain/test_argument_validator.rb +14 -0
  66. data/test/chain/test_custom_headers.rb +18 -0
  67. data/test/chain/test_parameter_resolver.rb +35 -0
  68. data/test/chain/test_request_resolver.rb +29 -0
  69. data/test/chain/test_response_reader.rb +24 -0
  70. data/test/data/htpasswd +1 -0
  71. data/test/data/server.crt +16 -0
  72. data/test/data/server.csr +12 -0
  73. data/test/data/server.key +15 -0
  74. data/test/data/server.pem +15 -0
  75. data/test/helper.rb +127 -0
  76. data/test/htdocs/alt_text.html +10 -0
  77. data/test/htdocs/bad_form_test.html +9 -0
  78. data/test/htdocs/button.jpg +0 -0
  79. data/test/htdocs/empty_form.html +6 -0
  80. data/test/htdocs/file_upload.html +26 -0
  81. data/test/htdocs/find_link.html +41 -0
  82. data/test/htdocs/form_multi_select.html +16 -0
  83. data/test/htdocs/form_multival.html +37 -0
  84. data/test/htdocs/form_no_action.html +18 -0
  85. data/test/htdocs/form_no_input_name.html +16 -0
  86. data/test/htdocs/form_select.html +16 -0
  87. data/test/htdocs/form_select_all.html +16 -0
  88. data/test/htdocs/form_select_none.html +17 -0
  89. data/test/htdocs/form_select_noopts.html +10 -0
  90. data/test/htdocs/form_set_fields.html +14 -0
  91. data/test/htdocs/form_test.html +188 -0
  92. data/test/htdocs/frame_test.html +30 -0
  93. data/test/htdocs/google.html +13 -0
  94. data/test/htdocs/iframe_test.html +16 -0
  95. data/test/htdocs/index.html +6 -0
  96. data/test/htdocs/link with space.html +5 -0
  97. data/test/htdocs/meta_cookie.html +11 -0
  98. data/test/htdocs/no_title_test.html +6 -0
  99. data/test/htdocs/relative/tc_relative_links.html +21 -0
  100. data/test/htdocs/tc_bad_links.html +5 -0
  101. data/test/htdocs/tc_base_link.html +8 -0
  102. data/test/htdocs/tc_blank_form.html +11 -0
  103. data/test/htdocs/tc_checkboxes.html +19 -0
  104. data/test/htdocs/tc_encoded_links.html +5 -0
  105. data/test/htdocs/tc_follow_meta.html +8 -0
  106. data/test/htdocs/tc_form_action.html +48 -0
  107. data/test/htdocs/tc_links.html +18 -0
  108. data/test/htdocs/tc_no_attributes.html +16 -0
  109. data/test/htdocs/tc_pretty_print.html +17 -0
  110. data/test/htdocs/tc_radiobuttons.html +17 -0
  111. data/test/htdocs/tc_referer.html +10 -0
  112. data/test/htdocs/tc_relative_links.html +19 -0
  113. data/test/htdocs/tc_textarea.html +23 -0
  114. data/test/htdocs/unusual______.html +5 -0
  115. data/test/servlets.rb +339 -0
  116. data/test/ssl_server.rb +48 -0
  117. data/test/test_authenticate.rb +71 -0
  118. data/test/test_bad_links.rb +25 -0
  119. data/test/test_blank_form.rb +16 -0
  120. data/test/test_checkboxes.rb +61 -0
  121. data/test/test_content_type.rb +13 -0
  122. data/test/test_cookie_class.rb +338 -0
  123. data/test/test_cookie_jar.rb +343 -0
  124. data/test/test_cookies.rb +123 -0
  125. data/test/test_encoded_links.rb +20 -0
  126. data/test/test_errors.rb +49 -0
  127. data/test/test_follow_meta.rb +69 -0
  128. data/test/test_form_action.rb +44 -0
  129. data/test/test_form_as_hash.rb +61 -0
  130. data/test/test_form_button.rb +38 -0
  131. data/test/test_form_no_inputname.rb +15 -0
  132. data/test/test_forms.rb +575 -0
  133. data/test/test_frames.rb +25 -0
  134. data/test/test_get_headers.rb +52 -0
  135. data/test/test_gzipping.rb +22 -0
  136. data/test/test_hash_api.rb +45 -0
  137. data/test/test_history.rb +142 -0
  138. data/test/test_history_added.rb +16 -0
  139. data/test/test_html_unscape_forms.rb +39 -0
  140. data/test/test_if_modified_since.rb +20 -0
  141. data/test/test_keep_alive.rb +31 -0
  142. data/test/test_links.rb +120 -0
  143. data/test/test_mech.rb +259 -0
  144. data/test/test_mechanize_file.rb +47 -0
  145. data/test/test_multi_select.rb +106 -0
  146. data/test/test_no_attributes.rb +13 -0
  147. data/test/test_option.rb +18 -0
  148. data/test/test_page.rb +67 -0
  149. data/test/test_pluggable_parser.rb +145 -0
  150. data/test/test_post_form.rb +34 -0
  151. data/test/test_pretty_print.rb +22 -0
  152. data/test/test_radiobutton.rb +75 -0
  153. data/test/test_redirect_limit_reached.rb +41 -0
  154. data/test/test_redirect_verb_handling.rb +45 -0
  155. data/test/test_referer.rb +39 -0
  156. data/test/test_relative_links.rb +40 -0
  157. data/test/test_request.rb +13 -0
  158. data/test/test_response_code.rb +52 -0
  159. data/test/test_save_file.rb +48 -0
  160. data/test/test_scheme.rb +48 -0
  161. data/test/test_select.rb +106 -0
  162. data/test/test_select_all.rb +15 -0
  163. data/test/test_select_none.rb +15 -0
  164. data/test/test_select_noopts.rb +16 -0
  165. data/test/test_set_fields.rb +44 -0
  166. data/test/test_ssl_server.rb +20 -0
  167. data/test/test_subclass.rb +14 -0
  168. data/test/test_textarea.rb +45 -0
  169. data/test/test_upload.rb +109 -0
  170. data/test/test_verbs.rb +25 -0
  171. metadata +284 -0
@@ -0,0 +1,343 @@
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_domain_case
14
+ values = { :name => 'Foo',
15
+ :value => 'Bar',
16
+ :path => '/',
17
+ :expires => Time.now + (10 * 86400),
18
+ :domain => 'rubyforge.org'
19
+ }
20
+ url = URI.parse('http://rubyforge.org/')
21
+
22
+ jar = WWW::Mechanize::CookieJar.new
23
+ assert_equal(0, jar.cookies(url).length)
24
+
25
+ # Add one cookie with an expiration date in the future
26
+ cookie = cookie_from_hash(values)
27
+ jar.add(url, cookie)
28
+ assert_equal(1, jar.cookies(url).length)
29
+
30
+ jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
31
+ :name => 'aaron'
32
+ ) ) )
33
+
34
+ assert_equal(2, jar.cookies(url).length)
35
+
36
+ url2 = URI.parse('http://RuByFoRgE.oRg/')
37
+ assert_equal(2, jar.cookies(url2).length)
38
+ end
39
+
40
+ def test_empty_value
41
+ values = { :name => 'Foo',
42
+ :value => '',
43
+ :path => '/',
44
+ :expires => Time.now + (10 * 86400),
45
+ :domain => 'rubyforge.org'
46
+ }
47
+ url = URI.parse('http://rubyforge.org/')
48
+
49
+ jar = WWW::Mechanize::CookieJar.new
50
+ assert_equal(0, jar.cookies(url).length)
51
+
52
+ # Add one cookie with an expiration date in the future
53
+ cookie = cookie_from_hash(values)
54
+ jar.add(url, cookie)
55
+ assert_equal(1, jar.cookies(url).length)
56
+
57
+ jar.add(url, cookie_from_hash( values.merge( :domain => 'RuByForge.Org',
58
+ :name => 'aaron'
59
+ ) ) )
60
+
61
+ assert_equal(2, jar.cookies(url).length)
62
+
63
+ url2 = URI.parse('http://RuByFoRgE.oRg/')
64
+ assert_equal(2, jar.cookies(url2).length)
65
+ end
66
+
67
+ def test_add_future_cookies
68
+ values = { :name => 'Foo',
69
+ :value => 'Bar',
70
+ :path => '/',
71
+ :expires => Time.now + (10 * 86400),
72
+ :domain => 'rubyforge.org'
73
+ }
74
+ url = URI.parse('http://rubyforge.org/')
75
+
76
+ jar = WWW::Mechanize::CookieJar.new
77
+ assert_equal(0, jar.cookies(url).length)
78
+
79
+ # Add one cookie with an expiration date in the future
80
+ cookie = cookie_from_hash(values)
81
+ jar.add(url, cookie)
82
+ assert_equal(1, jar.cookies(url).length)
83
+
84
+ # Add the same cookie, and we should still only have one
85
+ jar.add(url, cookie_from_hash(values))
86
+ assert_equal(1, jar.cookies(url).length)
87
+
88
+ # Make sure we can get the cookie from different paths
89
+ assert_equal(1, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
90
+
91
+ # Make sure we can't get the cookie from different domains
92
+ assert_equal(0, jar.cookies(URI.parse('http://google.com/')).length)
93
+ end
94
+
95
+ def test_add_multiple_cookies
96
+ values = { :name => 'Foo',
97
+ :value => 'Bar',
98
+ :path => '/',
99
+ :expires => Time.now + (10 * 86400),
100
+ :domain => 'rubyforge.org'
101
+ }
102
+ url = URI.parse('http://rubyforge.org/')
103
+
104
+ jar = WWW::Mechanize::CookieJar.new
105
+ assert_equal(0, jar.cookies(url).length)
106
+
107
+ # Add one cookie with an expiration date in the future
108
+ cookie = cookie_from_hash(values)
109
+ jar.add(url, cookie)
110
+ assert_equal(1, jar.cookies(url).length)
111
+
112
+ # Add the same cookie, and we should still only have one
113
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
114
+ assert_equal(2, jar.cookies(url).length)
115
+
116
+ # Make sure we can get the cookie from different paths
117
+ assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
118
+
119
+ # Make sure we can't get the cookie from different domains
120
+ assert_equal(0, jar.cookies(URI.parse('http://google.com/')).length)
121
+ end
122
+
123
+ def test_clear_cookies
124
+ values = { :name => 'Foo',
125
+ :value => 'Bar',
126
+ :path => '/',
127
+ :expires => Time.now + (10 * 86400),
128
+ :domain => 'rubyforge.org'
129
+ }
130
+ url = URI.parse('http://rubyforge.org/')
131
+
132
+ jar = WWW::Mechanize::CookieJar.new
133
+ assert_equal(0, jar.cookies(url).length)
134
+
135
+ # Add one cookie with an expiration date in the future
136
+ cookie = cookie_from_hash(values)
137
+ jar.add(url, cookie)
138
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
139
+ assert_equal(2, jar.cookies(url).length)
140
+
141
+ jar.clear!
142
+
143
+ assert_equal(0, jar.cookies(url).length)
144
+ end
145
+
146
+ def test_save_cookies
147
+ values = { :name => 'Foo',
148
+ :value => 'Bar',
149
+ :path => '/',
150
+ :expires => Time.now + (10 * 86400),
151
+ :domain => 'rubyforge.org'
152
+ }
153
+ url = URI.parse('http://rubyforge.org/')
154
+
155
+ jar = WWW::Mechanize::CookieJar.new
156
+ assert_equal(0, jar.cookies(url).length)
157
+
158
+ # Add one cookie with an expiration date in the future
159
+ cookie = cookie_from_hash(values)
160
+ jar.add(url, cookie)
161
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
162
+ assert_equal(2, jar.cookies(url).length)
163
+
164
+ jar.save_as("cookies.yml")
165
+ jar.clear!
166
+ assert_equal(0, jar.cookies(url).length)
167
+
168
+ jar.load("cookies.yml")
169
+ assert_equal(2, jar.cookies(url).length)
170
+ FileUtils.rm("cookies.yml")
171
+ end
172
+
173
+ def test_expire_cookies
174
+ values = { :name => 'Foo',
175
+ :value => 'Bar',
176
+ :path => '/',
177
+ :expires => Time.now + (10 * 86400),
178
+ :domain => 'rubyforge.org'
179
+ }
180
+ url = URI.parse('http://rubyforge.org/')
181
+
182
+ jar = WWW::Mechanize::CookieJar.new
183
+ assert_equal(0, jar.cookies(url).length)
184
+
185
+ # Add one cookie with an expiration date in the future
186
+ cookie = cookie_from_hash(values)
187
+ jar.add(url, cookie)
188
+ assert_equal(1, jar.cookies(url).length)
189
+
190
+ # Add a second cookie
191
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
192
+ assert_equal(2, jar.cookies(url).length)
193
+
194
+ # Make sure we can get the cookie from different paths
195
+ assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
196
+
197
+ # Expire the first cookie
198
+ jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
199
+ assert_equal(1, jar.cookies(url).length)
200
+
201
+ # Expire the second cookie
202
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
203
+ :expires => Time.now - (10 * 86400))))
204
+ assert_equal(0, jar.cookies(url).length)
205
+ end
206
+
207
+ def test_session_cookies
208
+ values = { :name => 'Foo',
209
+ :value => 'Bar',
210
+ :path => '/',
211
+ :expires => nil,
212
+ :domain => 'rubyforge.org'
213
+ }
214
+ url = URI.parse('http://rubyforge.org/')
215
+
216
+ jar = WWW::Mechanize::CookieJar.new
217
+ assert_equal(0, jar.cookies(url).length)
218
+
219
+ # Add one cookie with an expiration date in the future
220
+ cookie = cookie_from_hash(values)
221
+ jar.add(url, cookie)
222
+ assert_equal(1, jar.cookies(url).length)
223
+
224
+ # Add a second cookie
225
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
226
+ assert_equal(2, jar.cookies(url).length)
227
+
228
+ # Make sure we can get the cookie from different paths
229
+ assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length)
230
+
231
+ # Expire the first cookie
232
+ jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
233
+ assert_equal(1, jar.cookies(url).length)
234
+
235
+ # Expire the second cookie
236
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
237
+ :expires => Time.now - (10 * 86400))))
238
+ assert_equal(0, jar.cookies(url).length)
239
+
240
+ # When given a URI with a blank path, CookieJar#cookies should return
241
+ # cookies with the path '/':
242
+ url = URI.parse('http://rubyforge.org')
243
+ assert_equal '', url.path
244
+ assert_equal(0, jar.cookies(url).length)
245
+ # Now add a cookie with the path set to '/':
246
+ jar.add(url, cookie_from_hash(values.merge( :name => 'has_root_path',
247
+ :path => '/')))
248
+ assert_equal(1, jar.cookies(url).length)
249
+ end
250
+
251
+ def test_paths
252
+ values = { :name => 'Foo',
253
+ :value => 'Bar',
254
+ :path => '/login',
255
+ :expires => nil,
256
+ :domain => 'rubyforge.org'
257
+ }
258
+ url = URI.parse('http://rubyforge.org/login')
259
+
260
+ jar = WWW::Mechanize::CookieJar.new
261
+ assert_equal(0, jar.cookies(url).length)
262
+
263
+ # Add one cookie with an expiration date in the future
264
+ cookie = cookie_from_hash(values)
265
+ jar.add(url, cookie)
266
+ assert_equal(1, jar.cookies(url).length)
267
+
268
+ # Add a second cookie
269
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
270
+ assert_equal(2, jar.cookies(url).length)
271
+
272
+ # Make sure we don't get the cookie in a different path
273
+ assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/hello')).length)
274
+ assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/')).length)
275
+
276
+ # Expire the first cookie
277
+ jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400))))
278
+ assert_equal(1, jar.cookies(url).length)
279
+
280
+ # Expire the second cookie
281
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz',
282
+ :expires => Time.now - (10 * 86400))))
283
+ assert_equal(0, jar.cookies(url).length)
284
+ end
285
+
286
+
287
+ def test_save_and_read_cookiestxt
288
+ values = { :name => 'Foo',
289
+ :value => 'Bar',
290
+ :path => '/',
291
+ :expires => Time.now + (10 * 86400),
292
+ :domain => 'rubyforge.org'
293
+ }
294
+ url = URI.parse('http://rubyforge.org/')
295
+
296
+ jar = WWW::Mechanize::CookieJar.new
297
+ assert_equal(0, jar.cookies(url).length)
298
+
299
+ # Add one cookie with an expiration date in the future
300
+ cookie = cookie_from_hash(values)
301
+ jar.add(url, cookie)
302
+ jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' )))
303
+ assert_equal(2, jar.cookies(url).length)
304
+
305
+ jar.save_as("cookies.txt", :cookiestxt)
306
+ jar.clear!
307
+ assert_equal(0, jar.cookies(url).length)
308
+
309
+ jar.load("cookies.txt", :cookiestxt)
310
+ assert_equal(2, jar.cookies(url).length)
311
+
312
+ FileUtils.rm("cookies.txt")
313
+ end
314
+
315
+ def test_ssl_cookies
316
+ # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
317
+ values = { :name => 'Foo',
318
+ :value => 'Bar',
319
+ :path => '/login',
320
+ :expires => nil,
321
+ :domain => 'rubyforge.org'
322
+ }
323
+ values_ssl = { :name => 'Foo',
324
+ :value => 'Bar',
325
+ :path => '/login',
326
+ :expires => nil,
327
+ :domain => 'rubyforge.org:443'
328
+ }
329
+ url = URI.parse('https://rubyforge.org/login')
330
+
331
+ jar = WWW::Mechanize::CookieJar.new
332
+ assert_equal(0, jar.cookies(url).length)
333
+
334
+ cookie = cookie_from_hash(values)
335
+ jar.add(url, cookie)
336
+ assert_equal(1, jar.cookies(url).length, "did not handle SSL cookie")
337
+
338
+ cookie = cookie_from_hash(values_ssl)
339
+ jar.add(url, cookie)
340
+ assert_equal(2, jar.cookies(url).length, "did not handle SSL cookie with :443")
341
+ end
342
+
343
+ 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