Skip to content

Commit

Permalink
parse response error using multi xml
Browse files Browse the repository at this point in the history
  • Loading branch information
morgoth committed Oct 11, 2011
1 parent 1d1a6cc commit e9318eb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pkg/*
tmp/
*.gem
.bundle
Gemfile.lock
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ source :rubygems

gem "minitest", :platform => :ruby_18
gem "rake"
gem "ruby-debug19", :require => "ruby-debug", :platform => :ruby_19

gemspec
1 change: 1 addition & 0 deletions alexa.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/alexa/url_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/auth_failure.txt
Original file line number Diff line number Diff line change
@@ -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

<?xml version="1.0"?>
<Response><Errors><Error><Code>AuthFailure</Code><Message>AWS was not able to authenticate the request: access credentials are missing</Message></Error></Errors><RequestID>57d5192f-803a-1d6f-a50a-c354840a02b9</RequestID></Response>
21 changes: 17 additions & 4 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 15 additions & 3 deletions test/url_info_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit e9318eb

Please sign in to comment.