Skip to content

Commit 7c31335

Browse files
author
Piotr Boniecki
committed
Skip auto-deflate when there is no body
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.
1 parent f7ac94d commit 7c31335

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

lib/http/client.rb

-8
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,6 @@ def make_request_headers(opts)
160160
headers[Headers::COOKIE] = cookies
161161
end
162162

163-
if (auto_deflate = opts.feature(:auto_deflate))
164-
# We need to delete Content-Length header. It will be set automatically
165-
# by HTTP::Request::Writer
166-
headers.delete(Headers::CONTENT_LENGTH)
167-
168-
headers[Headers::CONTENT_ENCODING] = auto_deflate.method
169-
end
170-
171163
headers
172164
end
173165

lib/http/features/auto_deflate.rb

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def initialize(*)
2020

2121
def wrap_request(request)
2222
return request unless method
23+
return request if request.body.size.zero?
24+
25+
# We need to delete Content-Length header. It will be set automatically by HTTP::Request::Writer
26+
request.headers.delete(Headers::CONTENT_LENGTH)
27+
request.headers[Headers::CONTENT_ENCODING] = method
2328

2429
Request.new(
2530
:version => request.version,

spec/lib/http/client_spec.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def simple_response(body, status = 200)
234234

235235
context "when :auto_deflate was specified" do
236236
let(:headers) { {"Content-Length" => "12"} }
237-
let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}} }
237+
let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}}, body: "foo" }
238238

239239
it "deletes Content-Length header" do
240240
expect(client).to receive(:perform) do |req, _|
@@ -251,6 +251,18 @@ def simple_response(body, status = 200)
251251

252252
client.request(:get, "http://example.com/")
253253
end
254+
255+
context "and there is no body" do
256+
let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}}}
257+
258+
it "doesn't set Content-Encoding header" do
259+
expect(client).to receive(:perform) do |req, _|
260+
expect(req.headers).not_to include "Content-Encoding"
261+
end
262+
263+
client.request(:get, "http://example.com/")
264+
end
265+
end
254266
end
255267
end
256268

0 commit comments

Comments
 (0)