Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow regexpr on raise errors. #778

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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