From e9318eb99377b1e7c96e3d4d072ec9250680f30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Wn=C4=99trzak?= Date: Tue, 11 Oct 2011 22:44:09 +0200 Subject: [PATCH] parse response error using multi xml --- .gitignore | 1 + Gemfile | 1 + alexa.gemspec | 1 + lib/alexa/url_info.rb | 4 ++-- test/fixtures/auth_failure.txt | 7 +++++++ test/helper.rb | 21 +++++++++++++++++---- test/url_info_test.rb | 18 +++++++++++++++--- 7 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/auth_failure.txt diff --git a/.gitignore b/.gitignore index 780d2c5..d84d990 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ pkg/* +tmp/ *.gem .bundle Gemfile.lock diff --git a/Gemfile b/Gemfile index b76c046..026b0d8 100644 --- a/Gemfile +++ b/Gemfile @@ -2,5 +2,6 @@ source :rubygems gem "minitest", :platform => :ruby_18 gem "rake" +gem "ruby-debug19", :require => "ruby-debug", :platform => :ruby_19 gemspec diff --git a/alexa.gemspec b/alexa.gemspec index 5f7432d..37223e1 100644 --- a/alexa.gemspec +++ b/alexa.gemspec @@ -18,4 +18,5 @@ Gem::Specification.new do |gem| gem.add_dependency "multi_xml" gem.add_development_dependency "mocha" + gem.add_development_dependency "fakeweb" end diff --git a/lib/alexa/url_info.rb b/lib/alexa/url_info.rb index cec2b93..d2653fe 100644 --- a/lib/alexa/url_info.rb +++ b/lib/alexa/url_info.rb @@ -70,8 +70,8 @@ def handle_response(response) raise StandardError.new(response) else @xml_response = response.body - xml = XmlSimple.xml_in(response.body, 'ForceArray' => false) - message = xml['Errors']['Error']['Message'] + xml = MultiXml.parse(@xml_response) + message = xml["Response"]["Errors"]["Error"]["Message"] raise StandardError.new(message) end else diff --git a/test/fixtures/auth_failure.txt b/test/fixtures/auth_failure.txt new file mode 100644 index 0000000..5a92760 --- /dev/null +++ b/test/fixtures/auth_failure.txt @@ -0,0 +1,7 @@ +HTTP/1.1 401 Unauthorized +Server: Apache-Coyote/1.1 +Transfer-Encoding: chunked +Date: Tue, 11 Oct 2011 20:16:19 GMT + + +AuthFailureAWS was not able to authenticate the request: access credentials are missing57d5192f-803a-1d6f-a50a-c354840a02b9 \ No newline at end of file diff --git a/test/helper.rb b/test/helper.rb index 6e5d866..1ab156a 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,11 +1,24 @@ -Bundler.setup +require "bundler/setup" require "minitest/autorun" +require "fakeweb" require "mocha" require "alexa" -def fixture_file(filename) - file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename) - File.read(file_path) +class MiniTest::Unit::TestCase + def setup + FakeWeb.allow_net_connect = false + end + + def fixture_xml(filename) + file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename) + File.read(file_path) + end + + # Recording response is as simple as writing in terminal: + # curl -is "http://awis.amazonaws.com/?Action=UrlInfo&AWSAccessKeyId=fake" -X GET > response.txt + def fixture(filename) + File.read(File.join("test", "fixtures", filename)) + end end diff --git a/test/url_info_test.rb b/test/url_info_test.rb index cec74b4..7bafc57 100644 --- a/test/url_info_test.rb +++ b/test/url_info_test.rb @@ -26,7 +26,7 @@ describe "parsing xml returned by options LinksInCount,SiteData" do before do @alexa.response_group = "Rank,LinksInCount,SiteData" - xml = fixture_file('polsl_small.xml') + xml = fixture_xml('polsl_small.xml') @alexa.parse_xml(xml) end @@ -53,7 +53,7 @@ describe "parsing xml with all options" do before do - xml = fixture_file('polsl.xml') + xml = fixture_xml('polsl.xml') @alexa.parse_xml(xml) end @@ -116,7 +116,7 @@ describe "parsing empty xml response" do before do - xml = fixture_file('empty.xml') + xml = fixture_xml('empty.xml') @alexa.parse_xml(xml) end @@ -176,4 +176,16 @@ assert_nil @alexa.usage_statistics end end + + describe "parsing xml with auth failure" do + before do + FakeWeb.register_uri(:get, %r{http://awis.amazonaws.com}, :response => fixture("auth_failure.txt")) + end + + it "should raise error" do + assert_raises StandardError do + @alexa.connect + end + end + end end