1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
# coding: utf-8
require 'mechanize/test_case'
class TestMechanizePageLink < Mechanize::TestCase
WINDOWS_1255 = <<-HTML
<meta http-equiv="content-type" content="text/html; charset=windows-1255">
<title>hi</title>
HTML
BAD = <<-HTML
<meta http-equiv="content-type" content="text/html; charset=windows-1255">
<title>Bia\xB3ystok</title>
HTML
BAD.force_encoding Encoding::BINARY if defined? Encoding
SJIS_TITLE = "\x83\x65\x83\x58\x83\x67"
SJIS_AFTER_TITLE = <<-HTML
<title>#{SJIS_TITLE}</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
HTML
SJIS_AFTER_TITLE.force_encoding Encoding::BINARY if defined? Encoding
SJIS_BAD_AFTER_TITLE = <<-HTML
<title>#{SJIS_TITLE}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
HTML
SJIS_BAD_AFTER_TITLE.force_encoding Encoding::BINARY if defined? Encoding
UTF8_TITLE = 'ใในใ'
UTF8 = <<-HTML
<title>#{UTF8_TITLE}</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
HTML
ENCODING_ERROR_CLASS = Nokogiri::XML::SyntaxError
def setup
super
@uri = URI('http://example')
@res = { 'content-type' => 'text/html' }
@body = '<title>hi</title>'
end
def util_page body = @body, res = @res
Mechanize::Page.new @uri, res, body && body.force_encoding(Encoding::BINARY), 200, @mech
end
def nkf_dependency?
if RUBY_ENGINE == 'ruby'
false
else
meth = caller[0][/`(\w+)/, 1]
warn "#{meth}: skipped because this feature currently depends on NKF"
true
end
end
def test_override_content_type
page = Mechanize::Page.new nil, {'content-type' => 'text/html'}, WINDOWS_1255
assert page
assert_equal 'text/html; charset=windows-1255', page.content_type
end
def test_canonical_uri
page = @mech.get("http://localhost/canonical_uri.html")
assert_equal(URI("http://localhost/canonical_uri"), page.canonical_uri)
page = @mech.get("http://localhost/file_upload.html")
assert_nil page.canonical_uri
end
def test_canonical_uri_unescaped
page = util_page <<-BODY
<head>
<link rel="canonical" href="http://example/white space"/>
</head>
BODY
assert_equal @uri + '/white%20space', page.canonical_uri
end
def test_charset_from_content_type
charset = Mechanize::Page.__send__ :charset_from_content_type, 'text/html;charset=UTF-8'
assert_equal 'UTF-8', charset
end
def test_charset_from_bad_content_type
charset = Mechanize::Page.__send__ :charset_from_content_type, 'text/html'
assert_nil charset
end
def test_encoding
page = util_page WINDOWS_1255
assert_equal 'windows-1255', page.encoding
end
def test_encoding_charset_after_title
page = util_page SJIS_AFTER_TITLE
assert_equal false, page.encoding_error?
assert_equal 'Shift_JIS', page.encoding
end
def test_encoding_charset_after_title_bad
return if nkf_dependency?
page = util_page UTF8
assert_equal false, page.encoding_error?
assert_equal 'UTF-8', page.encoding
end
def test_encoding_charset_after_title_double_bad
return if nkf_dependency?
page = util_page SJIS_BAD_AFTER_TITLE
assert_equal false, page.encoding_error?
assert_equal 'SHIFT_JIS', page.encoding
end
def test_encoding_charset_bad
return if nkf_dependency?
page = util_page "<title>#{UTF8_TITLE}</title>"
page.encodings.replace %w[
UTF-8
Shift_JIS
]
assert_equal false, page.encoding_error?
assert_equal 'UTF-8', page.encoding
end
def test_encoding_meta_charset
page = util_page "<meta charset='UTF-8'>"
assert_equal 'UTF-8', page.encoding
end
def test_encoding_equals
page = util_page
page.meta_refresh
assert page.instance_variable_get(:@meta_refresh)
page.encoding = 'UTF-8'
assert_nil page.instance_variable_get(:@meta_refresh)
assert_equal 'UTF-8', page.encoding
assert_equal 'UTF-8', page.parser.encoding
end
def test_page_encoding_error?
page = util_page
page.parser.errors.clear
assert_equal false, page.encoding_error?
end
def test_detect_libxml2error_indicate_encoding
page = util_page
page.parser.errors.clear
# error in libxml2-2.7.8/parser.c, HTMLparser.c or parserInternals.c
page.parser.errors = [ENCODING_ERROR_CLASS.new("Input is not proper UTF-8, indicate encoding !\n")]
assert_equal true, page.encoding_error?
end
def test_detect_libxml2error_invalid_char
page = util_page
page.parser.errors.clear
# error in libxml2-2.7.8/HTMLparser.c
page.parser.errors = [ENCODING_ERROR_CLASS.new("Invalid char in CDATA 0x%X\n")]
assert_equal true, page.encoding_error?
end
def test_detect_libxml2error_input_conversion_failed
page = util_page
page.parser.errors.clear
# error in libxml2-2.7.8/encoding.c
page.parser.errors = [ENCODING_ERROR_CLASS.new("input conversion failed due to input error\n")]
assert_equal true, page.encoding_error?
end
def test_detect_libxml2error_which_unsupported_by_mechanize
page = util_page
page.parser.errors.clear
# error in libxml2-2.7.8/HTMLparser.c
page.parser.errors = [ENCODING_ERROR_CLASS.new("encoder error\n")]
assert_equal false, page.encoding_error?
end
def test_encoding_equals_before_parser
# document has a bad encoding information - windows-1255
page = util_page BAD
# encoding is wrong, so user wants to force ISO-8859-2
page.encoding = 'ISO-8859-2'
assert_equal false, page.encoding_error?
assert_equal 'ISO-8859-2', page.encoding
assert_equal 'ISO-8859-2', page.parser.encoding
end
def test_encoding_equals_after_parser
# document has a bad encoding information - windows-1255
page = util_page BAD
page.parser
# autodetection sets encoding to windows-1255
assert_equal 'windows-1255', page.encoding
# believe in yourself, not machine
assert_equal false, page.encoding_error?
# encoding is wrong, so user wants to force ISO-8859-2
page.encoding = 'ISO-8859-2'
assert_equal false, page.encoding_error?
assert_equal 'ISO-8859-2', page.encoding
assert_equal 'ISO-8859-2', page.parser.encoding
end
def test_frames_with
page = @mech.get("http://localhost/frame_test.html")
assert_equal(3, page.frames.size)
find_orig = page.frames.find_all { |f| f.name == 'frame1' }
find1 = page.frames_with(:name => 'frame1')
find_orig.zip(find1).each { |a,b|
assert_equal(a, b)
}
end
def test_links_with_dom_id
page = @mech.get("http://localhost/tc_links.html")
link = page.links_with(:dom_id => 'bold_aaron_link')
link_by_id = page.links_with(:id => 'bold_aaron_link')
assert_equal(1, link.length)
assert_equal('Aaron Patterson', link.first.text)
assert_equal(link, link_by_id)
end
def test_links_with_dom_class
page = @mech.get("http://localhost/tc_links.html")
link = page.links_with(:dom_class => 'thing_link')
link_by_class = page.links_with(:class => 'thing_link')
assert_equal(1, link.length)
assert_equal(link, link_by_class)
end
def test_link_with_encoded_space
page = @mech.get("http://localhost/tc_links.html")
link = page.link_with(:text => 'encoded space')
page = @mech.click link
end
def test_link_with_space
page = @mech.get("http://localhost/tc_links.html")
link = page.link_with(:text => 'not encoded space')
page = @mech.click link
end
def test_link_with_unusual_characters
page = @mech.get("http://localhost/tc_links.html")
link = page.link_with(:text => 'unusual characters')
@mech.click link
# HACK no assertion
end
def test_links
page = @mech.get("http://localhost/find_link.html")
assert_equal(18, page.links.length)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse FRAME outside of FRAMESET
def test_links_with_bold
page = @mech.get("http://localhost/tc_links.html")
link = page.links_with(:text => /Bold Dude/)
assert_equal(1, link.length)
assert_equal('Bold Dude', link.first.text)
assert_equal [], link.first.rel
assert !link.first.rel?('me')
assert !link.first.rel?('nofollow')
link = page.links_with(:text => 'Aaron James Patterson')
assert_equal(1, link.length)
assert_equal('Aaron James Patterson', link.first.text)
assert_equal ['me'], link.first.rel
assert link.first.rel?('me')
assert !link.first.rel?('nofollow')
link = page.links_with(:text => 'Aaron Patterson')
assert_equal(1, link.length)
assert_equal('Aaron Patterson', link.first.text)
assert_equal ['me', 'nofollow'], link.first.rel
assert link.first.rel?('me')
assert link.first.rel?('nofollow')
link = page.links_with(:text => 'Ruby Rocks!')
assert_equal(1, link.length)
assert_equal('Ruby Rocks!', link.first.text)
end
def test_meta_refresh
page = @mech.get("http://localhost/find_link.html")
assert_equal(3, page.meta_refresh.length)
assert_equal(%w{
http://www.drphil.com/
http://www.upcase.com/
http://tenderlovemaking.com/ }.sort,
page.meta_refresh.map { |x| x.href.downcase }.sort)
end
def test_title
page = util_page
assert_equal('hi', page.title)
end
def test_title_none
page = util_page '' # invalid HTML
assert_nil(page.title)
end
def test_page_decoded_with_charset
page = util_page @body, 'content-type' => 'text/html; charset=EUC-JP'
assert_equal 'EUC-JP', page.encoding
assert_equal 'EUC-JP', page.parser.encoding
end
def test_form
page = @mech.get("http://localhost/tc_form_action.html")
form = page.form(:name => 'post_form1')
assert form
yielded = false
form = page.form(:name => 'post_form1') { |f|
yielded = true
assert f
assert_equal(form, f)
}
assert yielded
form_by_action = page.form(:action => '/form_post?a=b&b=c')
assert form_by_action
assert_equal(form, form_by_action)
end
end
|