Skip to content

Commit

Permalink
Raise a Binged::Search::Error in the bad auth case, rather than JSON:…
Browse files Browse the repository at this point in the history
…:ParserError.
  • Loading branch information
Empact committed Nov 6, 2012
1 parent 2964ebb commit 5efbcfb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions 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

Expand Down
6 changes: 5 additions & 1 deletion lib/binged/search/base.rb
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/binged/search/web_spec.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions 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

0 comments on commit 5efbcfb

Please sign in to comment.