tenderlove-mechanize 0.9.3.20090617085936

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 +496 -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/chain/auth_headers.rb +80 -0
  16. data/lib/www/mechanize/chain/body_decoding_handler.rb +48 -0
  17. data/lib/www/mechanize/chain/connection_resolver.rb +78 -0
  18. data/lib/www/mechanize/chain/custom_headers.rb +23 -0
  19. data/lib/www/mechanize/chain/handler.rb +9 -0
  20. data/lib/www/mechanize/chain/header_resolver.rb +53 -0
  21. data/lib/www/mechanize/chain/parameter_resolver.rb +24 -0
  22. data/lib/www/mechanize/chain/post_connect_hook.rb +0 -0
  23. data/lib/www/mechanize/chain/pre_connect_hook.rb +22 -0
  24. data/lib/www/mechanize/chain/request_resolver.rb +32 -0
  25. data/lib/www/mechanize/chain/response_body_parser.rb +40 -0
  26. data/lib/www/mechanize/chain/response_header_handler.rb +50 -0
  27. data/lib/www/mechanize/chain/response_reader.rb +41 -0
  28. data/lib/www/mechanize/chain/ssl_resolver.rb +42 -0
  29. data/lib/www/mechanize/chain/uri_resolver.rb +77 -0
  30. data/lib/www/mechanize/chain.rb +34 -0
  31. data/lib/www/mechanize/content_type_error.rb +16 -0
  32. data/lib/www/mechanize/cookie.rb +72 -0
  33. data/lib/www/mechanize/cookie_jar.rb +191 -0
  34. data/lib/www/mechanize/file.rb +73 -0
  35. data/lib/www/mechanize/file_response.rb +62 -0
  36. data/lib/www/mechanize/file_saver.rb +39 -0
  37. data/lib/www/mechanize/form/button.rb +8 -0
  38. data/lib/www/mechanize/form/check_box.rb +13 -0
  39. data/lib/www/mechanize/form/field.rb +28 -0
  40. data/lib/www/mechanize/form/file_upload.rb +24 -0
  41. data/lib/www/mechanize/form/image_button.rb +23 -0
  42. data/lib/www/mechanize/form/multi_select_list.rb +69 -0
  43. data/lib/www/mechanize/form/option.rb +51 -0
  44. data/lib/www/mechanize/form/radio_button.rb +38 -0
  45. data/lib/www/mechanize/form/select_list.rb +45 -0
  46. data/lib/www/mechanize/form.rb +360 -0
  47. data/lib/www/mechanize/headers.rb +12 -0
  48. data/lib/www/mechanize/history.rb +67 -0
  49. data/lib/www/mechanize/inspect.rb +90 -0
  50. data/lib/www/mechanize/monkey_patch.rb +37 -0
  51. data/lib/www/mechanize/page/base.rb +10 -0
  52. data/lib/www/mechanize/page/frame.rb +22 -0
  53. data/lib/www/mechanize/page/link.rb +50 -0
  54. data/lib/www/mechanize/page/meta.rb +51 -0
  55. data/lib/www/mechanize/page.rb +176 -0
  56. data/lib/www/mechanize/pluggable_parsers.rb +103 -0
  57. data/lib/www/mechanize/redirect_limit_reached_error.rb +18 -0
  58. data/lib/www/mechanize/redirect_not_get_or_head_error.rb +20 -0
  59. data/lib/www/mechanize/response_code_error.rb +25 -0
  60. data/lib/www/mechanize/unsupported_scheme_error.rb +10 -0
  61. data/lib/www/mechanize/util.rb +76 -0
  62. data/lib/www/mechanize.rb +619 -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 +44 -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 +119 -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,564 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class FormsMechTest < Test::Unit::TestCase
