From 5efbcfbb747a7f566433621be3b67a66287f076f Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Tue, 6 Nov 2012 01:09:02 -0800 Subject: [PATCH] Raise a Binged::Search::Error in the bad auth case, rather than JSON::ParserError. --- HISTORY | 1 + lib/binged/search/base.rb | 6 +++++- spec/binged/search/web_spec.rb | 13 +++++++++++++ spec/fixtures/bad_request.curl | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/bad_request.curl diff --git a/HISTORY b/HISTORY index 665dfc5..7a6ecad 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,6 @@ == 1.0.0 (unreleased) * Migrate to Bing Search API 2.0. See https://datamarket.azure.com/dataset/bing/search and http://go.microsoft.com/fwlink/?LinkID=248077 for more details. +* Raise a Bing::Search::Error in at least some of the failure modes. == 0.3.0 2010-03-15 diff --git a/lib/binged/search/base.rb b/lib/binged/search/base.rb index 27375a0..4a09990 100644 --- a/lib/binged/search/base.rb +++ b/lib/binged/search/base.rb @@ -71,7 +71,11 @@ def perform response = Net::HTTP.start(url.hostname, url.port, :use_ssl => true) {|http| http.request(request) } - JSON.parse(response.body) + begin + JSON.parse(response.body) + rescue JSON::ParserError => e + raise Error, response.body.strip + end end # @yieldreturn [Hash] A result from a Bing query diff --git a/spec/binged/search/web_spec.rb b/spec/binged/search/web_spec.rb index a5ff420..cd51516 100644 --- a/spec/binged/search/web_spec.rb +++ b/spec/binged/search/web_spec.rb @@ -22,6 +22,19 @@ module Search @search.query['Web.FileType'].should == :pdf end + context 'errors' do + before(:each) do + stub_get("https://binged:binged@api.datamarket.azure.com:443/Data.ashx/Bing/Search/Web?%24format=JSON&%24skip=0&%24top=20&Query=%27ruby%27", 'bad_request.curl') + end + + it 'raises' do + @search.containing('ruby') + expect { + @search.fetch + }.to raise_error(Binged::Search::Error, 'The authorization type you provided is not supported. Only Basic and OAuth are supported') + end + end + context "fetching" do before(:each) do diff --git a/spec/fixtures/bad_request.curl b/spec/fixtures/bad_request.curl new file mode 100644 index 0000000..38e2a6a --- /dev/null +++ b/spec/fixtures/bad_request.curl @@ -0,0 +1,14 @@ +HTTP/1.1 401 The authorization type you provided is not supported. Only Basic and OAuth are supported +Server: Microsoft-IIS/7.5 +X-Content-Type-Options: nosniff +Access-Control-Allow-Origin: * +Access-Control-Allow-Credentials: false +Access-Control-Allow-Headers: Authorization +Access-Control-Allow-Methods: GET, POST, OPTIONS +Access-Control-Max-Age: 604800 +WWW-Authenticate: Basic Realm="" +X-Powered-By: ASP.NET +Date: Tue, 06 Nov 2012 09:00:44 GMT +Content-Length: 91 + +The authorization type you provided is not supported. Only Basic and OAuth are supported