From 7c3133579a0e959de03b104767f203c6018845c7 Mon Sep 17 00:00:00 2001 From: Piotr Boniecki Date: Tue, 12 Feb 2019 21:20:26 +0100 Subject: [PATCH] Skip auto-deflate when there is no body Fixes #521 Also move auto-deflate related code from `http/client.rb` to `http/features/auto_deflate.rb` which seems to be move appropriate place. --- lib/http/client.rb | 8 -------- lib/http/features/auto_deflate.rb | 5 +++++ spec/lib/http/client_spec.rb | 14 +++++++++++++- 3 files changed, 18 insertions(+), 9 deletions(-) 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..48639c6b 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