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,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
@@ -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 = WWW::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 = WWW::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(WWW::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 = WWW::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(WWW::Mechanize::RedirectLimitReachedError) {
75
+ begin
76
+ @agent.get('http://localhost/infinite_refresh')
77
+ rescue WWW::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,44 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestFormAction < Test::Unit::TestCase
4
+ def setup
5
+ @agent = WWW::Mechanize.new
6
+ @page = @agent.get("http://localhost/tc_form_action.html")
7
+ end
8
+
9
+ def test_post_encoded_action
10
+ form = @page.form(:name => 'post_form1') { |f|
11
+ f.first_name = "Aaron"
12
+ }
13
+ assert_equal('/form_post?a=b&b=c', form.action)
14
+ page = form.submit
15
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
16
+ end
17
+
18
+ def test_get_encoded_action
19
+ form = @page.form('post_form2') { |f|
20
+ f.first_name = "Aaron"
21
+ }
22
+ assert_equal('/form_post?a=b&b=c', form.action)
23
+ page = form.submit
24
+ assert_equal("http://localhost/form_post?first_name=Aaron", page.uri.to_s)
25
+ end
26
+
27
+ def test_post_nonencoded_action
28
+ form = @page.form('post_form3') { |f|
29
+ f.first_name = "Aaron"
30
+ }
31
+ assert_equal('/form_post?a=b&b=c', form.action)
32
+ page = form.submit
33
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
34
+ end
35
+
36
+ def test_post_pound_sign
37
+ form = @page.form('post_form4') { |f|
38
+ f.first_name = "Aaron"
39
+ }
40
+ assert_equal('/form_post#1', form.action)
41
+ page = form.submit
42
+ assert_equal("http://localhost/form_post#1", page.uri.to_s)
43
+ end
44
+ 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 = WWW::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 = WWW::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 = WWW::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 = WWW::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