From e5a6edde0619ca02dcb684d0e33249d8607333a3 Mon Sep 17 00:00:00 2001 From: James Brennan Date: Wed, 16 Sep 2015 13:29:30 -0700 Subject: [PATCH] Handle status codes as strings `Net::HTTPReponse#new` will pass through whatever you give it as the `code`, although a real `Net::HTTPResponse` status code is always a string. http://docs.ruby-lang.org/en/2.0.0/Net/HTTPResponse.html#attribute-i-code. --- lib/httparty/request.rb | 2 +- spec/support/stub_response.rb | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 99db68e..52698b8 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -282,7 +282,7 @@ module HTTParty unless options[:maintain_method_across_redirects] && options[:resend_on_redirect] self.http_method = Net::HTTP::Get end - elsif last_response.code != 307 && last_response.code != 308 + elsif last_response.code != '307' && last_response.code != '308' unless options[:maintain_method_across_redirects] self.http_method = Net::HTTP::Get end diff --git a/spec/support/stub_response.rb b/spec/support/stub_response.rb index 0d3aa29..c305b27 100644 --- a/spec/support/stub_response.rb +++ b/spec/support/stub_response.rb @@ -26,7 +26,8 @@ module HTTParty expect(HTTParty::Request).to receive(:new).and_return(http_request) end - def stub_response(body, code = 200) + def stub_response(body, code = '200') + code = code.to_s @request.options[:base_uri] ||= 'http://localhost' unless defined?(@http) && @http @http = Net::HTTP.new('localhost', 80) @@ -34,10 +35,10 @@ module HTTParty end # CODE_TO_OBJ currently missing 308 - if code.to_s == '308' + if code == '308' response = Net::HTTPRedirection.new("1.1", code, body) else - response = Net::HTTPResponse::CODE_TO_OBJ[code.to_s].new("1.1", code, body) + response = Net::HTTPResponse::CODE_TO_OBJ[code].new("1.1", code, body) end allow(response).to receive(:body).and_return(body)