allow unwrapped XMLs

This commit is contained in:
Benjamin Neff 2016-02-06 03:06:31 +01:00
parent 76f6929668
commit 5ce71f57d6
3 changed files with 21 additions and 22 deletions

View file

@ -33,11 +33,6 @@ module DiasporaFederation
class InvalidEncoding < RuntimeError class InvalidEncoding < RuntimeError
end 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 # Raised, if the entity name in the XML is invalid
class InvalidEntityName < RuntimeError class InvalidEntityName < RuntimeError
end end

View file

@ -18,6 +18,7 @@ module DiasporaFederation
# @param [Entity] entity subject # @param [Entity] entity subject
# @return [Nokogiri::XML::Element] XML root node # @return [Nokogiri::XML::Element] XML root node
# @raise [ArgumentError] if the argument is not an Entity subclass # @raise [ArgumentError] if the argument is not an Entity subclass
# @deprecated
def self.pack(entity) def self.pack(entity)
raise ArgumentError, "only instances of DiasporaFederation::Entity allowed" unless entity.is_a?(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 # @return [Entity] re-constructed Entity instance
# @raise [ArgumentError] if the argument is not an # @raise [ArgumentError] if the argument is not an
# {http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Element Nokogiri::XML::Element} # {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 # @raise [UnknownEntity] if the class for the entity contained inside the
# XML can't be found # XML can't be found
def self.unpack(xml) def self.unpack(xml)
raise ArgumentError, "only Nokogiri::XML::Element allowed" unless xml.instance_of?(Nokogiri::XML::Element) 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) klass_name = entity_class_name(data.name)
raise Salmon::UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name) raise Salmon::UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name)
@ -55,11 +54,12 @@ module DiasporaFederation
end end
# @param [Nokogiri::XML::Element] element # @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.name == "XML" && !element.at_xpath("post").nil? &&
!element.at_xpath("post").children.empty?) !element.at_xpath("post").children.empty?)
end end
private_class_method :wrap_valid? private_class_method :xml_wrapped?
# Transform the given String from the lowercase underscored version to a # Transform the given String from the lowercase underscored version to a
# camelized variant, used later for getting the Class constant. # camelized variant, used later for getting the Class constant.

View file

@ -67,7 +67,7 @@ XML
end end
end end
it "raises an error when the xml is wrong" do it "raises an error when the entity is unknown" do
xml = <<-XML xml = <<-XML
<XML> <XML>
<post> <post>
@ -81,17 +81,6 @@ XML
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root)
}.to raise_error Salmon::UnknownEntity, "'UnknownEntity' not found" }.to raise_error Salmon::UnknownEntity, "'UnknownEntity' not found"
end end
it "raises an error when the entity is not found" do
xml = <<-XML
<root>
<weird/>
</root>
XML
expect {
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root)
}.to raise_error Salmon::InvalidStructure
end
end end
context "returned object" do context "returned object" do
@ -107,6 +96,21 @@ XML
end end
end end
context "unwrapped xml" do
it "allows unwrapped entities" do
xml = <<-XML
<test_entity>
<test>asdf</test>
</test_entity>
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 context "parsing" do
it "uses xml_name for parsing" do it "uses xml_name for parsing" do
xml = <<-XML.strip xml = <<-XML.strip