diff --git a/test/xml/test_entity_reference.rb b/test/xml/test_entity_reference.rb index fcf15a39414..a6c2d0904c1 100644 --- a/test/xml/test_entity_reference.rb +++ b/test/xml/test_entity_reference.rb @@ -57,7 +57,7 @@ def setup @parser = Nokogiri::XML::Document end - test_relative_and_absolute_path :test_document_dtd_loading do + test_relative_and_absolute_path :test_dom_entity_reference_with_dtdloda do # Make sure that we can parse entity references and include them in the document html = File.read xml_document doc = @parser.parse html, path do |cfg| @@ -69,7 +69,7 @@ def setup assert_equal "foobar", doc.xpath('//blah').text end - test_relative_and_absolute_path :test_document_dtd_loading_with_dtdvalid do + test_relative_and_absolute_path :test_dom_entity_reference_with_dtdvalid do # Make sure that we can parse entity references and include them in the document html = File.read xml_document doc = @parser.parse html, path do |cfg| @@ -81,7 +81,7 @@ def setup assert_equal "foobar", doc.xpath('//blah').text end - test_absolute_path :test_document_dtd_loading_absolute_dtd_path do + test_absolute_path :test_dom_dtd_loading_with_absolute_path do # Make sure that we can parse entity references and include them in the document html = %Q[ @@ -98,19 +98,7 @@ def setup assert_equal "foobar", doc.xpath('//blah').text end - test_relative_and_absolute_path :test_document_dtd_loading_with_dtdvalid do - # Make sure that we can parse entity references and include them in the document - html = File.read xml_document - doc = @parser.parse html, path do |cfg| - cfg.default_xml - cfg.dtdvalid - cfg.noent - end - assert_equal [], doc.errors - assert_equal "foobar", doc.xpath('//blah').text - end - - test_relative_and_absolute_path :test_document_dtd_loading_with_io do + test_relative_and_absolute_path :test_dom_entity_reference_with_io do # Make sure that we can parse entity references and include them in the document html = File.open xml_document doc = @parser.parse html, nil do |cfg| @@ -122,24 +110,7 @@ def setup assert_equal "foobar", doc.xpath('//blah').text end - test_relative_and_absolute_path :test_document_dtd_loading_from_current_working_directory do - pwd = Dir.pwd - begin - Dir.chdir path - html = File.read 'document.xml' - doc = @parser.parse html, nil do |cfg| - cfg.default_xml - cfg.dtdload - cfg.noent - end - assert_equal [], doc.errors - assert_equal "foobar", doc.xpath('//blah').text - ensure - Dir.chdir pwd - end - end - - test_relative_and_absolute_path :test_document_dtd_loading_without_noent do + test_relative_and_absolute_path :test_dom_entity_reference_without_noent do # Make sure that we don't include entity references unless NOENT is set to true html = File.read xml_document doc = @parser.parse html, path do |cfg| @@ -150,7 +121,7 @@ def setup assert_kind_of Nokogiri::XML::EntityReference, doc.xpath('//body').first.children.first end - test_relative_and_absolute_path :test_document_dtd_loading_without_dtdload do + test_relative_and_absolute_path :test_dom_entity_reference_without_dtdload do # Make sure that we don't include entity references unless NOENT is set to true html = File.read xml_document doc = @parser.parse html, path do |cfg| @@ -163,7 +134,7 @@ def setup end test_relative_and_absolute_path :test_document_dtd_loading_with_nonet do - # Make sure that we don't include entity references unless NOENT is set to true + # Make sure that we don't include remote entities unless NOENT is set to true html = %Q[ @@ -181,41 +152,84 @@ def setup assert_equal ["Attempt to load network entity http://foo.bar.com/"], doc.errors.map(&:to_s) end end + # TODO: can we retreive a resource pointing to localhost when NONET is set to true ? + end - class TestReaderEntityReference < Nokogiri::TestCase - end - - class TestSaxEntityReference < Nokogiri::SAX::TestCase - include Common + class TestSaxEntityReference < Nokogiri::SAX::TestCase + include Common - def setup - super - @parser = XML::SAX::Parser.new(Doc.new) do |ctx| - ctx.replace_entities = true - end + def setup + super + @parser = XML::SAX::Parser.new(Doc.new) do |ctx| + ctx.replace_entities = true end + end - test_relative_and_absolute_path :test_document_dtd_loading do - # Make sure that we can parse entity references and include them in the document - html = File.read xml_document - @parser.parse html - refute_nil @parser.document.errors - assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip) - end + test_relative_and_absolute_path :test_sax_entity_reference do + # Make sure that we can parse entity references and include them in the document + html = File.read xml_document + @parser.parse html + refute_nil @parser.document.errors + assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip) + end - test_relative_and_absolute_path :test_document_dtd_loading_with_nonet do - # Make sure that we don't include entity references unless NOENT is set to true - html = %Q[ + test_relative_and_absolute_path :test_sax_entity_reference do + # Make sure that we don't include entity references unless NOENT is set to true + html = %Q[ &bar; ] - @parser.parse html - refute_nil @parser.document.errors - assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip) + @parser.parse html + refute_nil @parser.document.errors + assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip) + end + end + + class TestReaderEntityReference < Nokogiri::TestCase + include Common + + def setup + super + end + + test_relative_and_absolute_path :test_reader_entity_reference do + # Make sure that we can parse entity references and include them in the document + html = File.read xml_document + reader = Nokogiri::XML::Reader html, path do |cfg| + cfg.default_xml + cfg.dtdload + cfg.noent end - end # TODO: can we retreive a resource pointing to localhost when NONET is set to true ? + nodes = [] + reader.each { |n| nodes << n.value } + assert_equal ['foobar'], nodes.compact.map(&:strip).reject(&:empty?) + end + + test_relative_and_absolute_path :test_reader_entity_reference_without_noent do + # Make sure that we can parse entity references and include them in the document + html = File.read xml_document + reader = Nokogiri::XML::Reader html, path do |cfg| + cfg.default_xml + cfg.dtdload + end + nodes = [] + reader.each { |n| nodes << n.value } + assert_equal [], nodes.compact.map(&:strip).reject(&:empty?) + end + + test_relative_and_absolute_path :test_reader_entity_reference_without_dtdload do + # Make sure that we can parse entity references and include them in the document + html = File.read xml_document + assert_raises(Nokogiri::XML::SyntaxError)do + reader = Nokogiri::XML::Reader html, path do |cfg| + cfg.default_xml + end + nodes = [] + reader.each { |n| nodes << n.value } + end + end end end end