diff --git a/lib/http/client.rb b/lib/http/client.rb index 0ca9cd0e..5b9aa4fa 100644 --- a/lib/http/client.rb +++ b/lib/http/client.rb @@ -160,14 +160,6 @@ def make_request_headers(opts) headers[Headers::COOKIE] = cookies end - if (auto_deflate = opts.feature(:auto_deflate)) - # We need to delete Content-Length header. It will be set automatically - # by HTTP::Request::Writer - headers.delete(Headers::CONTENT_LENGTH) - - headers[Headers::CONTENT_ENCODING] = auto_deflate.method - end - headers end diff --git a/lib/http/features/auto_deflate.rb b/lib/http/features/auto_deflate.rb index 108aca46..2a2b5300 100644 --- a/lib/http/features/auto_deflate.rb +++ b/lib/http/features/auto_deflate.rb @@ -20,6 +20,11 @@ def initialize(*) def wrap_request(request) return request unless method + return request if request.body.size.zero? + + # We need to delete Content-Length header. It will be set automatically by HTTP::Request::Writer + request.headers.delete(Headers::CONTENT_LENGTH) + request.headers[Headers::CONTENT_ENCODING] = method Request.new( :version => request.version, diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index 168301d5..bbe8d836 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -234,7 +234,7 @@ def simple_response(body, status = 200) context "when :auto_deflate was specified" do let(:headers) { {"Content-Length" => "12"} } - let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}} } + let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}}, :body => "foo" } it "deletes Content-Length header" do expect(client).to receive(:perform) do |req, _| @@ -251,6 +251,18 @@ def simple_response(body, status = 200) client.request(:get, "http://example.com/") end + + context "and there is no body" do + let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}}} + + it "doesn't set Content-Encoding header" do + expect(client).to receive(:perform) do |req, _| + expect(req.headers).not_to include "Content-Encoding" + end + + client.request(:get, "http://example.com/") + end + end end end