diff --git a/lib/httparty.rb b/lib/httparty.rb index 4a7529cf..2aadf56b 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -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 diff --git a/lib/httparty/response.rb b/lib/httparty/response.rb index 178f09b9..4a93181d 100644 --- a/lib/httparty/response.rb +++ b/lib/httparty/response.rb @@ -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 diff --git a/spec/httparty/response_spec.rb b/spec/httparty/response_spec.rb index 30865a24..58945200 100644 --- a/spec/httparty/response_spec.rb +++ b/spec/httparty/response_spec.rb @@ -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