1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Remove decompression logic, as Ruby's Net::HTTP does this anyways

This commit is contained in:
Eric Urban 2017-02-11 07:44:27 -06:00
parent c46663d73f
commit 6f6bf6b726
2 changed files with 25 additions and 66 deletions

View file

@ -137,7 +137,6 @@ module HTTParty
end end
end end
handle_deflation unless http_method == Net::HTTP::Head
handle_host_redirection if response_redirects? handle_host_redirection if response_redirects?
handle_response(chunked_body, &block) handle_response(chunked_body, &block)
end end
@ -184,7 +183,17 @@ module HTTParty
@raw_request = http_method.new(request_uri(uri)) @raw_request = http_method.new(request_uri(uri))
@raw_request.body = body if body @raw_request.body = body if body
@raw_request.body_stream = options[:body_stream] if options[:body_stream] @raw_request.body_stream = options[:body_stream] if options[:body_stream]
@raw_request.initialize_http_header(options[:headers].to_hash) if options[:headers].respond_to?(:to_hash) if options[:headers].respond_to?(:to_hash)
headers_hash = options[:headers].to_hash
@raw_request.initialize_http_header(headers_hash)
# If the caller specified a header of 'Accept-Encoding', assume they want to
# deal with encoding of content. Disable the internal logic in Net:HTTP
# that handles encoding, if the platform supports it.
if @raw_request.respond_to?(:decode_content) && (headers_hash.key?('Accept-Encoding') || headers_hash.key?('accept-encoding'))
# Using the '[]=' sets decode_content to false
@raw_request['accept-encoding'] = @raw_request['accept-encoding']
end
end
@raw_request.basic_auth(username, password) if options[:basic_auth] && send_authorization_header? @raw_request.basic_auth(username, password) if options[:basic_auth] && send_authorization_header?
setup_digest_auth if options[:digest_auth] setup_digest_auth if options[:digest_auth]
end end
@ -308,22 +317,6 @@ module HTTParty
end end
end end
# Inspired by Ruby 1.9
def handle_deflation
return if response_redirects?
return if last_response.body.nil?
case last_response["content-encoding"]
when "gzip", "x-gzip"
body_io = StringIO.new(last_response.body)
last_response.body.replace Zlib::GzipReader.new(body_io).read
last_response.delete('content-encoding')
when "deflate"
last_response.body.replace Zlib::Inflate.inflate(last_response.body)
last_response.delete('content-encoding')
end
end
def handle_host_redirection def handle_host_redirection
check_duplicate_location_header check_duplicate_location_header
redirect_path = options[:uri_adapter].parse last_response['location'] redirect_path = options[:uri_adapter].parse last_response['location']

View file

@ -1059,54 +1059,6 @@ RSpec.describe HTTParty::Request do
end end
end end
describe "#handle_deflation" do
context "context-encoding" do
before do
@request.options[:format] = :html
@last_response = double
allow(@last_response).to receive(:body).and_return('')
end
it "should inflate the gzipped body with content-encoding: gzip" do
allow(@last_response).to receive(:[]).with("content-encoding").and_return("gzip")
allow(@request).to receive(:last_response).and_return(@last_response)
expect(Zlib::GzipReader).to receive(:new).and_return(StringIO.new(''))
expect(@request.last_response).to receive(:delete).with('content-encoding')
@request.send(:handle_deflation)
end
it "should inflate the gzipped body with content-encoding: x-gzip" do
allow(@last_response).to receive(:[]).with("content-encoding").and_return("x-gzip")
allow(@request).to receive(:last_response).and_return(@last_response)
expect(Zlib::GzipReader).to receive(:new).and_return(StringIO.new(''))
expect(@request.last_response).to receive(:delete).with('content-encoding')
@request.send(:handle_deflation)
end
it "should inflate the deflated body" do
allow(@last_response).to receive(:[]).with("content-encoding").and_return("deflate")
allow(@request).to receive(:last_response).and_return(@last_response)
expect(Zlib::Inflate).to receive(:inflate).and_return('')
expect(@request.last_response).to receive(:delete).with('content-encoding')
@request.send(:handle_deflation)
end
it "should not inflate a redirected response with content-encoding: gzip" do
allow(@last_response).to receive(:[]).with("content-encoding").and_return("gzip")
allow(@request).to receive(:last_response).and_return(@last_response)
allow(@request).to receive(:response_redirects?).and_return(true)
@request.send(:handle_deflation)
end
it "should not inflate a redirected response with content-encoding: deflate" do
allow(@last_response).to receive(:[]).with("content-encoding").and_return("deflate")
allow(@request).to receive(:last_response).and_return(@last_response)
allow(@request).to receive(:response_redirects?).and_return(true)
@request.send(:handle_deflation)
end
end
end
describe "#send_authorization_header?" do describe "#send_authorization_header?" do
context "basic_auth" do context "basic_auth" do
before do before do
@ -1202,4 +1154,18 @@ RSpec.describe HTTParty::Request do
}.to raise_error(HTTParty::RedirectionTooDeep, 'HTTP redirects too deep') }.to raise_error(HTTParty::RedirectionTooDeep, 'HTTP redirects too deep')
end end
end end
context 'with Accept-Encoding header' do
it 'should disable content decoding if present' do
request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', headers:{'Accept-Encoding' => 'custom'})
request.send(:setup_raw_request)
expect(request.instance_variable_get(:@raw_request).decode_content).to eq(false)
end
it 'should disable content decoding if present' do
request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1')
request.send(:setup_raw_request)
expect(request.instance_variable_get(:@raw_request).decode_content).to eq(true)
end
end
end end