Skip to content

Commit

Permalink
Skip auto-deflate when there is no body
Browse files Browse the repository at this point in the history
Fixes httprb#521

Also move auto-deflate related code from `http/client.rb` to `http/features/auto_deflate.rb`
which seems to be move appropriate place.
  • Loading branch information
Piotr Boniecki committed Feb 12, 2019
1 parent f7ac94d commit 7c31335
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
8 changes: 0 additions & 8 deletions lib/http/client.rb
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/http/features/auto_deflate.rb
Expand Up @@ -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,
Expand Down
14 changes: 13 additions & 1 deletion spec/lib/http/client_spec.rb
Expand Up @@ -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, _|
Expand All @@ -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

Expand Down

0 comments on commit 7c31335

Please sign in to comment.