aai10-mechanize 2.0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. data/.autotest +6 -0
  2. data/.gitignore +9 -0
  3. data/CHANGELOG.rdoc +652 -0
  4. data/EXAMPLES.rdoc +187 -0
  5. data/FAQ.rdoc +11 -0
  6. data/GUIDE.rdoc +163 -0
  7. data/LICENSE.rdoc +20 -0
  8. data/Manifest.txt +172 -0
  9. data/README.rdoc +63 -0
  10. data/Rakefile +36 -0
  11. data/aai10-mechanize.gemspec +20 -0
  12. data/examples/flickr_upload.rb +22 -0
  13. data/examples/mech-dump.rb +5 -0
  14. data/examples/proxy_req.rb +7 -0
  15. data/examples/rubyforge.rb +20 -0
  16. data/examples/spider.rb +21 -0
  17. data/lib/mechanize.rb +664 -0
  18. data/lib/mechanize/content_type_error.rb +14 -0
  19. data/lib/mechanize/cookie.rb +116 -0
  20. data/lib/mechanize/cookie_jar.rb +202 -0
  21. data/lib/mechanize/element_matcher.rb +35 -0
  22. data/lib/mechanize/file.rb +80 -0
  23. data/lib/mechanize/file_connection.rb +17 -0
  24. data/lib/mechanize/file_request.rb +26 -0
  25. data/lib/mechanize/file_response.rb +74 -0
  26. data/lib/mechanize/file_saver.rb +37 -0
  27. data/lib/mechanize/form.rb +478 -0
  28. data/lib/mechanize/form/button.rb +9 -0
  29. data/lib/mechanize/form/check_box.rb +11 -0
  30. data/lib/mechanize/form/field.rb +44 -0
  31. data/lib/mechanize/form/file_upload.rb +23 -0
  32. data/lib/mechanize/form/image_button.rb +20 -0
  33. data/lib/mechanize/form/multi_select_list.rb +83 -0
  34. data/lib/mechanize/form/option.rb +49 -0
  35. data/lib/mechanize/form/radio_button.rb +48 -0
  36. data/lib/mechanize/form/select_list.rb +40 -0
  37. data/lib/mechanize/headers.rb +25 -0
  38. data/lib/mechanize/history.rb +83 -0
  39. data/lib/mechanize/http.rb +3 -0
  40. data/lib/mechanize/http/agent.rb +738 -0
  41. data/lib/mechanize/inspect.rb +88 -0
  42. data/lib/mechanize/monkey_patch.rb +37 -0
  43. data/lib/mechanize/page.rb +408 -0
  44. data/lib/mechanize/page/base.rb +8 -0
  45. data/lib/mechanize/page/frame.rb +27 -0
  46. data/lib/mechanize/page/image.rb +30 -0
  47. data/lib/mechanize/page/label.rb +20 -0
  48. data/lib/mechanize/page/link.rb +82 -0
  49. data/lib/mechanize/page/meta_refresh.rb +56 -0
  50. data/lib/mechanize/pluggable_parsers.rb +101 -0
  51. data/lib/mechanize/redirect_limit_reached_error.rb +16 -0
  52. data/lib/mechanize/redirect_not_get_or_head_error.rb +19 -0
  53. data/lib/mechanize/response_code_error.rb +22 -0
  54. data/lib/mechanize/response_read_error.rb +27 -0
  55. data/lib/mechanize/robots_disallowed_error.rb +29 -0
  56. data/lib/mechanize/unsupported_scheme_error.rb +8 -0
  57. data/lib/mechanize/util.rb +113 -0
  58. data/test/data/htpasswd +1 -0
  59. data/test/data/server.crt +16 -0
  60. data/test/data/server.csr +12 -0
  61. data/test/data/server.key +15 -0
  62. data/test/data/server.pem +15 -0
  63. data/test/helper.rb +175 -0
  64. data/test/htdocs/alt_text.html +10 -0
  65. data/test/htdocs/bad_form_test.html +9 -0
  66. data/test/htdocs/button.jpg +0 -0
  67. data/test/htdocs/canonical_uri.html +9 -0
  68. data/test/htdocs/dir with spaces/foo.html +1 -0
  69. data/test/htdocs/empty_form.html +6 -0
  70. data/test/htdocs/file_upload.html +26 -0
  71. data/test/htdocs/find_link.html +41 -0
  72. data/test/htdocs/form_multi_select.html +16 -0
  73. data/test/htdocs/form_multival.html +37 -0
  74. data/test/htdocs/form_no_action.html +18 -0
  75. data/test/htdocs/form_no_input_name.html +16 -0
  76. data/test/htdocs/form_select.html +16 -0
  77. data/test/htdocs/form_select_all.html +16 -0
  78. data/test/htdocs/form_select_none.html +17 -0
  79. data/test/htdocs/form_select_noopts.html +10 -0
  80. data/test/htdocs/form_set_fields.html +14 -0
  81. data/test/htdocs/form_test.html +188 -0
  82. data/test/htdocs/frame_referer_test.html +10 -0
  83. data/test/htdocs/frame_test.html +30 -0
  84. data/test/htdocs/google.html +13 -0
  85. data/test/htdocs/iframe_test.html +16 -0
  86. data/test/htdocs/index.html +6 -0
  87. data/test/htdocs/link with space.html +5 -0
  88. data/test/htdocs/meta_cookie.html +11 -0
  89. data/test/htdocs/no_title_test.html +6 -0
  90. data/test/htdocs/nofollow.html +9 -0
  91. data/test/htdocs/noindex.html +9 -0
  92. data/test/htdocs/norobots.html +8 -0
  93. data/test/htdocs/rails_3_encoding_hack_form_test.html +27 -0
  94. data/test/htdocs/rel_nofollow.html +8 -0
  95. data/test/htdocs/relative/tc_relative_links.html +21 -0
  96. data/test/htdocs/robots.html +8 -0
  97. data/test/htdocs/robots.txt +2 -0
  98. data/test/htdocs/tc_bad_charset.html +9 -0
  99. data/test/htdocs/tc_bad_links.html +5 -0
  100. data/test/htdocs/tc_base_images.html +10 -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_charset.html +6 -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_field_precedence.html +11 -0
  107. data/test/htdocs/tc_follow_meta.html +8 -0
  108. data/test/htdocs/tc_form_action.html +48 -0
  109. data/test/htdocs/tc_images.html +8 -0
  110. data/test/htdocs/tc_links.html +18 -0
  111. data/test/htdocs/tc_meta_in_body.html +9 -0
  112. data/test/htdocs/tc_no_attributes.html +16 -0
  113. data/test/htdocs/tc_pretty_print.html +17 -0
  114. data/test/htdocs/tc_radiobuttons.html +17 -0
  115. data/test/htdocs/tc_referer.html +16 -0
  116. data/test/htdocs/tc_relative_links.html +19 -0
  117. data/test/htdocs/tc_textarea.html +23 -0
  118. data/test/htdocs/test_bad_encoding.html +52 -0
  119. data/test/htdocs/test_click.html +11 -0
  120. data/test/htdocs/unusual______.html +5 -0
  121. data/test/servlets.rb +402 -0
  122. data/test/ssl_server.rb +48 -0
  123. data/test/test_cookies.rb +129 -0
  124. data/test/test_form_action.rb +52 -0
  125. data/test/test_form_as_hash.rb +59 -0
  126. data/test/test_form_button.rb +46 -0
  127. data/test/test_frames.rb +34 -0
  128. data/test/test_headers.rb +33 -0
  129. data/test/test_history.rb +118 -0
  130. data/test/test_history_added.rb +16 -0
  131. data/test/test_html_unscape_forms.rb +46 -0
  132. data/test/test_if_modified_since.rb +20 -0
  133. data/test/test_images.rb +19 -0
  134. data/test/test_mechanize.rb +852 -0
  135. data/test/test_mechanize_cookie.rb +345 -0
  136. data/test/test_mechanize_cookie_jar.rb +433 -0
  137. data/test/test_mechanize_file.rb +53 -0
  138. data/test/test_mechanize_file_request.rb +19 -0
  139. data/test/test_mechanize_file_response.rb +21 -0
  140. data/test/test_mechanize_form.rb +576 -0
  141. data/test/test_mechanize_form_check_box.rb +37 -0
  142. data/test/test_mechanize_form_encoding.rb +120 -0
  143. data/test/test_mechanize_form_field.rb +21 -0
  144. data/test/test_mechanize_form_image_button.rb +12 -0
  145. data/test/test_mechanize_form_textarea.rb +51 -0
  146. data/test/test_mechanize_http_agent.rb +697 -0
  147. data/test/test_mechanize_link.rb +84 -0
  148. data/test/test_mechanize_page_encoding.rb +147 -0
  149. data/test/test_mechanize_page_link.rb +382 -0
  150. data/test/test_mechanize_page_meta_refresh.rb +115 -0
  151. data/test/test_mechanize_redirect_not_get_or_head_error.rb +18 -0
  152. data/test/test_mechanize_subclass.rb +22 -0
  153. data/test/test_mechanize_util.rb +92 -0
  154. data/test/test_multi_select.rb +118 -0
  155. data/test/test_no_attributes.rb +13 -0
  156. data/test/test_option.rb +18 -0
  157. data/test/test_pluggable_parser.rb +136 -0
  158. data/test/test_post_form.rb +37 -0
  159. data/test/test_pretty_print.rb +22 -0
  160. data/test/test_radiobutton.rb +75 -0
  161. data/test/test_redirect_limit_reached.rb +39 -0
  162. data/test/test_redirect_ok.rb +25 -0
  163. data/test/test_referer.rb +81 -0
  164. data/test/test_relative_links.rb +40 -0
  165. data/test/test_request.rb +13 -0
  166. data/test/test_response_code.rb +53 -0
  167. data/test/test_robots.rb +72 -0
  168. data/test/test_save_file.rb +48 -0
  169. data/test/test_scheme.rb +48 -0
  170. data/test/test_select.rb +119 -0
  171. data/test/test_select_all.rb +15 -0
  172. data/test/test_select_none.rb +15 -0
  173. data/test/test_select_noopts.rb +18 -0
  174. data/test/test_set_fields.rb +44 -0
  175. data/test/test_ssl_server.rb +20 -0
  176. metadata +360 -0
