# encoding: utf-8 require "logstash/devutils/rspec/spec_helper" require "logstash/filters/xml" describe LogStash::Filters::Xml do describe "parse standard xml (Deprecated checks)" do config <<-CONFIG filter { xml { source => "raw" target => "data" } } CONFIG sample("raw" => '') do insist { subject["tags"] }.nil? insist { subject["data"]} == {"key" => "value"} end #From parse xml with array as a value sample("raw" => 'value1value2') do insist { subject["tags"] }.nil? insist { subject["data"]} == {"key" => ["value1", "value2"]} end #From parse xml with hash as a value sample("raw" => 'value') do insist { subject["tags"] }.nil? insist { subject["data"]} == {"key1" => [{"key2" => ["value"]}]} end #From bad xml sample("raw" => ' "raw" target => "data" store_xml => false } } CONFIG sample("raw" => '') do insist { subject["tags"] }.nil? insist { subject["data"]} == nil end end describe "parse xml and store values with xpath (Deprecated checks)" do config <<-CONFIG filter { xml { source => "raw" target => "data" xpath => [ "/foo/key/text()", "xpath_field" ] } } CONFIG # Single value sample("raw" => 'value') do insist { subject["tags"] }.nil? insist { subject["xpath_field"]} == ["value"] end #Multiple values sample("raw" => 'value1value2') do insist { subject["tags"] }.nil? insist { subject["xpath_field"]} == ["value1","value2"] end end ## New tests describe "parse standard xml" do config <<-CONFIG filter { xml { source => "xmldata" target => "data" } } CONFIG sample("xmldata" => '') do insist { subject["tags"] }.nil? insist { subject["data"]} == {"key" => "value"} end #From parse xml with array as a value sample("xmldata" => 'value1value2') do insist { subject["tags"] }.nil? insist { subject["data"]} == {"key" => ["value1", "value2"]} end #From parse xml with hash as a value sample("xmldata" => 'value') do insist { subject["tags"] }.nil? insist { subject["data"]} == {"key1" => [{"key2" => ["value"]}]} end #From bad xml sample("xmldata" => ' "xmldata" target => "data" store_xml => false } } CONFIG sample("xmldata" => '') do insist { subject["tags"] }.nil? insist { subject["data"]} == nil end end describe "parse xml and store values with xpath" do config <<-CONFIG filter { xml { source => "xmldata" target => "data" xpath => [ "/foo/key/text()", "xpath_field" ] } } CONFIG # Single value sample("xmldata" => 'value') do insist { subject["tags"] }.nil? insist { subject["xpath_field"]} == ["value"] end #Multiple values sample("xmldata" => 'value1value2') do insist { subject["tags"] }.nil? insist { subject["xpath_field"]} == ["value1","value2"] end end describe "parse correctly non ascii content with xpath" do config <<-CONFIG filter { xml { source => "xmldata" target => "data" xpath => [ "/foo/key/text()", "xpath_field" ] } } CONFIG # Single value sample("xmldata" => 'Français') do insist { subject["tags"] }.nil? insist { subject["xpath_field"]} == ["Français"] end end end