require "helper" module Nokogiri module XML class TestC14N < Nokogiri::TestCase # http://www.w3.org/TR/xml-c14n#Example-OutsideDoc def test_3_1 doc = Nokogiri.XML <<-eoxml Hello, world! eoxml c14n = doc.canonicalize assert_no_match(/version=/, c14n) assert_match(/Hello, world/, c14n) assert_no_match(/Comment/, c14n) c14n = doc.canonicalize(nil, nil, true) assert_match(/Comment/, c14n) c14n = doc.canonicalize(nil, nil, false) assert_no_match(/Comment/, c14n) end def test_exclude_block_params xml = '' doc = Nokogiri.XML xml list = [] doc.canonicalize do |node, parent| list << [node, parent] true end if Nokogiri.jruby? assert_equal( ['a', 'document', 'document', nil, 'b', 'a'], list.flatten.map { |x| x ? x.name : x } ) else assert_equal( ['a', 'document', 'document', nil, 'b', 'a', 'a', 'document'], list.flatten.map { |x| x ? x.name : x } ) end end def test_exclude_block_true xml = '' doc = Nokogiri.XML xml c14n = doc.canonicalize do |node, parent| true end assert_equal xml, c14n end def test_exclude_block_false xml = '' doc = Nokogiri.XML xml c14n = doc.canonicalize do |node, parent| false end assert_equal '', c14n end def test_exclude_block_nil xml = '' doc = Nokogiri.XML xml c14n = doc.canonicalize do |node, parent| nil end assert_equal '', c14n end def test_exclude_block_object xml = '' doc = Nokogiri.XML xml c14n = doc.canonicalize do |node, parent| Object.new end assert_equal xml, c14n end def test_c14n_node xml = '' doc = Nokogiri.XML xml c14n = doc.at_xpath('//b').canonicalize assert_equal '', c14n end def test_c14n_modes # http://www.w3.org/TR/xml-exc-c14n/#sec-Enveloping doc1 = Nokogiri.XML <<-EOXML EOXML doc2 = Nokogiri.XML <<-EOXML EOXML c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize assert_equal ' ', c14n expected = ' ' c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize assert_equal expected, c14n expected = ' ' c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0) assert_equal expected, c14n expected = ' ' c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0) assert_equal expected, c14n expected = ' ' c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0, ['n2']) assert_equal expected, c14n expected = ' ' c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0, ['n2', 'n4']) assert_equal expected, c14n end def test_wrong_params xml = '' doc = Nokogiri.XML xml assert_raise(TypeError){ doc.canonicalize :wrong_type } assert_raise(TypeError){ doc.canonicalize nil, :wrong_type } doc.canonicalize nil, nil, :wrong_type end end end end