From 5ce71f57d6d58d285f7bf8d6204a3d0258a8278e Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 6 Feb 2016 03:06:31 +0100 Subject: [PATCH] allow unwrapped XMLs --- lib/diaspora_federation/salmon/exceptions.rb | 5 ---- lib/diaspora_federation/salmon/xml_payload.rb | 10 +++---- .../salmon/xml_payload_spec.rb | 28 +++++++++++-------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/diaspora_federation/salmon/exceptions.rb b/lib/diaspora_federation/salmon/exceptions.rb index 44ae542..649b9a6 100644 --- a/lib/diaspora_federation/salmon/exceptions.rb +++ b/lib/diaspora_federation/salmon/exceptions.rb @@ -33,11 +33,6 @@ module DiasporaFederation class InvalidEncoding < RuntimeError end - # Raised, if the XML structure of the parsed document doesn't resemble the - # expected structure. - class InvalidStructure < RuntimeError - end - # Raised, if the entity name in the XML is invalid class InvalidEntityName < RuntimeError end diff --git a/lib/diaspora_federation/salmon/xml_payload.rb b/lib/diaspora_federation/salmon/xml_payload.rb index 7439be3..50bae71 100644 --- a/lib/diaspora_federation/salmon/xml_payload.rb +++ b/lib/diaspora_federation/salmon/xml_payload.rb @@ -18,6 +18,7 @@ module DiasporaFederation # @param [Entity] entity subject # @return [Nokogiri::XML::Element] XML root node # @raise [ArgumentError] if the argument is not an Entity subclass + # @deprecated def self.pack(entity) raise ArgumentError, "only instances of DiasporaFederation::Entity allowed" unless entity.is_a?(Entity) @@ -39,14 +40,12 @@ module DiasporaFederation # @return [Entity] re-constructed Entity instance # @raise [ArgumentError] if the argument is not an # {http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Element Nokogiri::XML::Element} - # @raise [InvalidStructure] if the XML doesn't look like the wrapper XML # @raise [UnknownEntity] if the class for the entity contained inside the # XML can't be found def self.unpack(xml) raise ArgumentError, "only Nokogiri::XML::Element allowed" unless xml.instance_of?(Nokogiri::XML::Element) - raise Salmon::InvalidStructure unless wrap_valid?(xml) - data = xml.at_xpath("post/*[1]") + data = xml_wrapped?(xml) ? xml.at_xpath("post/*[1]") : xml klass_name = entity_class_name(data.name) raise Salmon::UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name) @@ -55,11 +54,12 @@ module DiasporaFederation end # @param [Nokogiri::XML::Element] element - def self.wrap_valid?(element) + # @deprecated + def self.xml_wrapped?(element) (element.name == "XML" && !element.at_xpath("post").nil? && !element.at_xpath("post").children.empty?) end - private_class_method :wrap_valid? + private_class_method :xml_wrapped? # Transform the given String from the lowercase underscored version to a # camelized variant, used later for getting the Class constant. diff --git a/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb b/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb index 8165483..1489053 100644 --- a/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb +++ b/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb @@ -67,7 +67,7 @@ XML end end - it "raises an error when the xml is wrong" do + it "raises an error when the entity is unknown" do xml = <<-XML @@ -81,17 +81,6 @@ XML Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) }.to raise_error Salmon::UnknownEntity, "'UnknownEntity' not found" end - - it "raises an error when the entity is not found" do - xml = <<-XML - - - -XML - expect { - Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) - }.to raise_error Salmon::InvalidStructure - end end context "returned object" do @@ -107,6 +96,21 @@ XML end end + context "unwrapped xml" do + it "allows unwrapped entities" do + xml = <<-XML + + asdf + + XML + + entity = Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) + + expect(entity).to be_an_instance_of Entities::TestEntity + expect(entity.test).to eq("asdf") + end + end + context "parsing" do it "uses xml_name for parsing" do xml = <<-XML.strip