mechanize-ntlm 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. data/CHANGELOG.rdoc +480 -0
  2. data/EXAMPLES.rdoc +171 -0
  3. data/FAQ.rdoc +11 -0
  4. data/GUIDE.rdoc +122 -0
  5. data/LICENSE.rdoc +340 -0
  6. data/Manifest.txt +169 -0
  7. data/README.rdoc +60 -0
  8. data/Rakefile +44 -0
  9. data/examples/flickr_upload.rb +23 -0
  10. data/examples/mech-dump.rb +7 -0
  11. data/examples/proxy_req.rb +9 -0
  12. data/examples/rubyforge.rb +21 -0
  13. data/examples/spider.rb +11 -0
  14. data/lib/mechanize-ntlm.rb +7 -0
  15. data/lib/www/mechanize.rb +582 -0
  16. data/lib/www/mechanize/chain.rb +34 -0
  17. data/lib/www/mechanize/chain/auth_headers.rb +82 -0
  18. data/lib/www/mechanize/chain/body_decoding_handler.rb +43 -0
  19. data/lib/www/mechanize/chain/connection_resolver.rb +78 -0
  20. data/lib/www/mechanize/chain/custom_headers.rb +23 -0
  21. data/lib/www/mechanize/chain/handler.rb +9 -0
  22. data/lib/www/mechanize/chain/header_resolver.rb +48 -0
  23. data/lib/www/mechanize/chain/parameter_resolver.rb +23 -0
  24. data/lib/www/mechanize/chain/post_connect_hook.rb +0 -0
  25. data/lib/www/mechanize/chain/pre_connect_hook.rb +22 -0
  26. data/lib/www/mechanize/chain/request_resolver.rb +32 -0
  27. data/lib/www/mechanize/chain/response_body_parser.rb +40 -0
  28. data/lib/www/mechanize/chain/response_header_handler.rb +51 -0
  29. data/lib/www/mechanize/chain/response_reader.rb +41 -0
  30. data/lib/www/mechanize/chain/ssl_resolver.rb +36 -0
  31. data/lib/www/mechanize/chain/uri_resolver.rb +73 -0
  32. data/lib/www/mechanize/content_type_error.rb +16 -0
  33. data/lib/www/mechanize/cookie.rb +72 -0
  34. data/lib/www/mechanize/cookie_jar.rb +191 -0
  35. data/lib/www/mechanize/file.rb +73 -0
  36. data/lib/www/mechanize/file_response.rb +62 -0
  37. data/lib/www/mechanize/file_saver.rb +39 -0
  38. data/lib/www/mechanize/form.rb +359 -0
  39. data/lib/www/mechanize/form/button.rb +8 -0
  40. data/lib/www/mechanize/form/check_box.rb +13 -0
  41. data/lib/www/mechanize/form/field.rb +28 -0
  42. data/lib/www/mechanize/form/file_upload.rb +24 -0
  43. data/lib/www/mechanize/form/image_button.rb +23 -0
  44. data/lib/www/mechanize/form/multi_select_list.rb +69 -0
  45. data/lib/www/mechanize/form/option.rb +51 -0
  46. data/lib/www/mechanize/form/radio_button.rb +38 -0
  47. data/lib/www/mechanize/form/select_list.rb +45 -0
  48. data/lib/www/mechanize/headers.rb +12 -0
  49. data/lib/www/mechanize/history.rb +67 -0
  50. data/lib/www/mechanize/inspect.rb +90 -0
  51. data/lib/www/mechanize/monkey_patch.rb +37 -0
  52. data/lib/www/mechanize/page.rb +145 -0
  53. data/lib/www/mechanize/page/base.rb +10 -0
  54. data/lib/www/mechanize/page/frame.rb +22 -0
  55. data/lib/www/mechanize/page/link.rb +50 -0
  56. data/lib/www/mechanize/page/meta.rb +10 -0
  57. data/lib/www/mechanize/pluggable_parsers.rb +103 -0
  58. data/lib/www/mechanize/redirect_limit_reached_error.rb +18 -0
  59. data/lib/www/mechanize/redirect_not_get_or_head_error.rb +20 -0
  60. data/lib/www/mechanize/response_code_error.rb +25 -0
  61. data/lib/www/mechanize/unsupported_scheme_error.rb +10 -0
  62. data/lib/www/mechanize/util.rb +76 -0
  63. data/lib/www/ntlm-http/lib/net/ntlm_http.rb +854 -0
  64. data/mechanize.gemspec +24 -0
  65. data/test/chain/test_argument_validator.rb +14 -0
  66. data/test/chain/test_custom_headers.rb +18 -0
  67. data/test/chain/test_parameter_resolver.rb +35 -0
  68. data/test/chain/test_request_resolver.rb +29 -0
  69. data/test/chain/test_response_reader.rb +24 -0
  70. data/test/data/htpasswd +1 -0
  71. data/test/data/server.crt +16 -0
  72. data/test/data/server.csr +12 -0
  73. data/test/data/server.key +15 -0
  74. data/test/data/server.pem +15 -0
  75. data/test/helper.rb +127 -0
  76. data/test/htdocs/alt_text.html +10 -0
  77. data/test/htdocs/bad_form_test.html +9 -0
  78. data/test/htdocs/button.jpg +0 -0
  79. data/test/htdocs/empty_form.html +6 -0
  80. data/test/htdocs/file_upload.html +26 -0
  81. data/test/htdocs/find_link.html +41 -0
  82. data/test/htdocs/form_multi_select.html +16 -0
  83. data/test/htdocs/form_multival.html +37 -0
  84. data/test/htdocs/form_no_action.html +18 -0
  85. data/test/htdocs/form_no_input_name.html +16 -0
  86. data/test/htdocs/form_select.html +16 -0
  87. data/test/htdocs/form_select_all.html +16 -0
  88. data/test/htdocs/form_select_none.html +17 -0
  89. data/test/htdocs/form_select_noopts.html +10 -0
  90. data/test/htdocs/form_set_fields.html +14 -0
  91. data/test/htdocs/form_test.html +188 -0
  92. data/test/htdocs/frame_test.html +30 -0
  93. data/test/htdocs/google.html +13 -0
  94. data/test/htdocs/iframe_test.html +16 -0
  95. data/test/htdocs/index.html +6 -0
  96. data/test/htdocs/link with space.html +5 -0
  97. data/test/htdocs/meta_cookie.html +11 -0
  98. data/test/htdocs/no_title_test.html +6 -0
  99. data/test/htdocs/relative/tc_relative_links.html +21 -0
  100. data/test/htdocs/tc_bad_links.html +5 -0
  101. data/test/htdocs/tc_base_link.html +8 -0
  102. data/test/htdocs/tc_blank_form.html +11 -0
  103. data/test/htdocs/tc_checkboxes.html +19 -0
  104. data/test/htdocs/tc_encoded_links.html +5 -0
  105. data/test/htdocs/tc_follow_meta.html +8 -0
  106. data/test/htdocs/tc_form_action.html +48 -0
  107. data/test/htdocs/tc_links.html +18 -0
  108. data/test/htdocs/tc_no_attributes.html +16 -0
  109. data/test/htdocs/tc_pretty_print.html +17 -0
  110. data/test/htdocs/tc_radiobuttons.html +17 -0
  111. data/test/htdocs/tc_referer.html +10 -0
  112. data/test/htdocs/tc_relative_links.html +19 -0
  113. data/test/htdocs/tc_textarea.html +23 -0
  114. data/test/htdocs/unusual______.html +5 -0
  115. data/test/servlets.rb +339 -0
  116. data/test/ssl_server.rb +48 -0
  117. data/test/test_authenticate.rb +71 -0
  118. data/test/test_bad_links.rb +25 -0
  119. data/test/test_blank_form.rb +16 -0
  120. data/test/test_checkboxes.rb +61 -0
  121. data/test/test_content_type.rb +13 -0
  122. data/test/test_cookie_class.rb +338 -0
  123. data/test/test_cookie_jar.rb +343 -0
  124. data/test/test_cookies.rb +123 -0
  125. data/test/test_encoded_links.rb +20 -0
  126. data/test/test_errors.rb +49 -0
  127. data/test/test_follow_meta.rb +69 -0
  128. data/test/test_form_action.rb +44 -0
  129. data/test/test_form_as_hash.rb +61 -0
  130. data/test/test_form_button.rb +38 -0
  131. data/test/test_form_no_inputname.rb +15 -0
  132. data/test/test_forms.rb +575 -0
  133. data/test/test_frames.rb +25 -0
  134. data/test/test_get_headers.rb +52 -0
  135. data/test/test_gzipping.rb +22 -0
  136. data/test/test_hash_api.rb +45 -0
  137. data/test/test_history.rb +142 -0
  138. data/test/test_history_added.rb +16 -0
  139. data/test/test_html_unscape_forms.rb +39 -0
  140. data/test/test_if_modified_since.rb +20 -0
  141. data/test/test_keep_alive.rb +31 -0
  142. data/test/test_links.rb +120 -0
  143. data/test/test_mech.rb +259 -0
  144. data/test/test_mechanize_file.rb +47 -0
  145. data/test/test_multi_select.rb +106 -0
  146. data/test/test_no_attributes.rb +13 -0
  147. data/test/test_option.rb +18 -0
  148. data/test/test_page.rb +67 -0
  149. data/test/test_pluggable_parser.rb +145 -0
  150. data/test/test_post_form.rb +34 -0
  151. data/test/test_pretty_print.rb +22 -0
  152. data/test/test_radiobutton.rb +75 -0
  153. data/test/test_redirect_limit_reached.rb +41 -0
  154. data/test/test_redirect_verb_handling.rb +45 -0
  155. data/test/test_referer.rb +39 -0
  156. data/test/test_relative_links.rb +40 -0
  157. data/test/test_request.rb +13 -0
  158. data/test/test_response_code.rb +52 -0
  159. data/test/test_save_file.rb +48 -0
  160. data/test/test_scheme.rb +48 -0
  161. data/test/test_select.rb +106 -0
  162. data/test/test_select_all.rb +15 -0
  163. data/test/test_select_none.rb +15 -0
  164. data/test/test_select_noopts.rb +16 -0
  165. data/test/test_set_fields.rb +44 -0
  166. data/test/test_ssl_server.rb +20 -0
  167. data/test/test_subclass.rb +14 -0
  168. data/test/test_textarea.rb +45 -0
  169. data/test/test_upload.rb +109 -0
  170. data/test/test_verbs.rb +25 -0
  171. metadata +284 -0
