kitamomonga-mechanize 0.9.3.20090724215219

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) 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 +176 -0
  7. data/README.rdoc +60 -0
  8. data/Rakefile +33 -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 +666 -0
  15. data/lib/mechanize/chain.rb +34 -0
  16. data/lib/mechanize/chain/auth_headers.rb +78 -0
  17. data/lib/mechanize/chain/body_decoding_handler.rb +46 -0
  18. data/lib/mechanize/chain/connection_resolver.rb +76 -0
  19. data/lib/mechanize/chain/custom_headers.rb +21 -0
  20. data/lib/mechanize/chain/handler.rb +9 -0
  21. data/lib/mechanize/chain/header_resolver.rb +51 -0
  22. data/lib/mechanize/chain/parameter_resolver.rb +22 -0
  23. data/lib/mechanize/chain/post_connect_hook.rb +0 -0
  24. data/lib/mechanize/chain/post_page_hook.rb +18 -0
  25. data/lib/mechanize/chain/pre_connect_hook.rb +20 -0
  26. data/lib/mechanize/chain/request_resolver.rb +30 -0
  27. data/lib/mechanize/chain/response_body_parser.rb +38 -0
  28. data/lib/mechanize/chain/response_header_handler.rb +48 -0
  29. data/lib/mechanize/chain/response_reader.rb +39 -0
  30. data/lib/mechanize/chain/ssl_resolver.rb +40 -0
  31. data/lib/mechanize/chain/uri_resolver.rb +75 -0
  32. data/lib/mechanize/content_type_error.rb +14 -0
  33. data/lib/mechanize/cookie.rb +70 -0
  34. data/lib/mechanize/cookie_jar.rb +188 -0
  35. data/lib/mechanize/file.rb +71 -0
  36. data/lib/mechanize/file_response.rb +60 -0
  37. data/lib/mechanize/file_saver.rb +37 -0
  38. data/lib/mechanize/form.rb +364 -0
  39. data/lib/mechanize/form/button.rb +7 -0
  40. data/lib/mechanize/form/check_box.rb +11 -0
  41. data/lib/mechanize/form/field.rb +26 -0
  42. data/lib/mechanize/form/file_upload.rb +22 -0
  43. data/lib/mechanize/form/image_button.rb +21 -0
  44. data/lib/mechanize/form/multi_select_list.rb +67 -0
  45. data/lib/mechanize/form/option.rb +49 -0
  46. data/lib/mechanize/form/radio_button.rb +36 -0
  47. data/lib/mechanize/form/select_list.rb +43 -0
  48. data/lib/mechanize/headers.rb +11 -0
  49. data/lib/mechanize/history.rb +65 -0
  50. data/lib/mechanize/inspect.rb +88 -0
  51. data/lib/mechanize/monkey_patch.rb +35 -0
  52. data/lib/mechanize/page.rb +279 -0
  53. data/lib/mechanize/page/base.rb +8 -0
  54. data/lib/mechanize/page/encoding.rb +61 -0
  55. data/lib/mechanize/page/frame.rb +20 -0
  56. data/lib/mechanize/page/link.rb +53 -0
  57. data/lib/mechanize/page/meta.rb +50 -0
  58. data/lib/mechanize/pluggable_parsers.rb +101 -0
  59. data/lib/mechanize/redirect_limit_reached_error.rb +16 -0
  60. data/lib/mechanize/redirect_not_get_or_head_error.rb +18 -0
  61. data/lib/mechanize/response_code_error.rb +22 -0
  62. data/lib/mechanize/unsupported_scheme_error.rb +8 -0
  63. data/lib/mechanize/util.rb +73 -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_charset.html +9 -0
  102. data/test/htdocs/tc_bad_links.html +5 -0
  103. data/test/htdocs/tc_base_link.html +8 -0
  104. data/test/htdocs/tc_blank_form.html +11 -0
  105. data/test/htdocs/tc_charset.html +6 -0
  106. data/test/htdocs/tc_checkboxes.html +19 -0
  107. data/test/htdocs/tc_encoded_links.html +5 -0
  108. data/test/htdocs/tc_follow_meta.html +8 -0
  109. data/test/htdocs/tc_form_action.html +48 -0
  110. data/test/htdocs/tc_links.html +19 -0
  111. data/test/htdocs/tc_no_attributes.html +16 -0
  112. data/test/htdocs/tc_pretty_print.html +17 -0
  113. data/test/htdocs/tc_radiobuttons.html +17 -0
  114. data/test/htdocs/tc_referer.html +10 -0
  115. data/test/htdocs/tc_relative_links.html +19 -0
  116. data/test/htdocs/tc_textarea.html +23 -0
  117. data/test/htdocs/test_bad_encoding.html +52 -0
  118. data/test/htdocs/unusual______.html +5 -0
  119. data/test/servlets.rb +365 -0
  120. data/test/ssl_server.rb +48 -0
  121. data/test/test_authenticate.rb +71 -0
  122. data/test/test_bad_links.rb +25 -0
  123. data/test/test_blank_form.rb +16 -0
  124. data/test/test_checkboxes.rb +61 -0
  125. data/test/test_content_type.rb +13 -0
  126. data/test/test_cookie_class.rb +338 -0
  127. data/test/test_cookie_jar.rb +362 -0
  128. data/test/test_cookies.rb +123 -0
  129. data/test/test_encoded_links.rb +20 -0
  130. data/test/test_errors.rb +49 -0
  131. data/test/test_follow_meta.rb +108 -0
  132. data/test/test_form_action.rb +52 -0
  133. data/test/test_form_as_hash.rb +61 -0
  134. data/test/test_form_button.rb +38 -0
  135. data/test/test_form_no_inputname.rb +15 -0
  136. data/test/test_forms.rb +577 -0
  137. data/test/test_frames.rb +25 -0
  138. data/test/test_get_headers.rb +73 -0
  139. data/test/test_gzipping.rb +22 -0
  140. data/test/test_hash_api.rb +45 -0
  141. data/test/test_history.rb +142 -0
  142. data/test/test_history_added.rb +16 -0
  143. data/test/test_html_unscape_forms.rb +39 -0
  144. data/test/test_if_modified_since.rb +20 -0
  145. data/test/test_keep_alive.rb +31 -0
  146. data/test/test_links.rb +127 -0
  147. data/test/test_mech.rb +289 -0
  148. data/test/test_mechanize_file.rb +72 -0
  149. data/test/test_meta.rb +65 -0
  150. data/test/test_multi_select.rb +106 -0
  151. data/test/test_no_attributes.rb +13 -0
  152. data/test/test_option.rb +18 -0
  153. data/test/test_page.rb +127 -0
  154. data/test/test_page_encoding.rb +298 -0
  155. data/test/test_pluggable_parser.rb +145 -0
  156. data/test/test_post_form.rb +34 -0
  157. data/test/test_pretty_print.rb +22 -0
  158. data/test/test_radiobutton.rb +75 -0
  159. data/test/test_redirect_limit_reached.rb +39 -0
  160. data/test/test_redirect_verb_handling.rb +43 -0
  161. data/test/test_referer.rb +39 -0
  162. data/test/test_relative_links.rb +40 -0
  163. data/test/test_request.rb +13 -0
  164. data/test/test_response_code.rb +52 -0
  165. data/test/test_save_file.rb +103 -0
  166. data/test/test_scheme.rb +63 -0
  167. data/test/test_select.rb +106 -0
  168. data/test/test_select_all.rb +15 -0
  169. data/test/test_select_none.rb +15 -0
  170. data/test/test_select_noopts.rb +16 -0
  171. data/test/test_set_fields.rb +44 -0
  172. data/test/test_ssl_server.rb +20 -0
  173. data/test/test_subclass.rb +14 -0
  174. data/test/test_textarea.rb +45 -0
  175. data/test/test_upload.rb +109 -0
  176. data/test/test_verbs.rb +25 -0
  177. metadata +320 -0
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestEncodedLinks < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ @page = @agent.get("http://localhost/tc_encoded_links.html")
7
+ end
8
+
9
+ def test_click_link
10
+ link = @page.links.first
11
+ assert_equal('/form_post?a=b&b=c', link.href)
12
+ page = @agent.click(link)
13
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
14
+ end
15
+
16
+ def test_hpricot_link
17
+ page = @agent.click(@page.search('a').first)
18
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
19
+ end
20
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class MechErrorsTest < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ end
7
+
8
+ def test_bad_form_method
9
+ page = @agent.get("http://localhost/bad_form_test.html")
10
+ assert_raise(RuntimeError) {
11
+ @agent.submit(page.forms.first)
12
+ }
13
+ end
14
+
15
+ def test_non_exist
16
+ begin
17
+ page = @agent.get("http://localhost/bad_form_test.html")
18
+ rescue RuntimeError => ex
19
+ assert_equal("404", ex.inspect)
20
+ end
21
+ end
22
+
23
+ def test_too_many_radio
24
+ page = @agent.get("http://localhost/form_test.html")
25
+ form = page.form_with(:name => 'post_form1')
26
+ form.radiobuttons.each { |r| r.checked = true }
27
+ assert_raise(RuntimeError) {
28
+ @agent.submit(form)
29
+ }
30
+ end
31
+
32
+ def test_unknown_agent
33
+ assert_raise(RuntimeError) {
34
+ @agent.user_agent_alias = "Aaron's Browser"
35
+ }
36
+ end
37
+
38
+ def test_bad_url
39
+ assert_raise(RuntimeError) {
40
+ @agent.get('/foo.html')
41
+ }
42
+ end
43
+
44
+ def test_unsupported_scheme
45
+ assert_raise(Mechanize::UnsupportedSchemeError) {
46
+ @agent.get('ftp://server.com/foo.html')
47
+ }
48
+ end
49
+ end
@@ -0,0 +1,108 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class FollowMetaTest < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ end
7
+
8
+ def test_dont_follow_meta_by_default
9
+ page = @agent.get('http://localhost/tc_follow_meta.html')
10
+ assert_equal('http://localhost/tc_follow_meta.html', page.uri.to_s)
11
+ assert_equal(1, page.meta.length)
12
+ end
13
+
14
+ def test_meta_refresh_does_not_send_referer
15
+ @agent.follow_meta_refresh = true
16
+ requests = []
17
+ @agent.pre_connect_hooks << lambda { |params|
18
+ requests << params[:request]
19
+ }
20
+
21
+ page = @agent.get('http://localhost/tc_follow_meta.html')
22
+ assert_nil requests[1]['referer']
23
+ end
24
+
25
+ def test_follow_meta_if_set
26
+ @agent.follow_meta_refresh = true
27
+
28
+ page = @agent.get('http://localhost/tc_follow_meta.html')
29
+
30
+ assert_equal(2, @agent.history.length)
31
+ assert_equal('http://localhost/tc_follow_meta.html',
32
+ @agent.history[0].uri.to_s)
33
+ assert_equal('http://localhost/index.html', page.uri.to_s)
34
+ assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
35
+ end
36
+
37
+ def test_follow_meta_with_empty_url
38
+ @agent.follow_meta_refresh = true
39
+
40
+ page = @agent.get('http://localhost/refresh_with_empty_url')
41
+
42
+ assert_equal(3, @agent.history.length)
43
+ assert_equal('http://localhost/refresh_with_empty_url',
44
+ @agent.history[0].uri.to_s)
45
+ assert_equal('http://localhost/refresh_with_empty_url',
46
+ @agent.history[1].uri.to_s)
47
+ assert_equal('http://localhost/index.html', page.uri.to_s)
48
+ assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
49
+ end
50
+
51
+ def test_follow_meta_without_url
52
+ @agent.follow_meta_refresh = true
53
+
54
+ page = @agent.get('http://localhost/refresh_without_url')
55
+
56
+ assert_equal(3, @agent.history.length)
57
+ assert_equal('http://localhost/refresh_without_url',
58
+ @agent.history[0].uri.to_s)
59
+ assert_equal('http://localhost/refresh_without_url',
60
+ @agent.history[1].uri.to_s)
61
+ assert_equal('http://localhost/index.html', page.uri.to_s)
62
+ assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
63
+ end
64
+
65
+ def test_always_follow_302
66
+ @agent.follow_meta_refresh = false
67
+ page = @agent.get('http://localhost/response_code?code=302&ct=test/xml')
68
+ assert_equal('http://localhost/index.html', page.uri.to_s)
69
+ assert_equal(2, @agent.history.length)
70
+ end
71
+
72
+ def test_infinite_refresh_throws_exception
73
+ @agent.follow_meta_refresh = true
74
+ assert_raises(Mechanize::RedirectLimitReachedError) {
75
+ begin
76
+ @agent.get('http://localhost/infinite_refresh')
77
+ rescue Mechanize::RedirectLimitReachedError => ex
78
+ raise ex
79
+ end
80
+ }
81
+ end
82
+
83
+ def test_dont_honor_http_refresh_by_default
84
+ page = @agent.get('http://localhost/http_refresh?refresh_time=0')
85
+ assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
86
+ end
87
+
88
+ def test_honor_http_refresh_if_set
89
+ @agent.follow_meta_refresh = true
90
+ page = @agent.get('http://localhost/http_refresh?refresh_time=0')
91
+ assert_equal('http://localhost/index.html', page.uri.to_s)
92
+ assert_equal(2, @agent.history.length)
93
+ end
94
+
95
+ def test_honor_http_refresh_delay_if_set
96
+ @agent.follow_meta_refresh = true
97
+ class << @agent
98
+ attr_accessor :slept
99
+ def sleep *args
100
+ @slept = args
101
+ end
102
+ end
103
+
104
+ page = @agent.get('http://localhost/http_refresh?refresh_time=1')
105
+ assert_equal [1], @agent.slept
106
+ end
107
+
108
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestFormAction < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ @page = @agent.get("http://localhost/tc_form_action.html")
7
+ end
8
+
9
+ def test_post_with_bad_encoding_does_not_raise_exception
10
+ @page = @agent.get("http://localhost/test_bad_encoding.html")
11
+ form = @page.form(:name => 'post_form1') { |f|
12
+ f.first_name = "Aaron"
13
+ }
14
+ form.submit
15
+ end
16
+
17
+ def test_post_encoded_action
18
+ form = @page.form(:name => 'post_form1') { |f|
19
+ f.first_name = "Aaron"
20
+ }
21
+ assert_equal('/form_post?a=b&b=c', form.action)
22
+ page = form.submit
23
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
24
+ end
25
+
26
+ def test_get_encoded_action
27
+ form = @page.form('post_form2') { |f|
28
+ f.first_name = "Aaron"
29
+ }
30
+ assert_equal('/form_post?a=b&b=c', form.action)
31
+ page = form.submit
32
+ assert_equal("http://localhost/form_post?first_name=Aaron", page.uri.to_s)
33
+ end
34
+
35
+ def test_post_nonencoded_action
36
+ form = @page.form('post_form3') { |f|
37
+ f.first_name = "Aaron"
38
+ }
39
+ assert_equal('/form_post?a=b&b=c', form.action)
40
+ page = form.submit
41
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
42
+ end
43
+
44
+ def test_post_pound_sign
45
+ form = @page.form('post_form4') { |f|
46
+ f.first_name = "Aaron"
47
+ }
48
+ assert_equal('/form_post#1', form.action)
49
+ page = form.submit
50
+ assert_equal("http://localhost/form_post#1", page.uri.to_s)
51
+ end
52
+ end
@@ -0,0 +1,61 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestFormHash < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ @page = @agent.get('http://localhost/form_multival.html')
7
+ end
8
+
9
+ def test_form_hash
10
+ form = @page.form_with(:name => 'post_form')
11
+
12
+ assert_not_nil(form)
13
+ field_length = form.fields.length
14
+ assert_nil(form['intarweb'])
15
+ form['intarweb'] = 'Aaron'
16
+
17
+ assert_not_nil(form['intarweb'])
18
+ assert_equal(field_length + 1, form.fields.length)
19
+ end
20
+
21
+ def test_add_field_via_hash
22
+ form = @page.form_with(:name => 'post_form')
23
+
24
+ assert_not_nil(form)
25
+ field_length = form.fields.length
26
+ assert_nil(form['intarweb'])
27
+ form['intarweb'] = 'Aaron'
28
+
29
+ assert_not_nil(form['intarweb'])
30
+ assert_equal(field_length + 1, form.fields.length)
31
+ end
32
+
33
+ def test_fields_as_hash
34
+ form = @page.form_with(:name => 'post_form')
35
+
36
+ assert_not_nil(form)
37
+ assert_equal(2, form.fields_with(:name => 'first').length)
38
+
39
+ form['first'] = 'Aaron'
40
+ assert_equal('Aaron', form['first'])
41
+ assert_equal('Aaron', form.field_with(:name => 'first').value)
42
+ end
43
+
44
+ def test_keys
45
+ @page = @agent.get('http://localhost/empty_form.html')
46
+ form = @page.forms.first
47
+
48
+ assert_not_nil(form)
49
+ assert_equal(false, form.has_field?('name'))
50
+ assert_equal(false, form.has_value?('Aaron'))
51
+ assert_equal(0, form.keys.length)
52
+ assert_equal(0, form.values.length)
53
+ form['name'] = 'Aaron'
54
+ assert_equal(true, form.has_field?('name'))
55
+ assert_equal(true, form.has_value?('Aaron'))
56
+ assert_equal(1, form.keys.length)
57
+ assert_equal(['name'], form.keys)
58
+ assert_equal(1, form.values.length)
59
+ assert_equal(['Aaron'], form.values)
60
+ end
61
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestFormButtons < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ end
7
+
8
+ def test_submit_input_tag
9
+ assert_form_contains_button('<input type="submit" value="submit" />')
10
+ end
11
+
12
+ def test_button_input_tag
13
+ assert_form_contains_button('<input type="button" value="submit" />')
14
+ end
15
+
16
+ def test_submit_button_tag
17
+ assert_form_contains_button('<button type="submit" value="submit"/>')
18
+ end
19
+
20
+ def test_button_button_tag
21
+ assert_form_contains_button('<button type="button" value="submit"/>')
22
+ end
23
+
24
+ def assert_form_contains_button(button)
25
+ page = Mechanize::Page.new(nil, html_response, html(button), 200, @agent)
26
+ assert_equal(1, page.forms.length)
27
+ assert_equal(1, page.forms.first.buttons.length)
28
+ end
29
+
30
+ def html(input)
31
+ "<html><body><form>#{input}</form></body></html>"
32
+ end
33
+
34
+ def html_response
35
+ { 'content-type' => 'text/html' }
36
+ end
37
+ end
38
+
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class FormNoInputNameTest < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ @page = @agent.get('http://localhost/form_no_input_name.html')
7
+ end
8
+
9
+ def test_no_input_name
10
+ form = @page.forms.first
11
+ assert_equal(0, form.fields.length)
12
+ assert_equal(0, form.radiobuttons.length)
13
+ assert_equal(0, form.checkboxes.length)
14
+ end
15
+ end
@@ -0,0 +1,577 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class FormsMechTest < Test::Unit::TestCase
4
+ def setup
5
+ @agent = 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
+
565
+ def test_form_attribute_class
566
+ page = @agent.get('http://localhost:2000/form_no_action.html')
567
+ form = page.forms.first
568
+ assert_equal('formclass', form.attribute_class)
569
+ end
570
+
571
+ def test_form_attribute_id
572
+ page = @agent.get('http://localhost:2000/form_no_action.html')
573
+ form = page.forms.first
574
+ assert_equal('formid', form.attribute_id)
575
+ end
576
+
577
+ end