Skip to content

Commit

Permalink
Merge branch 'raise_on_regexpr' of https://github.com/ryanfox1985/htt…
Browse files Browse the repository at this point in the history
…party into ryanfox1985-raise_on_regexpr
  • Loading branch information
jnunemaker committed Mar 20, 2024
2 parents 1aa552f + c630a95 commit baa5e16
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def logger(logger, level = :info, format = :apache)
#
# class Foo
# include HTTParty
# raise_on [404, 500]
# raise_on [404, 500, '5[0-9]*']
# end
def raise_on(codes = [])
default_options[:raise_on] = *codes
Expand Down
2 changes: 1 addition & 1 deletion lib/httparty/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def method_missing(name, *args, &block)
end

def throw_exception
if @request.options[:raise_on] && @request.options[:raise_on].include?(code)
if @request.options[:raise_on].to_a.detect { |c| code.to_s.match(/#{c.to_s}/) }
::Kernel.raise ::HTTParty::ResponseError.new(@response), "Code #{code} - #{body}"
end
end
Expand Down
47 changes: 34 additions & 13 deletions spec/httparty/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,50 @@
expect(unparseable_response.http_version).to eq(@response_object.http_version)
end

context 'when raise_on is supplied' do
context 'test raise_on requests' do
let(:request) { HTTParty::Request.new(Net::HTTP::Get, '/', raise_on: [404]) }
let(:body) { 'Not Found' }
let(:response) { Net::HTTPNotFound.new('1.1', 404, body) }

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

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

context 'when raise_on is a number' do
let(:raise_on) { [404] }

context "and response's status code is in range" do
it 'throws exception' do
expect{ subject }.to raise_error(HTTParty::ResponseError, "Code 404 - #{body}")
end
end

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

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

context "and response's status code is not in range" do
subject { described_class.new(request, @response_object, @parsed_response) }
context 'when raise_on is a regexpr' do
let(:raise_on) { ['4[0-9]*'] }

context "and response's status code is in range" do
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
it 'does not throw exception' do
expect{ subject }.not_to raise_error
end
end
end
end
Expand Down

0 comments on commit baa5e16

Please sign in to comment.