Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip auto-deflate when there is no body #529

Merged
merged 1 commit into from Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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