Skip to content

Commit

Permalink
Merge pull request #455 from dccunha/raise_on_option
Browse files Browse the repository at this point in the history
Raise on some HTTP status codes
  • Loading branch information
greatuserongithub committed Jan 4, 2016
2 parents 880a2d5 + fc02683 commit d85e617
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ def logger(logger, level = :info, format = :apache)
default_options[:log_format] = format
end

# Raises HTTParty::ResponseError if response's code matches this statuses
#
# class Foo
# include HTTParty
# raise_on [404, 500]
# end
def raise_on(codes = [])
default_options[:raise_on] = *codes
end

# Allows setting http proxy information to be used
#
# class Foo
Expand Down
8 changes: 8 additions & 0 deletions lib/httparty/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def initialize(request, response, parsed_block, options = {})
logger = ::HTTParty::Logger.build(request.options[:logger], request.options[:log_level], request.options[:log_format])
logger.format(request, self)
end

throw_exception
end

def parsed_response
Expand Down Expand Up @@ -77,6 +79,12 @@ def method_missing(name, *args, &block)
super
end
end

def throw_exception
if @request.options[:raise_on] && @request.options[:raise_on].include?(code)
::Kernel.raise ::HTTParty::ResponseError.new(@response), "Code #{code} - #{body}"
end
end
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/httparty/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@
it "should set code as a Fixnum" do
expect(@response.code).to be_an_instance_of(Fixnum)
end

context 'when raise_on is supplied' do
let(:request) { HTTParty::Request.new(Net::HTTP::Get, '/', raise_on: [404]) }

context "and response's status code is in range" do
let(:body) { 'Not Found' }
let(:response) { Net::HTTPNotFound.new('1.1', 404, body) }

before do
allow(response).to receive(:body).and_return(body)
end

subject { described_class.new(request, response, @parsed_response) }

it 'throws exception' do
expect{ subject }.to raise_error(HTTParty::ResponseError, "Code 404 - #{body}")
end
end

context "and response's status code is not in range" do
subject { described_class.new(request, @response_object, @parsed_response) }

it 'does not throw exception' do
expect{ subject }.not_to raise_error(HTTParty::ResponseError)
end
end
end
end

it "returns response headers" do
Expand Down
16 changes: 16 additions & 0 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,22 @@ def self.parse uri
end
end

describe ".raise_on" do
context 'when parameters is an array' do
it 'sets raise_on option' do
@klass.raise_on [500, 404]
expect(@klass.default_options[:raise_on]).to contain_exactly(404, 500)
end
end

context 'when parameters is a fixnum' do
it 'sets raise_on option' do
@klass.raise_on 404
expect(@klass.default_options[:raise_on]).to contain_exactly(404)
end
end
end

describe "with explicit override of automatic redirect handling" do
before do
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', format: :xml, no_follow: true)
Expand Down

0 comments on commit d85e617

Please sign in to comment.