4
+ def setup
5
+ @agent = WWW::Mechanize.new
6
+ end
7
+
8
+ def test_no_form_action
9
+ page = @agent.get('http://localhost:2000/form_no_action.html')
10
+ page.forms.first.fields.first.value = 'Aaron'
11
+ page = @agent.submit(page.forms.first)
12
+ assert_match('/form_no_action.html?first=Aaron', page.uri.to_s)
13
+ end
14
+
15
+ def test_submit_takes_arbirary_headers
16
+ page = @agent.get('http://localhost:2000/form_no_action.html')
17
+ assert form = page.forms.first
18
+ form.action = '/http_headers'
19
+ page = @agent.submit(form, nil, { 'foo' => 'bar' })
20
+ headers = Hash[*(
21
+ page.body.split("\n").map { |x| x.split('|') }.flatten
22
+ )]
23
+ assert_equal 'bar', headers['foo']
24
+ end
25
+
26
+ # Test submitting form with two fields of the same name
27
+ def test_post_multival
28
+ page = @agent.get("http://localhost/form_multival.html")
29
+ form = page.form_with(:name => 'post_form')
30
+
31
+ assert_not_nil(form)
32
+ assert_equal(2, form.fields_with(:name => 'first').length)
33
+
34
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
35
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
36
+
37
+ page = @agent.submit(form)
38
+
39
+ assert_not_nil(page)
40
+
41
+ assert_equal(2, page.links.length)
42
+ assert_not_nil(page.link_with(:text => 'first:Aaron'))
43
+ assert_not_nil(page.link_with(:text => 'first:Patterson'))
44
+ end
45
+
46
+ # Test calling submit on the form object
47
+ def test_submit_on_form
48
+ page = @agent.get("http://localhost/form_multival.html")
49
+ form = page.form_with(:name => 'post_form')
50
+
51
+ assert_not_nil(form)
52
+ assert_equal(2, form.fields_with(:name => 'first').length)
53
+
54
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
55
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
56
+
57
+ page = form.submit
58
+
59
+ assert_not_nil(page)
60
+
61
+ assert_equal(2, page.links.length)
62
+ assert_not_nil(page.link_with(:text => 'first:Aaron'))
63
+ assert_not_nil(page.link_with(:text => 'first:Patterson'))
64
+ end
65
+
66
+ # Test submitting form with two fields of the same name
67
+ def test_get_multival
68
+ page = @agent.get("http://localhost/form_multival.html")
69
+ form = page.form_with(:name => 'get_form')
70
+
71
+ assert_not_nil(form)
72
+ assert_equal(2, form.fields_with(:name => 'first').length)
73
+
74
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
75
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
76
+
77
+ page = @agent.submit(form)
78
+
79
+ assert_not_nil(page)
80
+
81
+ assert_equal(2, page.links.length)
82
+ assert_not_nil(page.link_with(:text => 'first:Aaron'))
83
+ assert_not_nil(page.link_with(:text => 'first:Patterson'))
84
+ end
85
+
86
+ def test_post_with_non_strings
87
+ page = @agent.get("http://localhost/form_test.html")
88
+ page.form('post_form1') do |form|
89
+ form.first_name = 10
90
+ end.submit
91
+ end
92
+
93
+ def test_post
94
+ page = @agent.get("http://localhost/form_test.html")
95
+ post_form = page.forms.find { |f| f.name == "post_form1" }
96
+ assert_not_nil(post_form, "Post form is null")
97
+ assert_equal("post", post_form.method.downcase)
98
+ assert_equal("/form_post", post_form.action)
99
+
100
+ assert_equal(3, post_form.fields.size)
101
+
102
+ assert_equal(1, post_form.buttons.size)
103
+ assert_equal(2, post_form.radiobuttons.size)
104
+ assert_equal(3, post_form.checkboxes.size)
105
+ assert_not_nil(post_form.fields.find { |f| f.name == "first_name" },
106
+ "First name field was nil"
107
+ )
108
+ assert_not_nil(post_form.fields.find { |f| f.name == "country" },
109
+ "Country field was nil"
110
+ )
111
+ assert_not_nil(
112
+ post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
113
+ "Gender male button was nil"
114
+ )
115
+
116
+ assert_not_nil(
117
+ post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
118
+ "Gender female button was nil"
119
+ )
120
+
121
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "cool person" },
122
+ "couldn't find cool person checkbox")
123
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "likes ham" },
124
+ "couldn't find likes ham checkbox")
125
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "green[eggs]" },
126
+ "couldn't find green[eggs] checkbox")
127
+
128
+ # Find the select list
129
+ s = post_form.fields.find { |f| f.name == "country" }
130
+ assert_equal(2, s.options.length)
131
+ assert_equal("USA", s.value)
132
+ assert_equal("USA", s.options.first.value)
133
+ assert_equal("USA", s.options.first.text)
134
+ assert_equal("CANADA", s.options[1].value)
135
+ assert_equal("CANADA", s.options[1].text)
136
+
137
+ # Now set all the fields
138
+ post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
139
+ post_form.radiobuttons.find { |f|
140
+ f.name == "gender" && f.value == "male"
141
+ }.checked = true
142
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
143
+ post_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
144
+ page = @agent.submit(post_form, post_form.buttons.first)
145
+
146
+ # Check that the submitted fields exist
147
+ assert_equal(5, page.links.size, "Not enough links")
148
+ assert_not_nil(
149
+ page.links.find { |l| l.text == "likes ham:on" },
150
+ "likes ham check box missing"
151
+ )
152
+ assert_not_nil(
153
+ page.links.find { |l| l.text == "green[eggs]:on" },
154
+ "green[eggs] check box missing"
155
+ )
156
+ assert_not_nil(
157
+ page.links.find { |l| l.text == "first_name:Aaron" },
158
+ "first_name field missing"
159
+ )
160
+ assert_not_nil(
161
+ page.links.find { |l| l.text == "gender:male" },
162
+ "gender field missing"
163
+ )
164
+ assert_not_nil(
165
+ page.links.find { |l| l.text == "country:USA" },
166
+ "select box not submitted"
167
+ )
168
+ end
169
+
170
+ def test_post_multipart
171
+ page = @agent.get("http://localhost/form_test.html")
172
+ post_form = page.forms.find { |f| f.name == "post_form4_multipart" }
173
+ assert_not_nil(post_form, "Post form is null")
174
+ assert_equal("post", post_form.method.downcase)
175
+ assert_equal("/form_post", post_form.action)
176
+
177
+ assert_equal(1, post_form.fields.size)
178
+ assert_equal(1, post_form.buttons.size)
179
+
180
+ page = @agent.submit(post_form, post_form.buttons.first)
181
+
182
+ assert_not_nil(page)
183
+ end
184
+
185
+ def test_select_box
186
+ page = @agent.get("http://localhost/form_test.html")
187
+ post_form = page.forms.find { |f| f.name == "post_form1" }
188
+ assert_not_nil(post_form, "Post form is null")
189
+ assert_not_nil(page.header)
190
+ assert_not_nil(page.root)
191
+ assert_equal(0, page.iframes.length)
192
+ assert_equal("post", post_form.method.downcase)
193
+ assert_equal("/form_post", post_form.action)
194
+
195
+ # Find the select list
196
+ s = post_form.field_with(:name => /country/)
197
+ assert_not_nil(s, "Couldn't find country select list")
198
+ assert_equal(2, s.options.length)
199
+ assert_equal("USA", s.value)
200
+ assert_equal("USA", s.options.first.value)
201
+ assert_equal("USA", s.options.first.text)
202
+ assert_equal("CANADA", s.options[1].value)
203
+ assert_equal("CANADA", s.options[1].text)
204
+
205
+ # Now set all the fields
206
+ post_form.field_with(:name => /country/).value = s.options[1]
207
+ assert_equal('CANADA', post_form.country)
208
+ page = @agent.submit(post_form, post_form.buttons.first)
209
+
210
+ # Check that the submitted fields exist
211
+ assert_not_nil(
212
+ page.links.find { |l| l.text == "country:CANADA" },
213
+ "select box not submitted"
214
+ )
215
+ end
216
+
217
+ def test_get
218
+ page = @agent.get("http://localhost/form_test.html")
219
+ get_form = page.forms.find { |f| f.name == "get_form1" }
220
+ assert_not_nil(get_form, "Get form is null")
221
+ assert_equal("get", get_form.method.downcase)
222
+ assert_equal("/form_post", get_form.action)
223
+ assert_equal(1, get_form.fields.size)
224
+ assert_equal(2, get_form.buttons.size)
225
+ assert_equal(2, get_form.radiobuttons.size)
226
+ assert_equal(3, get_form.checkboxes.size)
227
+ assert_not_nil(get_form.fields.find { |f| f.name == "first_name" },
228
+ "First name field was nil"
229
+ )
230
+ assert_not_nil(
231
+ get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
232
+ "Gender male button was nil"
233
+ )
234
+
235
+ assert_not_nil(
236
+ get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
237
+ "Gender female button was nil"
238
+ )
239
+
240
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "cool person" },
241
+ "couldn't find cool person checkbox")
242
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "likes ham" },
243
+ "couldn't find likes ham checkbox")
244
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "green[eggs]" },
245
+ "couldn't find green[eggs] checkbox")
246
+
247
+ # Set up the image button
248
+ img = get_form.buttons.find { |f| f.name == "button" }
249
+ img.x = "9"
250
+ img.y = "10"
251
+ # Now set all the fields
252
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
253
+ get_form.radiobuttons.find { |f|
254
+ f.name == "gender" && f.value == "male"
255
+ }.checked = true
256
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
257
+ get_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
258
+ page = @agent.submit(get_form, get_form.buttons.first)
259
+
260
+ # Check that the submitted fields exist
261
+ assert_equal(7, page.links.size, "Not enough links")
262
+ assert_not_nil(
263
+ page.links.find { |l| l.text == "likes ham:on" },
264
+ "likes ham check box missing"
265
+ )
266
+ assert_not_nil(
267
+ page.links.find { |l| l.text == "green[eggs]:on" },
268
+ "green[eggs] check box missing"
269
+ )
270
+ assert_not_nil(
271
+ page.links.find { |l| l.text == "first_name:Aaron" },
272
+ "first_name field missing"
273
+ )
274
+ assert_not_nil(
275
+ page.links.find { |l| l.text == "gender:male" },
276
+ "gender field missing"
277
+ )
278
+ assert_not_nil(
279
+ page.links.find { |l| l.text == "button.y:10" },
280
+ "Image button missing"
281
+ )
282
+ assert_not_nil(
283
+ page.links.find { |l| l.text == "button.x:9" },
284
+ "Image button missing"
285
+ )
286
+ assert_not_nil(
287
+ page.links.find { |l| l.text == "button:button" },
288
+ "Image button missing"
289
+ )
290
+ end
291
+
292
+ def test_post_with_space_in_action
293
+ page = @agent.get("http://localhost/form_test.html")
294
+ post_form = page.forms.find { |f| f.name == "post_form2" }
295
+ assert_not_nil(post_form, "Post form is null")
296
+ assert_equal("post", post_form.method.downcase)
297
+ assert_equal("/form post", post_form.action)
298
+ assert_equal(1, post_form.fields.size)
299
+ assert_equal(1, post_form.buttons.size)
300
+ assert_equal(2, post_form.radiobuttons.size)
301
+ assert_equal(2, post_form.checkboxes.size)
302
+ assert_not_nil(post_form.fields.find { |f| f.name == "first_name" },
303
+ "First name field was nil"
304
+ )
305
+ assert_not_nil(
306
+ post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
307
+ "Gender male button was nil"
308
+ )
309
+
310
+ assert_not_nil(
311
+ post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
312
+ "Gender female button was nil"
313
+ )
314
+
315
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "cool person" },
316
+ "couldn't find cool person checkbox")
317
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "likes ham" },
318
+ "couldn't find likes ham checkbox")
319
+
320
+ # Now set all the fields
321
+ post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
322
+ post_form.radiobuttons.find { |f|
323
+ f.name == "gender" && f.value == "male"
324
+ }.checked = true
325
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
326
+ page = @agent.submit(post_form, post_form.buttons.first)
327
+
328
+ # Check that the submitted fields exist
329
+ assert_equal(3, page.links.size, "Not enough links")
330
+ assert_not_nil(
331
+ page.links.find { |l| l.text == "likes ham:on" },
332
+ "likes ham check box missing"
333
+ )
334
+ assert_not_nil(
335
+ page.links.find { |l| l.text == "first_name:Aaron" },
336
+ "first_name field missing"
337
+ )
338
+ assert_not_nil(
339
+ page.links.find { |l| l.text == "gender:male" },
340
+ "gender field missing"
341
+ )
342
+ end
343
+
344
+ def test_get_with_space_in_action
345
+ page = @agent.get("http://localhost/form_test.html")
346
+ get_form = page.forms.find { |f| f.name == "get_form2" }
347
+ assert_not_nil(get_form, "Get form is null")
348
+ assert_equal("get", get_form.method.downcase)
349
+ assert_equal("/form post", get_form.action)
350
+ assert_equal(1, get_form.fields.size)
351
+ assert_equal(1, get_form.buttons.size)
352
+ assert_equal(2, get_form.radiobuttons.size)
353
+ assert_equal(2, get_form.checkboxes.size)
354
+ assert_not_nil(get_form.fields.find { |f| f.name == "first_name" },
355
+ "First name field was nil"
356
+ )
357
+ assert_not_nil(
358
+ get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
359
+ "Gender male button was nil"
360
+ )
361
+
362
+ assert_not_nil(
363
+ get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
364
+ "Gender female button was nil"
365
+ )
366
+
367
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "cool person" },
368
+ "couldn't find cool person checkbox")
369
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "likes ham" },
370
+ "couldn't find likes ham checkbox")
371
+
372
+ # Now set all the fields
373
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
374
+ get_form.radiobuttons.find { |f|
375
+ f.name == "gender" && f.value == "male"
376
+ }.checked = true
377
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
378
+ page = @agent.submit(get_form, get_form.buttons.first)
379
+
380
+ # Check that the submitted fields exist
381
+ assert_equal(3, page.links.size, "Not enough links")
382
+ assert_not_nil(
383
+ page.links.find { |l| l.text == "likes ham:on" },
384
+ "likes ham check box missing"
385
+ )
386
+ assert_not_nil(
387
+ page.links.find { |l| l.text == "first_name:Aaron" },
388
+ "first_name field missing"
389
+ )
390
+ assert_not_nil(
391
+ page.links.find { |l| l.text == "gender:male" },
392
+ "gender field missing"
393
+ )
394
+ end
395
+
396
+ def test_post_with_param_in_action
397
+ page = @agent.get("http://localhost/form_test.html")
398
+ post_form = page.forms.find { |f| f.name == "post_form3" }
399
+ assert_not_nil(post_form, "Post form is null")
400
+ assert_equal("post", post_form.method.downcase)
401
+ assert_equal("/form_post?great day=yes&one=two", post_form.action)
402
+ assert_equal(1, post_form.fields.size)
403
+ assert_equal(1, post_form.buttons.size)
404
+ assert_equal(2, post_form.radiobuttons.size)
405
+ assert_equal(2, post_form.checkboxes.size)
406
+ assert_not_nil(post_form.fields.find { |f| f.name == "first_name" },
407
+ "First name field was nil"
408
+ )
409
+ assert_not_nil(
410
+ post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
411
+ "Gender male button was nil"
412
+ )
413
+
414
+ assert_not_nil(
415
+ post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
416
+ "Gender female button was nil"
417
+ )
418
+
419
+ assert_not_nil(post_form.checkbox_with(:name => "cool person"),
420
+ "couldn't find cool person checkbox")
421
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "likes ham" },
422
+ "couldn't find likes ham checkbox")
423
+
424
+ # Now set all the fields
425
+ post_form.field_with(:name => 'first_name').value = "Aaron"
426
+ post_form.radiobuttons.find { |f|
427
+ f.name == "gender" && f.value == "male"
428
+ }.checked = true
429
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
430
+ page = @agent.submit(post_form, post_form.buttons.first)
431
+
432
+ # Check that the submitted fields exist
433
+ assert_equal(3, page.links.size, "Not enough links")
434
+ assert_not_nil(
435
+ page.links.find { |l| l.text == "likes ham:on" },
436
+ "likes ham check box missing"
437
+ )
438
+ assert_not_nil(
439
+ page.links.find { |l| l.text == "first_name:Aaron" },
440
+ "first_name field missing"
441
+ )
442
+ assert_not_nil(
443
+ page.links.find { |l| l.text == "gender:male" },
444
+ "gender field missing"
445
+ )
446
+ end
447
+
448
+ def test_get_with_param_in_action
449
+ page = @agent.get("http://localhost/form_test.html")
450
+ get_form = page.forms.find { |f| f.name == "get_form3" }
451
+ assert_not_nil(get_form, "Get form is null")
452
+ assert_equal("get", get_form.method.downcase)
453
+ assert_equal("/form_post?great day=yes&one=two", get_form.action)
454
+ assert_equal(1, get_form.fields.size)
455
+ assert_equal(1, get_form.buttons.size)
456
+ assert_equal(2, get_form.radiobuttons.size)
457
+ assert_equal(2, get_form.checkboxes.size)
458
+ assert_not_nil(get_form.fields.find { |f| f.name == "first_name" },
459
+ "First name field was nil"
460
+ )
461
+ assert_not_nil(
462
+ get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
463
+ "Gender male button was nil"
464
+ )
465
+
466
+ assert_not_nil(
467
+ get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
468
+ "Gender female button was nil"
469
+ )
470
+
471
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "cool person" },
472
+ "couldn't find cool person checkbox")
473
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "likes ham" },
474
+ "couldn't find likes ham checkbox")
475
+
476
+ # Now set all the fields
477
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
478
+ get_form.radiobuttons.find { |f|
479
+ f.name == "gender" && f.value == "male"
480
+ }.checked = true
481
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
482
+ page = @agent.submit(get_form, get_form.buttons.first)
483
+ # Check that the submitted fields exist
484
+ assert_equal(3, page.links.size, "Not enough links")
485
+ assert_not_nil(
486
+ page.links.find { |l| l.text == "likes ham:on" },
487
+ "likes ham check box missing"
488
+ )
489
+ assert_not_nil(
490
+ page.links.find { |l| l.text == "first_name:Aaron" },
491
+ "first_name field missing"
492
+ )
493
+ assert_not_nil(
494
+ page.links.find { |l| l.text == "gender:male" },
495
+ "gender field missing"
496
+ )
497
+ end
498
+
499
+ def test_field_addition
500
+ page = @agent.get("http://localhost/form_test.html")
501
+ get_form = page.forms.find { |f| f.name == "get_form1" }
502
+ get_form.field("first_name").value = "Gregory"
503
+ assert_equal( "Gregory", get_form.field("first_name").value )
504
+ end
505
+
506
+ def test_fields_as_accessors
507
+ page = @agent.get("http://localhost/form_multival.html")
508
+ form = page.form_with(:name => 'post_form')
509
+
510
+ assert_not_nil(form)
511
+ assert_equal(2, form.fields_with(:name => 'first').length)
512
+
513
+ form.first = 'Aaron'
514
+ assert_equal('Aaron', form.first)
515
+ end
516
+
517
+ def test_add_field
518
+ page = @agent.get("http://localhost/form_multival.html")
519
+ form = page.form_with(:name => 'post_form')
520
+
521
+ assert_not_nil(form)
522
+ number_of_fields = form.fields.length
523
+
524
+ f = form.add_field!('intarweb')
525
+ assert_not_nil(f)
526
+ assert_equal(number_of_fields + 1, form.fields.length)
527
+ end
528
+
529
+ def test_delete_field
530
+ page = @agent.get("http://localhost/form_multival.html")
531
+ form = page.form_with(:name => 'post_form')
532
+
533
+ assert_not_nil(form)
534
+ number_of_fields = form.fields.length
535
+ assert_equal 2, number_of_fields
536
+
537
+ form.delete_field!('first')
538
+ assert_nil(form['first'])
539
+ assert_equal(number_of_fields - 2, form.fields.length)
540
+ end
541
+
542
+ def test_has_field
543
+ page = @agent.get("http://localhost/form_multival.html")
544
+ form = page.form_with(:name => 'post_form')
545
+
546
+ assert_not_nil(form)
547
+ assert_equal(false, form.has_field?('intarweb'))
548
+ f = form.add_field!('intarweb')
549
+ assert_not_nil(f)
550
+ assert_equal(true, form.has_field?('intarweb'))
551
+ end
552
+
553
+ def test_field_error
554
+ @page = @agent.get('http://localhost/empty_form.html')
555
+ form = @page.forms.first
556
+ assert_raise(NoMethodError) {
557
+ form.foo = 'asdfasdf'
558
+ }
559
+
560
+ assert_raise(NoMethodError) {
561
+ form.foo
562
+ }
563
+ end
564
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class FramesMechTest < Test::Unit::TestCase
4
+ def setup
5
+ @agent = WWW::Mechanize.new
6
+ end
7
+
8
+ def test_frames
9
+ page = @agent.get("http://localhost/frame_test.html")
10
+ assert_equal(3, page.frames.size)
11
+ assert_equal("frame1", page.frames[0].name)
12
+ assert_equal("frame2", page.frames[1].name)
13
+ assert_equal("frame3", page.frames[2].name)
14
+ assert_equal("/google.html", page.frames[0].src)
15
+ assert_equal("/form_test.html", page.frames[1].src)
16
+ assert_equal("/file_upload.html", page.frames[2].src)
17
+ end
18
+
19
+ def test_iframes
20
+ page = @agent.get("http://localhost/iframe_test.html")
21
+ assert_equal(1, page.iframes.size)
22
+ assert_equal("frame4", page.iframes.first.name)
23
+ assert_equal("/file_upload.html", page.iframes.first.src)
24
+ end
25
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestGetHeaders < Test::Unit::TestCase
4
+ def setup
5
+ @agent = WWW::Mechanize.new
6
+ end
7
+
8
+ def test_bad_header_symbol
9
+ assert_raises(ArgumentError) do
10
+ @agent.get(:url => "http://localhost/file_upload.html", :headers => { :foobar => "is fubar"})
11
+ end
12
+ end
13
+
14
+ def test_host_header
15
+ page = @agent.get(:url => 'http://localhost/http_headers', :headers => { :etag => '160604-24bc-9fe2c40'})
16
+ assert_header(page, 'host' => 'localhost')
17
+ end
18
+
19
+ def test_etag_header
20
+ page = @agent.get(:url => 'http://localhost/http_headers', :headers => { :etag => '160604-24bc-9fe2c40'})
21
+ assert_header(page, 'etag' => '160604-24bc-9fe2c40')
22
+ end
23
+
24
+ def test_if_modified_since_header
25
+ value = (Time.now - 600).strftime("%a, %d %b %Y %H:%M:%S %z")
26
+ page = @agent.get(:url => 'http://localhost/http_headers', :headers => { :if_modified_since => value})
27
+ assert_header(page, 'if-modified-since' => value)
28
+ end
29
+
30
+ def test_if_modified_since_response
31
+ value = (Time.now - 600).strftime("%a, %d %b %Y %H:%M:%S %z")
32
+ page = @agent.get(:url => 'http://localhost/if_modified_since', :headers => { :if_modified_since => value})
33
+ assert_equal "304", page.code
34
+ assert_equal "0", page.header['content-length']
35
+ end
36
+
37
+ def test_string_header
38
+ page = @agent.get(:url => 'http://localhost/http_headers', :headers => { "X-BS-French-Header" => 'Ou est la bibliotheque?'})
39
+ assert_header(page, 'x-bs-french-header' => 'Ou est la bibliotheque?')
40
+ end
41
+
42
+ def assert_header(page, header)
43
+ headers = {}
44
+ page.body.split(/[\r\n]+/).each do |page_header|
45
+ headers.[]=(*page_header.chomp.split(/\|/))
46
+ end
47
+ header.each do |key, value|
48
+ assert(headers.has_key?(key))
49
+ assert_equal(value, headers[key])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestGzip < Test::Unit::TestCase
4
+ def setup
5
+ @agent = WWW::Mechanize.new
6
+ end
7
+
8
+ def test_request_empty_gzip
9
+ assert_nothing_raised do
10
+ page = @agent.get("http://localhost/gzip")
11
+ end
12
+ end
13
+
14
+ def test_request_gzip
15
+ page = nil
16
+ assert_nothing_raised do
17
+ page = @agent.get("http://localhost/gzip?file=index.html")
18
+ end
19
+ assert_kind_of(WWW::Mechanize::Page, page)
20
+ assert_match('Hello World', page.body)
21
+ end
22
+ end