neocoin-mechanize 2.0.2

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