@@ -0,0 +1,16 @@
1
+ require "helper"
2
+
3
+ class HistoryAddedTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ end
7
+
8
+ def test_history_added_gets_called
9
+ onload = 0
10
+ @agent.history_added = lambda { |page|
11
+ onload += 1
12
+ }
13
+ @agent.get('http://localhost/tc_blank_form.html')
14
+ assert_equal(1, onload)
15
+ end
16
+ end
@@ -0,0 +1,46 @@
1
+ require "helper"
2
+
3
+ class TestCheckBoxes < MiniTest::Unit::TestCase
4
+ def test_field
5
+ f = Mechanize::Form::Field.new({'name' => 'a&amp;b'}, 'a&amp;b')
6
+ assert_equal('a&b', f.name)
7
+ assert_equal('a&b', f.value)
8
+
9
+ f = Mechanize::Form::Field.new({'name' => 'a&b'}, 'a&b')
10
+ assert_equal('a&b', f.name)
11
+ assert_equal('a&b', f.value)
12
+
13
+ f = Mechanize::Form::Field.new({'name' => 'a&#38;b'}, 'a&#38;b')
14
+ assert_equal('a&b', f.name)
15
+ assert_equal('a&b', f.value)
16
+ end
17
+
18
+ def test_file_upload
19
+ f = Mechanize::Form::FileUpload.new(fake_node, 'a&amp;b')
20
+ assert_equal('a&b', f.name)
21
+ assert_equal('a&b', f.file_name)
22
+
23
+ f = Mechanize::Form::FileUpload.new(fake_node, 'a&b')
24
+ assert_equal('a&b', f.name)
25
+ assert_equal('a&b', f.file_name)
26
+ end
27
+
28
+ def test_image_button
29
+ f = Mechanize::Form::ImageButton.new({'name' => 'a&amp;b'}, 'a&amp;b')
30
+ assert_equal('a&b', f.name)
31
+ assert_equal('a&b', f.value)
32
+ end
33
+
34
+ def test_radio_button
35
+ f = Mechanize::Form::RadioButton.new(fake_node, nil)
36
+ assert_equal('a&b', f.name)
37
+ assert_equal('a&b', f.value)
38
+ end
39
+
40
+ def fake_node
41
+ {
42
+ 'name' => 'a&amp;b',
43
+ 'value' => 'a&amp;b'
44
+ }
45
+ end
46
+ end
@@ -0,0 +1,20 @@
1
+ require "helper"
2
+
3
+ class TestIfModifiedSince < MiniTest::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ end
7
+
8
+ def test_get_twice
9
+ assert_equal(0, @agent.history.length)
10
+ page = @agent.get('http://localhost/if_modified_since')
11
+ assert_match(/You did not send/, page.body)
12
+
13
+ assert_equal(1, @agent.history.length)
14
+ page2 = @agent.get('http://localhost/if_modified_since')
15
+
16
+ assert_equal(2, @agent.history.length)
17
+ assert_equal(page.object_id, page2.object_id)
18
+ assert_match(/You did not send/, page.body)
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require "helper"
2
+
3
+ class ImagesMechTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ end
7
+
8
+ def test_base
9
+ page = @agent.get("http://google.com/tc_base_images.html")
10
+ assert_equal page.images[0].url, "http://localhost/a.jpg"
11
+ assert_equal page.images[1].url, "http://localhost/b.gif"
12
+ end
13
+
14
+ def test_base2
15
+ page = @agent.get("http://google.com/tc_images.html")
16
+ assert_equal page.images[0].url, "http://google.com/a.jpg"
17
+ assert_equal page.images[1].url, "http://google.com/b.gif"
18
+ end
19
+ end
@@ -0,0 +1,852 @@
1
+ # coding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class TestMechanize < MiniTest::Unit::TestCase
6
+
7
+ KEY = OpenSSL::PKey::RSA.new 512
8
+ name = OpenSSL::X509::Name.parse 'CN=nobody/DC=example'
9
+ CERT = OpenSSL::X509::Certificate.new
10
+ CERT.version = 2
11
+ CERT.serial = 0
12
+ CERT.not_before = Time.now
13
+ CERT.not_after = Time.now + 60
14
+ CERT.public_key = KEY.public_key
15
+ CERT.subject = name
16
+ CERT.issuer = name
17
+ CERT.sign KEY, OpenSSL::Digest::SHA1.new
18
+
19
+ def setup
20
+ @mech = Mechanize.new
21
+ @mech.log = nil
22
+ @uri = URI.parse 'http://example/'
23
+ @req = Net::HTTP::Get.new '/'
24
+
25
+ @res = Net::HTTPOK.allocate
26
+ @res.instance_variable_set :@code, 200
27
+ @res.instance_variable_set :@header, {}
28
+
29
+ @headers = if RUBY_VERSION > '1.9' then
30
+ %w[accept user-agent]
31
+ else
32
+ %w[accept]
33
+ end
34
+ end
35
+
36
+ def test_back
37
+ 0.upto(5) do |i|
38
+ assert_equal(i, @mech.history.size)
39
+ @mech.get("http://localhost/")
40
+ end
41
+ @mech.get("http://localhost/form_test.html")
42
+
43
+ assert_equal("http://localhost/form_test.html",
44
+ @mech.history.last.uri.to_s)
45
+ assert_equal("http://localhost/",
46
+ @mech.history[-2].uri.to_s)
47
+
48
+ assert_equal(7, @mech.history.size)
49
+ @mech.back
50
+ assert_equal(6, @mech.history.size)
51
+ assert_equal("http://localhost/",
52
+ @mech.history.last.uri.to_s)
53
+ end
54
+
55
+ def test_basic_auth
56
+ @mech.basic_auth('user', 'pass')
57
+ page = @mech.get("http://localhost/basic_auth")
58
+ assert_equal('You are authenticated', page.body)
59
+ end
60
+
61
+ def test_cert_key_file
62
+ Tempfile.open 'key' do |key|
63
+ Tempfile.open 'cert' do |cert|
64
+ key.write KEY.to_pem
65
+ key.rewind
66
+
67
+ cert.write CERT.to_pem
68
+ cert.rewind
69
+
70
+ mech = Mechanize.new do |a|
71
+ a.cert = cert.path
72
+ a.key = key.path
73
+ end
74
+
75
+ # Certificate#== seems broken
76
+ assert_equal CERT.to_pem, mech.certificate.to_pem
77
+ end
78
+ end
79
+ end
80
+
81
+ def test_cert_key_object
82
+ mech = Mechanize.new do |a|
83
+ a.cert = CERT
84
+ a.key = KEY
85
+ end
86
+
87
+ assert_equal CERT, mech.certificate
88
+ end
89
+
90
+ def test_click
91
+ @mech.user_agent_alias = 'Mac Safari'
92
+ page = @mech.get("http://localhost/frame_test.html")
93
+ link = page.link_with(:text => "Form Test")
94
+
95
+ page = @mech.click(link)
96
+
97
+ assert_equal("http://localhost/form_test.html",
98
+ @mech.history.last.uri.to_s)
99
+ end
100
+
101
+ def test_click_frame_hpricot_style
102
+ page = @mech.get("http://localhost/frame_test.html")
103
+ link = (page/"//frame[@name='frame2']").first
104
+
105
+ page = @mech.click(link)
106
+
107
+ assert_equal("http://localhost/form_test.html",
108
+ @mech.history.last.uri.to_s)
109
+ end
110
+
111
+ def test_click_hpricot_style # HACK move to test_divide in Page
112
+ page = @mech.get("http://localhost/frame_test.html")
113
+ link = (page/"//a[@class='bar']").first
114
+
115
+ page = @mech.click(link)
116
+
117
+ assert_equal("http://localhost/form_test.html",
118
+ @mech.history.last.uri.to_s)
119
+ end
120
+
121
+ def test_click_link
122
+ agent = Mechanize.new
123
+ agent.user_agent_alias = 'Mac Safari'
124
+ page = agent.get("http://localhost/frame_test.html")
125
+ link = page.link_with(:text => "Form Test")
126
+
127
+ agent.click link
128
+
129
+ assert_equal("http://localhost/form_test.html",
130
+ agent.history.last.uri.to_s)
131
+ end
132
+
133
+ def test_click_link_hpricot_style # HACK move to test_search in Page
134
+ page = @mech.get("http://localhost/tc_encoded_links.html")
135
+
136
+ page = @mech.click(page.search('a').first)
137
+
138
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
139
+ end
140
+
141
+ def test_click_link_query
142
+ page = @mech.get("http://localhost/tc_encoded_links.html")
143
+ link = page.links.first
144
+ assert_equal('/form_post?a=b&b=c', link.href)
145
+
146
+ page = @mech.click(link)
147
+
148
+ assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
149
+ end
150
+
151
+ def test_click_link_space
152
+ page = @mech.get("http://localhost/tc_bad_links.html")
153
+
154
+ @mech.click page.links.first
155
+
156
+ assert_match(/alt_text.html$/, @mech.history.last.uri.to_s)
157
+ assert_equal(2, @mech.history.length)
158
+ end
159
+
160
+ def test_click_more
161
+ @mech.get 'http://localhost/test_click.html'
162
+ @mech.click 'A Button'
163
+ assert_equal 'http://localhost/frame_test.html?words=nil',
164
+ @mech.page.uri.to_s
165
+ @mech.back
166
+ @mech.click 'A Link'
167
+ assert_equal 'http://localhost/index.html',
168
+ @mech.page.uri.to_s
169
+ @mech.back
170
+ @mech.click @mech.page.link_with(:text => 'A Link')
171
+ assert_equal 'http://localhost/index.html',
172
+ @mech.page.uri.to_s
173
+ end
174
+
175
+ def test_cookie_jar
176
+ assert_kind_of Mechanize::CookieJar, @mech.cookie_jar
177
+
178
+ jar = Mechanize::CookieJar.new
179
+
180
+ @mech.cookie_jar = jar
181
+
182
+ assert_equal jar, @mech.cookie_jar
183
+ end
184
+
185
+ def test_delete
186
+ page = @mech.delete('http://localhost/verb', { 'q' => 'foo' })
187
+ assert_equal 1, @mech.history.length
188
+ assert_equal 'DELETE', page.header['X-Request-Method']
189
+ end
190
+
191
+ def test_delete_redirect
192
+ page = @mech.delete('http://localhost/redirect')
193
+
194
+ assert_equal(page.uri.to_s, 'http://localhost/verb')
195
+
196
+ assert_equal 'GET', page.header['X-Request-Method']
197
+ end
198
+
199
+ #def test_download
200
+ # Dir.mktmpdir do |dir|
201
+ # file = "#{dir}/download"
202
+ # open file, 'w' do |io|
203
+ # @mech.download 'http://example', io
204
+ # end
205
+
206
+ # assert_equal 1, File.stat(file).size
207
+ # end
208
+ #end
209
+
210
+ def test_get
211
+ page = @mech.get('http://localhost', { :q => 'h' }, 'http://example',
212
+ { 'X-H' => 'v' })
213
+
214
+ assert_equal 'http://localhost/?q=h', page.uri.to_s
215
+ end
216
+
217
+ def test_get_HTTP
218
+ page = @mech.get('HTTP://localhost/', { :q => 'hello' })
219
+ assert_equal('HTTP://localhost/?q=hello', page.uri.to_s)
220
+ end
221
+
222
+ def test_get_anchor
223
+ page = @mech.get('http://localhost/?foo=bar&#34;')
224
+ assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
225
+ end
226
+
227
+ def test_get_bad_url
228
+ assert_raises ArgumentError do
229
+ @mech.get '/foo.html'
230
+ end
231
+ end
232
+
233
+ def test_get_basic_auth_bad
234
+ @mech.basic_auth('aaron', 'aaron')
235
+
236
+ e = assert_raises Mechanize::ResponseCodeError do
237
+ @mech.get("http://localhost/basic_auth")
238
+ end
239
+
240
+ assert_equal("401", e.response_code)
241
+ end
242
+
243
+ def test_get_basic_auth_none
244
+ e = assert_raises Mechanize::ResponseCodeError do
245
+ @mech.get("http://localhost/basic_auth")
246
+ end
247
+
248
+ assert_equal("401", e.response_code)
249
+ end
250
+
251
+ def test_get_digest_auth
252
+ block_called = false
253
+
254
+ @mech.basic_auth('user', 'pass')
255
+
256
+ @mech.pre_connect_hooks << lambda { |_, request|
257
+ block_called = true
258
+ request.to_hash.each do |k,v|
259
+ assert_equal(1, v.length)
260
+ end
261
+ }
262
+
263
+ page = @mech.get("http://localhost/digest_auth")
264
+ assert_equal('You are authenticated', page.body)
265
+ assert block_called
266
+ end
267
+
268
+ def test_get_file
269
+ page = @mech.get("http://localhost/frame_test.html")
270
+ content_length = page.header['Content-Length']
271
+ page_as_string = @mech.get_file("http://localhost/frame_test.html")
272
+ assert_equal(content_length.to_i, page_as_string.length.to_i)
273
+ end
274
+
275
+ def test_get_follow_meta_refresh
276
+ @mech.follow_meta_refresh = true
277
+
278
+ page = @mech.get('http://localhost/tc_follow_meta.html')
279
+
280
+ assert_equal(2, @mech.history.length)
281
+
282
+ assert_equal('http://localhost/tc_follow_meta.html',
283
+ @mech.history.first.uri.to_s)
284
+ assert_equal('http://localhost/index.html', page.uri.to_s)
285
+ assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
286
+ end
287
+
288
+ def test_get_follow_meta_refresh_anywhere
289
+ @mech.follow_meta_refresh = :anywhere
290
+ requests = []
291
+ @mech.pre_connect_hooks << lambda { |_, request|
292
+ requests << request
293
+ }
294
+
295
+ @mech.get('http://localhost/tc_meta_in_body.html')
296
+ assert_equal 2, requests.length
297
+ end
298
+
299
+ def test_get_follow_meta_refresh_disabled
300
+ page = @mech.get('http://localhost/tc_follow_meta.html')
301
+ assert_equal('http://localhost/tc_follow_meta.html', page.uri.to_s)
302
+ assert_equal(1, page.meta_refresh.length)
303
+ end
304
+
305
+ def test_get_follow_meta_refresh_empty_url
306
+ @mech.follow_meta_refresh = true
307
+
308
+ page = @mech.get('http://localhost/refresh_with_empty_url')
309
+
310
+ assert_equal(3, @mech.history.length)
311
+ assert_equal('http://localhost/refresh_with_empty_url',
312
+ @mech.history[0].uri.to_s)
313
+ assert_equal('http://localhost/refresh_with_empty_url',
314
+ @mech.history[1].uri.to_s)
315
+ assert_equal('http://localhost/index.html', page.uri.to_s)
316
+ assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
317
+ end
318
+
319
+ def test_get_follow_meta_refresh_in_body
320
+ @mech.follow_meta_refresh = true
321
+ requests = []
322
+ @mech.pre_connect_hooks << lambda { |_, request|
323
+ requests << request
324
+ }
325
+
326
+ @mech.get('http://localhost/tc_meta_in_body.html')
327
+ assert_equal 1, requests.length
328
+ end
329
+
330
+ def test_get_follow_meta_refresh_no_url
331
+ @mech.follow_meta_refresh = true
332
+
333
+ page = @mech.get('http://localhost/refresh_without_url')
334
+
335
+ assert_equal(3, @mech.history.length)
336
+ assert_equal('http://localhost/refresh_without_url',
337
+ @mech.history[0].uri.to_s)
338
+ assert_equal('http://localhost/refresh_without_url',
339
+ @mech.history[1].uri.to_s)
340
+ assert_equal('http://localhost/index.html', page.uri.to_s)
341
+ assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
342
+ end
343
+
344
+ def test_get_follow_meta_refresh_referer_not_sent
345
+ @mech.follow_meta_refresh = true
346
+
347
+ requests = []
348
+
349
+ @mech.pre_connect_hooks << lambda { |_, request|
350
+ requests << request
351
+ }
352
+
353
+ @mech.get('http://localhost/tc_follow_meta.html')
354
+
355
+ assert_equal 2, @mech.history.length
356
+ assert_nil requests.last['referer']
357
+ end
358
+
359
+ def test_get_gzip
360
+ page = @mech.get("http://localhost/gzip?file=index.html")
361
+
362
+ assert_kind_of(Mechanize::Page, page)
363
+
364
+ assert_match('Hello World', page.body)
365
+ end
366
+
367
+ def test_get_http_refresh
368
+ @mech.follow_meta_refresh = true
369
+ page = @mech.get('http://localhost/http_refresh?refresh_time=0')
370
+ assert_equal('http://localhost/index.html', page.uri.to_s)
371
+ assert_equal(2, @mech.history.length)
372
+ end
373
+
374
+ def test_get_http_refresh_delay
375
+ @mech.follow_meta_refresh = true
376
+ class << @mech.agent
377
+ attr_accessor :slept
378
+ def sleep *args
379
+ @slept = args
380
+ end
381
+ end
382
+
383
+ @mech.get('http://localhost/http_refresh?refresh_time=1')
384
+ assert_equal [1], @mech.agent.slept
385
+ end
386
+
387
+ def test_get_http_refresh_disabled
388
+ page = @mech.get('http://localhost/http_refresh?refresh_time=0')
389
+ assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
390
+ end
391
+
392
+ def test_get_kcode
393
+ $KCODE = 'u'
394
+ page = @mech.get("http://localhost/?a=#{[0xd6].pack('U')}")
395
+
396
+ assert_equal('http://localhost/?a=%D6', page.uri.to_s)
397
+
398
+ $KCODE = 'NONE'
399
+ end unless RUBY_VERSION >= '1.9.0'
400
+
401
+ def test_get_query
402
+ page = @mech.get('http://localhost/', { :q => 'hello' })
403
+ assert_equal('http://localhost/?q=hello', page.uri.to_s)
404
+ end
405
+
406
+ def test_get_redirect
407
+ page = @mech.get('http://localhost/redirect')
408
+
409
+ assert_equal(page.uri.to_s, 'http://localhost/verb')
410
+
411
+ assert_equal 'GET', page.header['X-Request-Method']
412
+ end
413
+
414
+ def test_get_redirect_found
415
+ page = @mech.get('http://localhost/response_code?code=302&ct=test/xml')
416
+
417
+ assert_equal('http://localhost/index.html', page.uri.to_s)
418
+
419
+ assert_equal(2, @mech.history.length)
420
+ end
421
+
422
+ def test_get_redirect_infinite
423
+ assert_raises(Mechanize::RedirectLimitReachedError) {
424
+ @mech.get('http://localhost/infinite_refresh')
425
+ }
426
+ end
427
+
428
+ def test_get_referer
429
+ request = nil
430
+ @mech.pre_connect_hooks << lambda { |_, req|
431
+ request = req
432
+ }
433
+
434
+ @mech.get('http://localhost/', [], 'http://tenderlovemaking.com/')
435
+ assert_equal 'http://tenderlovemaking.com/', request['Referer']
436
+ end
437
+
438
+ def test_get_referer_file
439
+ uri = URI.parse 'http://tenderlovemaking.com/crossdomain.xml'
440
+ file = Mechanize::File.new uri
441
+
442
+ @mech.get('http://localhost', [], file)
443
+
444
+ # HACK no assertion of behavior
445
+ end
446
+
447
+ def test_get_referer_none
448
+ requests = []
449
+ @mech.pre_connect_hooks << lambda { |_, request|
450
+ requests << request
451
+ }
452
+
453
+ @mech.get('http://localhost/')
454
+ @mech.get('http://localhost/')
455
+ assert_equal(2, requests.length)
456
+ requests.each do |request|
457
+ assert_nil request['referer']
458
+ end
459
+ end
460
+
461
+ def test_get_scheme_unsupported
462
+ assert_raises Mechanize::UnsupportedSchemeError do
463
+ @mech.get('ftp://server.com/foo.html')
464
+ end
465
+ end
466
+
467
+ def test_get_space
468
+ page = nil
469
+
470
+ page = @mech.get("http://localhost/tc_bad_links.html ")
471
+
472
+ assert_match(/tc_bad_links.html$/, @mech.history.last.uri.to_s)
473
+
474
+ assert_equal(1, @mech.history.length)
475
+ end
476
+
477
+ def test_get_tilde
478
+ page = @mech.get('http://localhost/?foo=~2')
479
+
480
+ assert_equal('http://localhost/?foo=~2', page.uri.to_s)
481
+ end
482
+
483
+ def test_get_weird
484
+ @mech.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
485
+ @mech.get('http://localhost/?a=b&#038;b=c&#038;c=d')
486
+ @mech.get("http://localhost/?a=#{[0xd6].pack('U')}")
487
+
488
+ # HACK no assertion of behavior
489
+ end
490
+
491
+ def test_get_yield
492
+ pages = nil
493
+
494
+ @mech.get("http://localhost/file_upload.html") { |page|
495
+ pages = page
496
+ }
497
+
498
+ assert pages
499
+ assert_equal('File Upload Form', pages.title)
500
+ end
501
+
502
+ def test_head
503
+ page = @mech.head('http://localhost/verb', { 'q' => 'foo' })
504
+ assert_equal 0, @mech.history.length
505
+ assert_equal 'HEAD', page.header['X-Request-Method']
506
+ end
507
+
508
+ def test_head_redirect
509
+ page = @mech.head('http://localhost/redirect')
510
+
511
+ assert_equal(page.uri.to_s, 'http://localhost/verb')
512
+
513
+ assert_equal 'HEAD', page.header['X-Request-Method']
514
+ end
515
+
516
+ def test_history
517
+ 2.times do |i|
518
+ assert_equal(i, @mech.history.size)
519
+
520
+ @mech.get("http://localhost/")
521
+ end
522
+
523
+ page = @mech.get("http://localhost/form_test.html")
524
+
525
+ assert_equal("http://localhost/form_test.html",
526
+ @mech.history.last.uri.to_s)
527
+ assert_equal("http://localhost/",
528
+ @mech.history[-2].uri.to_s)
529
+
530
+ assert @mech.visited?("http://localhost/")
531
+ assert @mech.visited?("/form_test.html"), 'relative'
532
+ assert !@mech.visited?("http://google.com/")
533
+ assert @mech.visited?(page.links.first)
534
+ end
535
+
536
+ def test_history_order
537
+ @mech.max_history = 2
538
+ assert_equal(0, @mech.history.length)
539
+
540
+ @mech.get('http://localhost/form_test.html')
541
+ assert_equal(1, @mech.history.length)
542
+
543
+ @mech.get('http://localhost/empty_form.html')
544
+ assert_equal(2, @mech.history.length)
545
+
546
+ @mech.get('http://localhost/tc_checkboxes.html')
547
+ assert_equal(2, @mech.history.length)
548
+ assert_equal('http://localhost/empty_form.html', @mech.history[0].uri.to_s)
549
+ assert_equal('http://localhost/tc_checkboxes.html',
550
+ @mech.history[1].uri.to_s)
551
+ end
552
+
553
+ def test_html_parser_equals
554
+ @mech.html_parser = {}
555
+ assert_raises(NoMethodError) {
556
+ @mech.get('http://localhost/?foo=~2').links
557
+ }
558
+ end
559
+
560
+ def test_keep_alive_time
561
+ assert_equal 0, @mech.keep_alive_time
562
+
563
+ @mech.keep_alive_time = 1
564
+
565
+ assert_equal 1, @mech.keep_alive_time
566
+ end
567
+
568
+ def test_max_history_equals
569
+ @mech.max_history = 10
570
+ 0.upto(10) do |i|
571
+ assert_equal(i, @mech.history.size)
572
+ @mech.get("http://localhost/")
573
+ end
574
+
575
+ 0.upto(10) do |i|
576
+ assert_equal(10, @mech.history.size)
577
+ @mech.get("http://localhost/")
578
+ end
579
+ end
580
+
581
+ def test_open_timeout_equals
582
+ @mech.open_timeout = 5
583
+
584
+ assert_equal 5, @mech.open_timeout
585
+ end
586
+
587
+ def test_post_basic_auth
588
+ requests = []
589
+
590
+ @mech.pre_connect_hooks << proc { |agent, request|
591
+ requests << request.class
592
+ }
593
+
594
+ @mech.basic_auth('user', 'pass')
595
+ page = @mech.post("http://localhost/basic_auth")
596
+ assert_equal('You are authenticated', page.body)
597
+ assert_equal(2, requests.length)
598
+ r1 = requests[0]
599
+ r2 = requests[1]
600
+ assert_equal(r1, r2)
601
+ end
602
+
603
+ def test_post_multipart
604
+ page = @mech.post('http://localhost/file_upload', {
605
+ :name => 'Some file',
606
+ :userfile1 => File.open(__FILE__)
607
+ })
608
+
609
+ name = File.basename __FILE__
610
+ assert_match(
611
+ "Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"",
612
+ page.body
613
+ )
614
+ assert page.body.length > File.read(__FILE__).length
615
+ end
616
+
617
+ def test_post_redirect
618
+ page = @mech.post('http://localhost/redirect')
619
+
620
+ assert_equal(page.uri.to_s, 'http://localhost/verb')
621
+
622
+ assert_equal 'GET', page.header['X-Request-Method']
623
+ end
624
+
625
+ def test_put
626
+ page = @mech.put('http://localhost/verb', 'foo')
627
+ assert_equal 1, @mech.history.length
628
+ assert_equal 'PUT', page.header['X-Request-Method']
629
+ end
630
+
631
+ def test_put_redirect
632
+ page = @mech.put('http://localhost/redirect', 'foo')
633
+
634
+ assert_equal(page.uri.to_s, 'http://localhost/verb')
635
+
636
+ assert_equal 'GET', page.header['X-Request-Method']
637
+ end
638
+
639
+ def test_read_timeout_equals
640
+ @mech.read_timeout = 5
641
+
642
+ assert_equal 5, @mech.read_timeout
643
+ end
644
+
645
+ def test_set_proxy
646
+ http = @mech.agent.http
647
+
648
+ @mech.set_proxy 'localhost', 8080, 'user', 'pass'
649
+
650
+ assert_equal 'localhost', @mech.proxy_addr
651
+ assert_equal 8080, @mech.proxy_port
652
+ assert_equal 'user', @mech.proxy_user
653
+ assert_equal 'pass', @mech.proxy_pass
654
+
655
+ refute_same http, @mech.agent.http
656
+ end
657
+
658
+ def test_submit_bad_form_method
659
+ page = @mech.get("http://localhost/bad_form_test.html")
660
+ assert_raises ArgumentError do
661
+ @mech.submit(page.forms.first)
662
+ end
663
+ end
664
+
665
+ def test_submit_check_one
666
+ page = @mech.get('http://localhost/tc_checkboxes.html')
667
+ form = page.forms.first
668
+ form.checkboxes_with(:name => 'green')[1].check
669
+
670
+ page = @mech.submit(form)
671
+
672
+ assert_equal(1, page.links.length)
673
+ assert_equal('green:on', page.links.first.text)
674
+ end
675
+
676
+ def test_submit_check_two
677
+ page = @mech.get('http://localhost/tc_checkboxes.html')
678
+ form = page.forms.first
679
+ form.checkboxes_with(:name => 'green')[0].check
680
+ form.checkboxes_with(:name => 'green')[1].check
681
+
682
+ page = @mech.submit(form)
683
+
684
+ assert_equal(2, page.links.length)
685
+ assert_equal('green:on', page.links[0].text)
686
+ assert_equal('green:on', page.links[1].text)
687
+ end
688
+
689
+ def test_submit_enctype
690
+ page = @mech.get("http://localhost/file_upload.html")
691
+ assert_equal('multipart/form-data', page.forms[0].enctype)
692
+
693
+ form = page.forms.first
694
+ form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
695
+ form.file_uploads.first.mime_type = "text/plain"
696
+ form.file_uploads.first.file_data = "Hello World\n\n"
697
+
698
+ page = @mech.submit(form)
699
+
700
+ assert_match(
701
+ "Content-Disposition: form-data; name=\"userfile1\"; filename=\"helper.rb\"",
702
+ page.body
703
+ )
704
+ assert_match(
705
+ "Content-Disposition: form-data; name=\"name\"",
706
+ page.body
707
+ )
708
+ assert_match('Content-Type: text/plain', page.body)
709
+ assert_match('Hello World', page.body)
710
+ assert_match('foo[aaron]', page.body)
711
+ end
712
+
713
+ def test_submit_file_data
714
+ page = @mech.get("http://localhost/file_upload.html")
715
+ assert_equal('multipart/form-data', page.forms[1].enctype)
716
+
717
+ form = page.forms[1]
718
+ form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
719
+ form.file_uploads.first.file_data = File.open("#{BASE_DIR}/helper.rb", 'rb')
720
+
721
+ page = @mech.submit(form)
722
+
723
+ contents = File.open("#{BASE_DIR}/helper.rb", 'rb') { |f| f.read }
724
+ assert_match(
725
+ "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"helper.rb\"",
726
+ page.body
727
+ )
728
+ assert_match(contents, page.body)
729
+ end
730
+
731
+ def test_submit_file_name
732
+ page = @mech.get("http://localhost/file_upload.html")
733
+ assert_equal('multipart/form-data', page.forms[1].enctype)
734
+
735
+ form = page.forms[1]
736
+ form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
737
+
738
+ page = @mech.submit(form)
739
+
740
+ contents = File.open("#{BASE_DIR}/helper.rb", 'rb') { |f| f.read }
741
+ assert_match(
742
+ "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"helper.rb\"",
743
+ page.body
744
+ )
745
+ assert_match(contents, page.body)
746
+ end
747
+
748
+ def test_submit_headers
749
+ page = @mech.get 'http://localhost:2000/form_no_action.html'
750
+
751
+ assert form = page.forms.first
752
+ form.action = '/http_headers'
753
+
754
+ page = @mech.submit form, nil, 'foo' => 'bar'
755
+
756
+ headers = page.body.split("\n").map { |x| x.split('|', 2) }.flatten
757
+ headers = Hash[*headers]
758
+
759
+ assert_equal 'bar', headers['foo']
760
+ end
761
+
762
+ def test_submit_multipart
763
+ page = @mech.get("http://localhost/file_upload.html")
764
+
765
+ assert_equal('multipart/form-data', page.forms[1].enctype)
766
+
767
+ form = page.forms[1]
768
+ form.file_uploads.first.file_name = "#{BASE_DIR}/helper.rb"
769
+ form.file_uploads.first.mime_type = "text/plain"
770
+ form.file_uploads.first.file_data = "Hello World\n\n"
771
+
772
+ page = @mech.submit(form)
773
+
774
+ assert_match(
775
+ "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"helper.rb\"",
776
+ page.body
777
+ )
778
+ end
779
+
780
+ def test_submit_no_file
781
+ page = @mech.get("http://localhost/file_upload.html")
782
+ form = page.forms.first
783
+ form.field_with(:name => 'name').value = 'Aaron'
784
+ @page = @mech.submit(form)
785
+ assert_match('Aaron', @page.body)
786
+ assert_match(
787
+ "Content-Disposition: form-data; name=\"userfile1\"; filename=\"\"",
788
+ @page.body
789
+ )
790
+ end
791
+
792
+ def test_submit_too_many_radiobuttons
793
+ page = @mech.get("http://localhost/form_test.html")
794
+ form = page.form_with(:name => 'post_form1')
795
+ form.radiobuttons.each { |r| r.checked = true }
796
+
797
+ assert_raises Mechanize::Error do
798
+ @mech.submit(form)
799
+ end
800
+ end
801
+
802
+ def test_transact
803
+ @mech.get("http://localhost/frame_test.html")
804
+ assert_equal(1, @mech.history.length)
805
+ @mech.transact { |a|
806
+ 5.times {
807
+ @mech.get("http://localhost/frame_test.html")
808
+ }
809
+ assert_equal(6, @mech.history.length)
810
+ }
811
+ assert_equal(1, @mech.history.length)
812
+ end
813
+
814
+ def test_user_agent_alias_equals_unknown
815
+ assert_raises ArgumentError do
816
+ @mech.user_agent_alias = "Aaron's Browser"
817
+ end
818
+ end
819
+
820
+ def test_visited_eh
821
+ @mech.get("http://localhost/content_type_test?ct=application/pdf")
822
+
823
+ assert \
824
+ @mech.visited?("http://localhost/content_type_test?ct=application/pdf")
825
+ assert \
826
+ !@mech.visited?("http://localhost/content_type_test")
827
+ assert \
828
+ !@mech.visited?("http://localhost/content_type_test?ct=text/html")
829
+ end
830
+
831
+ def test_visited_eh_redirect
832
+ @mech.get("http://localhost/response_code?code=302")
833
+
834
+ assert_equal("http://localhost/index.html", @mech.current_page.uri.to_s)
835
+
836
+ assert @mech.visited?('http://localhost/response_code?code=302')
837
+ end
838
+
839
+ def assert_header(page, header)
840
+ headers = {}
841
+
842
+ page.body.split(/[\r\n]+/).each do |page_header|
843
+ headers.[]=(*page_header.chomp.split(/\|/))
844
+ end
845
+
846
+ header.each do |key, value|
847
+ assert(headers.has_key?(key))
848
+ assert_equal(value, headers[key])
849
+ end
850
+ end
851
+ end
852
+