Skip to content

Commit

Permalink
REXML backend no longer transforms errors
Browse files Browse the repository at this point in the history
...because Formatter.decode method takes care of ensuring all errors are OEmbed::ParseError instances.
  • Loading branch information
metavida committed Apr 3, 2016
1 parent 0b0d906 commit 6afedc4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
7 changes: 0 additions & 7 deletions lib/oembed/formatter/xml/backends/rexml.rb
Expand Up @@ -17,13 +17,6 @@ def decode(xml)
obj[el.name] = el.text
end
obj
rescue
case $!
when parse_error
raise $!
else
raise parse_error, 'Couldn\'t parse the given document.'
end
end

def decode_fail_msg
Expand Down
71 changes: 50 additions & 21 deletions spec/formatter/xml/rexml_backend_spec.rb
Expand Up @@ -8,7 +8,7 @@
OEmbed::Formatter::XML.backend = 'REXML'
}.to_not raise_error

expect(!!defined?(REXML)).to eq(true)
expect(defined?(REXML)).to be_truthy
end

it 'should support XML' do
Expand All @@ -18,38 +18,67 @@
end

it 'should be using the XmlSimple backend' do
expect(OEmbed::Formatter::XML.backend).to eq(OEmbed::Formatter::XML::Backends::REXML)
expect(OEmbed::Formatter::XML.backend)
.to eq(OEmbed::Formatter::XML::Backends::REXML)
end

it 'should decode an XML String' do
decoded = OEmbed::Formatter.decode(:xml, valid_response(:xml))
# We need to compare keys & values separately because we don't expect all
# non-string values to be recognized correctly.
expect(decoded.keys).to eq(valid_response(:object).keys)
expect(decoded.values.map(&:to_s)).to eq(valid_response(:object).values.map(&:to_s))
expect(decoded.values.map(&:to_s))
.to eq(valid_response(:object).values.map(&:to_s))
end

it 'should raise an OEmbed::ParseError when decoding an invalid XML String' do
expect {
decode = OEmbed::Formatter.decode(:xml, invalid_response('unclosed_container', :xml))
}.to raise_error(OEmbed::ParseError)
expect {
decode = OEmbed::Formatter.decode(:xml, invalid_response('unclosed_tag', :xml))
}.to raise_error(OEmbed::ParseError)
expect {
decode = OEmbed::Formatter.decode(:xml, invalid_response('invalid_syntax', :xml))
}.to raise_error(OEmbed::ParseError)
RSpec.shared_examples 'a backend' do
around(:example) do |example|
RSpec::Expectations
.configuration.warn_about_potential_false_positives = false

example.run

RSpec::Expectations
.configuration.warn_about_potential_false_positives = true
end

it 'should not catch that error when decoding' do
expect {
OEmbed::Formatter::XML::Backends::REXML.decode(xml_input)
}.to raise_error
end
end

it 'should raise an OEmbed::ParseError when decoding fails with an unexpected error' do
error_to_raise = ArgumentError
expect(OEmbed::Formatter::XML.backend.parse_error).to_not be_kind_of(error_to_raise)
context 'given an unclosed xml continer' do
it_behaves_like 'a backend' do
let(:xml_input) { invalid_response('unclosed_container', :xml) }
end
end

expect(::REXML::Document).to receive(:new)
.and_raise(error_to_raise.new('unknown error'))
context 'given an unclosed xml tag' do
it_behaves_like 'a backend' do
let(:xml_input) { invalid_response('unclosed_tag', :xml) }
end
end

expect {
decode = OEmbed::Formatter.decode(:xml, valid_response(:xml))
}.to raise_error(OEmbed::ParseError)
context 'given invalid xml syntax' do
it_behaves_like 'a backend' do
let(:xml_input) { invalid_response('invalid_syntax', :xml) }
end
end

context 'given an unexpected error when parsing xml' do
it 'should not catch that error when decoding' do
error_to_raise = Interrupt
expect(OEmbed::Formatter::XML::Backends::REXML.parse_error)
.to_not be_kind_of(error_to_raise)

expect(::REXML::Document).to receive(:new)
.and_raise(error_to_raise.new('unknown error'))

expect {
OEmbed::Formatter::XML::Backends::REXML.decode(valid_response(:xml))
}.to raise_error(error_to_raise)
end
end
end
2 changes: 1 addition & 1 deletion spec/formatter/xml/xmlsimple_backend_spec.rb
Expand Up @@ -73,7 +73,7 @@

context 'given an unexpected error when parsing xml' do
it 'should not catch that error when decoding' do
error_to_raise = RuntimeError
error_to_raise = Interrupt
expect(OEmbed::Formatter::XML.backend.parse_error)
.to_not be_kind_of(error_to_raise)

Expand Down

0 comments on commit 6afedc4

Please sign in to comment.