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

Fix support for empty bodies having Content-Encoding Gzip #192

Merged
merged 2 commits into from
Aug 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/faraday_middleware/gzip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ def self.supported_encodings
def call(env)
env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS
@app.call(env).on_complete do |response_env|
case response_env[:response_headers][CONTENT_ENCODING]
when 'gzip'
reset_body(response_env, &method(:uncompress_gzip))
when 'deflate'
reset_body(response_env, &method(:inflate))
when 'br'
reset_body(response_env, &method(:brotli_inflate))
if response_env[:body].empty?
reset_body(response_env, &method(:raw_body))
else
case response_env[:response_headers][CONTENT_ENCODING]
when 'gzip'
reset_body(response_env, &method(:uncompress_gzip))
when 'deflate'
reset_body(response_env, &method(:inflate))
when 'br'
reset_body(response_env, &method(:brotli_inflate))
end
end
end
end
Expand Down Expand Up @@ -81,5 +85,9 @@ def inflate(body)
def brotli_inflate(body)
Brotli.inflate(body)
end

def raw_body(body)
body
end
end
end
16 changes: 16 additions & 0 deletions spec/unit/gzip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
let(:brotlied_body) {
Brotli.deflate(uncompressed_body)
}
let(:empty_body) {
""
}

shared_examples 'compressed response' do
it 'uncompresses the body' do
Expand Down Expand Up @@ -92,6 +95,19 @@
it_behaves_like 'compressed response'
end unless jruby?

context 'empty response' do
let(:body) { empty_body }
let(:headers) { {'Content-Encoding' => 'gzip', 'Content-Length' => body.length } }

it 'sets the Content-Length' do
expect(process(body).headers['Content-Length']).to eq(empty_body.length)
end

it 'removes the Content-Encoding' do
expect(process(body).headers['Content-Encoding']).to be_nil
end
end

context 'identity response' do
let(:body) { uncompressed_body }

Expand Down