@@ -0,0 +1,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,69 @@
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_follow_meta_if_set
15
+ @agent.follow_meta_refresh = true
16
+
17
+ page = @agent.get('http://localhost/tc_follow_meta.html')
18
+
19
+ assert_equal(2, @agent.history.length)
20
+ assert_equal('http://localhost/tc_follow_meta.html',
21
+ @agent.history[0].uri.to_s)
22
+ assert_equal('http://localhost/index.html', page.uri.to_s)
23
+ assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
24
+ end
25
+
26
+ def test_always_follow_302
27
+ @agent.follow_meta_refresh = false
28
+ page = @agent.get('http://localhost/response_code?code=302&ct=test/xml')
29
+ assert_equal('http://localhost/index.html', page.uri.to_s)
30
+ assert_equal(2, @agent.history.length)
31
+ end
32
+
33
+ def test_infinite_refresh_throws_exception
34
+ @agent.follow_meta_refresh = true
35
+ assert_raises(WWW::Mechanize::RedirectLimitReachedError) {
36
+ begin
37
+ @agent.get('http://localhost/infinite_refresh')
38
+ rescue WWW::Mechanize::RedirectLimitReachedError => ex
39
+ raise ex
40
+ end
41
+ }
42
+ end
43
+
44
+ def test_dont_honor_http_refresh_by_default
45
+ page = @agent.get('http://localhost/http_refresh?refresh_time=0')
46
+ assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
47
+ end
48
+
49
+ def test_honor_http_refresh_if_set
50
+ @agent.follow_meta_refresh = true
51
+ page = @agent.get('http://localhost/http_refresh?refresh_time=0')
52
+ assert_equal('http://localhost/index.html', page.uri.to_s)
53
+ assert_equal(2, @agent.history.length)
54
+ end
55
+
56
+ def test_honor_http_refresh_delay_if_set
57
+ @agent.follow_meta_refresh = true
58
+ class << @agent
59
+ attr_accessor :slept
60
+ def sleep *args
61
+ @slept = args
62
+ end
63
+ end
64
+
65
+ page = @agent.get('http://localhost/http_refresh?refresh_time=1')
66
+ assert_equal [1], @agent.slept
67
+ end
68
+
69
+ 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
@@ -0,0 +1,575 @@
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
+ def test_submit_takes_arbirary_headers
27
+ page = @agent.get('http://localhost:2000/form_no_action.html')
28
+ assert form = page.forms.first
29
+ form.action = '/http_headers'
30
+ page = form.submit(nil, { 'foo' => 'bar' })
31
+ headers = Hash[*(
32
+ page.body.split("\n").map { |x| x.split('|') }.flatten
33
+ )]
34
+ assert_equal 'bar', headers['foo']
35
+ end
36
+
37
+ # Test submitting form with two fields of the same name
38
+ def test_post_multival
39
+ page = @agent.get("http://localhost/form_multival.html")
40
+ form = page.form_with(:name => 'post_form')
41
+
42
+ assert_not_nil(form)
43
+ assert_equal(2, form.fields_with(:name => 'first').length)
44
+
45
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
46
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
47
+
48
+ page = @agent.submit(form)
49
+
50
+ assert_not_nil(page)
51
+
52
+ assert_equal(2, page.links.length)
53
+ assert_not_nil(page.link_with(:text => 'first:Aaron'))
54
+ assert_not_nil(page.link_with(:text => 'first:Patterson'))
55
+ end
56
+
57
+ # Test calling submit on the form object
58
+ def test_submit_on_form
59
+ page = @agent.get("http://localhost/form_multival.html")
60
+ form = page.form_with(:name => 'post_form')
61
+
62
+ assert_not_nil(form)
63
+ assert_equal(2, form.fields_with(:name => 'first').length)
64
+
65
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
66
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
67
+
68
+ page = form.submit
69
+
70
+ assert_not_nil(page)
71
+
72
+ assert_equal(2, page.links.length)
73
+ assert_not_nil(page.link_with(:text => 'first:Aaron'))
74
+ assert_not_nil(page.link_with(:text => 'first:Patterson'))
75
+ end
76
+
77
+ # Test submitting form with two fields of the same name
78
+ def test_get_multival
79
+ page = @agent.get("http://localhost/form_multival.html")
80
+ form = page.form_with(:name => 'get_form')
81
+
82
+ assert_not_nil(form)
83
+ assert_equal(2, form.fields_with(:name => 'first').length)
84
+
85
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
86
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
87
+
88
+ page = @agent.submit(form)
89
+
90
+ assert_not_nil(page)
91
+
92
+ assert_equal(2, page.links.length)
93
+ assert_not_nil(page.link_with(:text => 'first:Aaron'))
94
+ assert_not_nil(page.link_with(:text => 'first:Patterson'))
95
+ end
96
+
97
+ def test_post_with_non_strings
98
+ page = @agent.get("http://localhost/form_test.html")
99
+ page.form('post_form1') do |form|
100
+ form.first_name = 10
101
+ end.submit
102
+ end
103
+
104
+ def test_post
105
+ page = @agent.get("http://localhost/form_test.html")
106
+ post_form = page.forms.find { |f| f.name == "post_form1" }
107
+ assert_not_nil(post_form, "Post form is null")
108
+ assert_equal("post", post_form.method.downcase)
109
+ assert_equal("/form_post", post_form.action)
110
+
111
+ assert_equal(3, post_form.fields.size)
112
+
113
+ assert_equal(1, post_form.buttons.size)
114
+ assert_equal(2, post_form.radiobuttons.size)
115
+ assert_equal(3, post_form.checkboxes.size)
116
+ assert_not_nil(post_form.fields.find { |f| f.name == "first_name" },
117
+ "First name field was nil"
118
+ )
119
+ assert_not_nil(post_form.fields.find { |f| f.name == "country" },
120
+ "Country field was nil"
121
+ )
122
+ assert_not_nil(
123
+ post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
124
+ "Gender male button was nil"
125
+ )
126
+
127
+ assert_not_nil(
128
+ post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
129
+ "Gender female button was nil"
130
+ )
131
+
132
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "cool person" },
133
+ "couldn't find cool person checkbox")
134
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "likes ham" },
135
+ "couldn't find likes ham checkbox")
136
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "green[eggs]" },
137
+ "couldn't find green[eggs] checkbox")
138
+
139
+ # Find the select list
140
+ s = post_form.fields.find { |f| f.name == "country" }
141
+ assert_equal(2, s.options.length)
142
+ assert_equal("USA", s.value)
143
+ assert_equal("USA", s.options.first.value)
144
+ assert_equal("USA", s.options.first.text)
145
+ assert_equal("CANADA", s.options[1].value)
146
+ assert_equal("CANADA", s.options[1].text)
147
+
148
+ # Now set all the fields
149
+ post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
150
+ post_form.radiobuttons.find { |f|
151
+ f.name == "gender" && f.value == "male"
152
+ }.checked = true
153
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
154
+ post_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
155
+ page = @agent.submit(post_form, post_form.buttons.first)
156
+
157
+ # Check that the submitted fields exist
158
+ assert_equal(5, page.links.size, "Not enough links")
159
+ assert_not_nil(
160
+ page.links.find { |l| l.text == "likes ham:on" },
161
+ "likes ham check box missing"
162
+ )
163
+ assert_not_nil(
164
+ page.links.find { |l| l.text == "green[eggs]:on" },
165
+ "green[eggs] check box missing"
166
+ )
167
+ assert_not_nil(
168
+ page.links.find { |l| l.text == "first_name:Aaron" },
169
+ "first_name field missing"
170
+ )
171
+ assert_not_nil(
172
+ page.links.find { |l| l.text == "gender:male" },
173
+ "gender field missing"
174
+ )
175
+ assert_not_nil(
176
+ page.links.find { |l| l.text == "country:USA" },
177
+ "select box not submitted"
178
+ )
179
+ end
180
+
181
+ def test_post_multipart
182
+ page = @agent.get("http://localhost/form_test.html")
183
+ post_form = page.forms.find { |f| f.name == "post_form4_multipart" }
184
+ assert_not_nil(post_form, "Post form is null")
185
+ assert_equal("post", post_form.method.downcase)
186
+ assert_equal("/form_post", post_form.action)
187
+
188
+ assert_equal(1, post_form.fields.size)
189
+ assert_equal(1, post_form.buttons.size)
190
+
191
+ page = @agent.submit(post_form, post_form.buttons.first)
192
+
193
+ assert_not_nil(page)
194
+ end
195
+
196
+ def test_select_box
197
+ page = @agent.get("http://localhost/form_test.html")
198
+ post_form = page.forms.find { |f| f.name == "post_form1" }
199
+ assert_not_nil(post_form, "Post form is null")
200
+ assert_not_nil(page.header)
201
+ assert_not_nil(page.root)
202
+ assert_equal(0, page.iframes.length)
203
+ assert_equal("post", post_form.method.downcase)
204
+ assert_equal("/form_post", post_form.action)
205
+
206
+ # Find the select list
207
+ s = post_form.field_with(:name => /country/)
208
+ assert_not_nil(s, "Couldn't find country select list")
209
+ assert_equal(2, s.options.length)
210
+ assert_equal("USA", s.value)
211
+ assert_equal("USA", s.options.first.value)
212
+ assert_equal("USA", s.options.first.text)
213
+ assert_equal("CANADA", s.options[1].value)
214
+ assert_equal("CANADA", s.options[1].text)
215
+
216
+ # Now set all the fields
217
+ post_form.field_with(:name => /country/).value = s.options[1]
218
+ assert_equal('CANADA', post_form.country)
219
+ page = @agent.submit(post_form, post_form.buttons.first)
220
+
221
+ # Check that the submitted fields exist
222
+ assert_not_nil(
223
+ page.links.find { |l| l.text == "country:CANADA" },
224
+ "select box not submitted"
225
+ )
226
+ end
227
+
228
+ def test_get
229
+ page = @agent.get("http://localhost/form_test.html")
230
+ get_form = page.forms.find { |f| f.name == "get_form1" }
231
+ assert_not_nil(get_form, "Get form is null")
232
+ assert_equal("get", get_form.method.downcase)
233
+ assert_equal("/form_post", get_form.action)
234
+ assert_equal(1, get_form.fields.size)
235
+ assert_equal(2, get_form.buttons.size)
236
+ assert_equal(2, get_form.radiobuttons.size)
237
+ assert_equal(3, get_form.checkboxes.size)
238
+ assert_not_nil(get_form.fields.find { |f| f.name == "first_name" },
239
+ "First name field was nil"
240
+ )
241
+ assert_not_nil(
242
+ get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
243
+ "Gender male button was nil"
244
+ )
245
+
246
+ assert_not_nil(
247
+ get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
248
+ "Gender female button was nil"
249
+ )
250
+
251
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "cool person" },
252
+ "couldn't find cool person checkbox")
253
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "likes ham" },
254
+ "couldn't find likes ham checkbox")
255
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "green[eggs]" },
256
+ "couldn't find green[eggs] checkbox")
257
+
258
+ # Set up the image button
259
+ img = get_form.buttons.find { |f| f.name == "button" }
260
+ img.x = "9"
261
+ img.y = "10"
262
+ # Now set all the fields
263
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
264
+ get_form.radiobuttons.find { |f|
265
+ f.name == "gender" && f.value == "male"
266
+ }.checked = true
267
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
268
+ get_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
269
+ page = @agent.submit(get_form, get_form.buttons.first)
270
+
271
+ # Check that the submitted fields exist
272
+ assert_equal(7, page.links.size, "Not enough links")
273
+ assert_not_nil(
274
+ page.links.find { |l| l.text == "likes ham:on" },
275
+ "likes ham check box missing"
276
+ )
277
+ assert_not_nil(
278
+ page.links.find { |l| l.text == "green[eggs]:on" },
279
+ "green[eggs] check box missing"
280
+ )
281
+ assert_not_nil(
282
+ page.links.find { |l| l.text == "first_name:Aaron" },
283
+ "first_name field missing"
284
+ )
285
+ assert_not_nil(
286
+ page.links.find { |l| l.text == "gender:male" },
287
+ "gender field missing"
288
+ )
289
+ assert_not_nil(
290
+ page.links.find { |l| l.text == "button.y:10" },
291
+ "Image button missing"
292
+ )
293
+ assert_not_nil(
294
+ page.links.find { |l| l.text == "button.x:9" },
295
+ "Image button missing"
296
+ )
297
+ assert_not_nil(
298
+ page.links.find { |l| l.text == "button:button" },
299
+ "Image button missing"
300
+ )
301
+ end
302
+
303
+ def test_post_with_space_in_action
304
+ page = @agent.get("http://localhost/form_test.html")
305
+ post_form = page.forms.find { |f| f.name == "post_form2" }
306
+ assert_not_nil(post_form, "Post form is null")
307
+ assert_equal("post", post_form.method.downcase)
308
+ assert_equal("/form post", post_form.action)
309
+ assert_equal(1, post_form.fields.size)
310
+ assert_equal(1, post_form.buttons.size)
311
+ assert_equal(2, post_form.radiobuttons.size)
312
+ assert_equal(2, post_form.checkboxes.size)
313
+ assert_not_nil(post_form.fields.find { |f| f.name == "first_name" },
314
+ "First name field was nil"
315
+ )
316
+ assert_not_nil(
317
+ post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
318
+ "Gender male button was nil"
319
+ )
320
+
321
+ assert_not_nil(
322
+ post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
323
+ "Gender female button was nil"
324
+ )
325
+
326
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "cool person" },
327
+ "couldn't find cool person checkbox")
328
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "likes ham" },
329
+ "couldn't find likes ham checkbox")
330
+
331
+ # Now set all the fields
332
+ post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
333
+ post_form.radiobuttons.find { |f|
334
+ f.name == "gender" && f.value == "male"
335
+ }.checked = true
336
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
337
+ page = @agent.submit(post_form, post_form.buttons.first)
338
+
339
+ # Check that the submitted fields exist
340
+ assert_equal(3, page.links.size, "Not enough links")
341
+ assert_not_nil(
342
+ page.links.find { |l| l.text == "likes ham:on" },
343
+ "likes ham check box missing"
344
+ )
345
+ assert_not_nil(
346
+ page.links.find { |l| l.text == "first_name:Aaron" },
347
+ "first_name field missing"
348
+ )
349
+ assert_not_nil(
350
+ page.links.find { |l| l.text == "gender:male" },
351
+ "gender field missing"
352
+ )
353
+ end
354
+
355
+ def test_get_with_space_in_action
356
+ page = @agent.get("http://localhost/form_test.html")
357
+ get_form = page.forms.find { |f| f.name == "get_form2" }
358
+ assert_not_nil(get_form, "Get form is null")
359
+ assert_equal("get", get_form.method.downcase)
360
+ assert_equal("/form post", get_form.action)
361
+ assert_equal(1, get_form.fields.size)
362
+ assert_equal(1, get_form.buttons.size)
363
+ assert_equal(2, get_form.radiobuttons.size)
364
+ assert_equal(2, get_form.checkboxes.size)
365
+ assert_not_nil(get_form.fields.find { |f| f.name == "first_name" },
366
+ "First name field was nil"
367
+ )
368
+ assert_not_nil(
369
+ get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
370
+ "Gender male button was nil"
371
+ )
372
+
373
+ assert_not_nil(
374
+ get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
375
+ "Gender female button was nil"
376
+ )
377
+
378
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "cool person" },
379
+ "couldn't find cool person checkbox")
380
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "likes ham" },
381
+ "couldn't find likes ham checkbox")
382
+
383
+ # Now set all the fields
384
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
385
+ get_form.radiobuttons.find { |f|
386
+ f.name == "gender" && f.value == "male"
387
+ }.checked = true
388
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
389
+ page = @agent.submit(get_form, get_form.buttons.first)
390
+
391
+ # Check that the submitted fields exist
392
+ assert_equal(3, page.links.size, "Not enough links")
393
+ assert_not_nil(
394
+ page.links.find { |l| l.text == "likes ham:on" },
395
+ "likes ham check box missing"
396
+ )
397
+ assert_not_nil(
398
+ page.links.find { |l| l.text == "first_name:Aaron" },
399
+ "first_name field missing"
400
+ )
401
+ assert_not_nil(
402
+ page.links.find { |l| l.text == "gender:male" },
403
+ "gender field missing"
404
+ )
405
+ end
406
+
407
+ def test_post_with_param_in_action
408
+ page = @agent.get("http://localhost/form_test.html")
409
+ post_form = page.forms.find { |f| f.name == "post_form3" }
410
+ assert_not_nil(post_form, "Post form is null")
411
+ assert_equal("post", post_form.method.downcase)
412
+ assert_equal("/form_post?great day=yes&one=two", post_form.action)
413
+ assert_equal(1, post_form.fields.size)
414
+ assert_equal(1, post_form.buttons.size)
415
+ assert_equal(2, post_form.radiobuttons.size)
416
+ assert_equal(2, post_form.checkboxes.size)
417
+ assert_not_nil(post_form.fields.find { |f| f.name == "first_name" },
418
+ "First name field was nil"
419
+ )
420
+ assert_not_nil(
421
+ post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
422
+ "Gender male button was nil"
423
+ )
424
+
425
+ assert_not_nil(
426
+ post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
427
+ "Gender female button was nil"
428
+ )
429
+
430
+ assert_not_nil(post_form.checkbox_with(:name => "cool person"),
431
+ "couldn't find cool person checkbox")
432
+ assert_not_nil(post_form.checkboxes.find { |f| f.name == "likes ham" },
433
+ "couldn't find likes ham checkbox")
434
+
435
+ # Now set all the fields
436
+ post_form.field_with(:name => 'first_name').value = "Aaron"
437
+ post_form.radiobuttons.find { |f|
438
+ f.name == "gender" && f.value == "male"
439
+ }.checked = true
440
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
441
+ page = @agent.submit(post_form, post_form.buttons.first)
442
+
443
+ # Check that the submitted fields exist
444
+ assert_equal(3, page.links.size, "Not enough links")
445
+ assert_not_nil(
446
+ page.links.find { |l| l.text == "likes ham:on" },
447
+ "likes ham check box missing"
448
+ )
449
+ assert_not_nil(
450
+ page.links.find { |l| l.text == "first_name:Aaron" },
451
+ "first_name field missing"
452
+ )
453
+ assert_not_nil(
454
+ page.links.find { |l| l.text == "gender:male" },
455
+ "gender field missing"
456
+ )
457
+ end
458
+
459
+ def test_get_with_param_in_action
460
+ page = @agent.get("http://localhost/form_test.html")
461
+ get_form = page.forms.find { |f| f.name == "get_form3" }
462
+ assert_not_nil(get_form, "Get form is null")
463
+ assert_equal("get", get_form.method.downcase)
464
+ assert_equal("/form_post?great day=yes&one=two", get_form.action)
465
+ assert_equal(1, get_form.fields.size)
466
+ assert_equal(1, get_form.buttons.size)
467
+ assert_equal(2, get_form.radiobuttons.size)
468
+ assert_equal(2, get_form.checkboxes.size)
469
+ assert_not_nil(get_form.fields.find { |f| f.name == "first_name" },
470
+ "First name field was nil"
471
+ )
472
+ assert_not_nil(
473
+ get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
474
+ "Gender male button was nil"
475
+ )
476
+
477
+ assert_not_nil(
478
+ get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
479
+ "Gender female button was nil"
480
+ )
481
+
482
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "cool person" },
483
+ "couldn't find cool person checkbox")
484
+ assert_not_nil(get_form.checkboxes.find { |f| f.name == "likes ham" },
485
+ "couldn't find likes ham checkbox")
486
+
487
+ # Now set all the fields
488
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
489
+ get_form.radiobuttons.find { |f|
490
+ f.name == "gender" && f.value == "male"
491
+ }.checked = true
492
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
493
+ page = @agent.submit(get_form, get_form.buttons.first)
494
+ # Check that the submitted fields exist
495
+ assert_equal(3, page.links.size, "Not enough links")
496
+ assert_not_nil(
497
+ page.links.find { |l| l.text == "likes ham:on" },
498
+ "likes ham check box missing"
499
+ )
500
+ assert_not_nil(
501
+ page.links.find { |l| l.text == "first_name:Aaron" },
502
+ "first_name field missing"
503
+ )
504
+ assert_not_nil(
505
+ page.links.find { |l| l.text == "gender:male" },
506
+ "gender field missing"
507
+ )
508
+ end
509
+
510
+ def test_field_addition
511
+ page = @agent.get("http://localhost/form_test.html")
512
+ get_form = page.forms.find { |f| f.name == "get_form1" }
513
+ get_form.field("first_name").value = "Gregory"
514
+ assert_equal( "Gregory", get_form.field("first_name").value )
515
+ end
516
+
517
+ def test_fields_as_accessors
518
+ page = @agent.get("http://localhost/form_multival.html")
519
+ form = page.form_with(:name => 'post_form')
520
+
521
+ assert_not_nil(form)
522
+ assert_equal(2, form.fields_with(:name => 'first').length)
523
+
524
+ form.first = 'Aaron'
525
+ assert_equal('Aaron', form.first)
526
+ end
527
+
528
+ def test_add_field
529
+ page = @agent.get("http://localhost/form_multival.html")
530
+ form = page.form_with(:name => 'post_form')
531
+
532
+ assert_not_nil(form)
533
+ number_of_fields = form.fields.length
534
+
535
+ f = form.add_field!('intarweb')
536
+ assert_not_nil(f)
537
+ assert_equal(number_of_fields + 1, form.fields.length)
538
+ end
539
+
540
+ def test_delete_field
541
+ page = @agent.get("http://localhost/form_multival.html")
542
+ form = page.form_with(:name => 'post_form')
543
+
544
+ assert_not_nil(form)
545
+ number_of_fields = form.fields.length
546
+ assert_equal 2, number_of_fields
547
+
548
+ form.delete_field!('first')
549
+ assert_nil(form['first'])
550
+ assert_equal(number_of_fields - 2, form.fields.length)
551
+ end
552
+
553
+ def test_has_field
554
+ page = @agent.get("http://localhost/form_multival.html")
555
+ form = page.form_with(:name => 'post_form')
556
+
557
+ assert_not_nil(form)
558
+ assert_equal(false, form.has_field?('intarweb'))
559
+ f = form.add_field!('intarweb')
560
+ assert_not_nil(f)
561
+ assert_equal(true, form.has_field?('intarweb'))
562
+ end
563
+
564
+ def test_field_error
565
+ @page = @agent.get('http://localhost/empty_form.html')
566
+ form = @page.forms.first
567
+ assert_raise(NoMethodError) {
568
+ form.foo = 'asdfasdf'
569
+ }
570
+
571
+ assert_raise(NoMethodError) {
572
+ form.foo
573
+ }
574
+ end
575
+ end