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

Add brotli support #134

Merged
merged 1 commit into from
Aug 17, 2017
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
source 'https://rubygems.org'

gem 'brotli', '>= 0.1.8'
gem 'hashie', '>= 1.2'
gem 'jruby-openssl', :platforms => :jruby
gem 'json', :platforms => [:jruby, :rbx, :ruby_18]
Expand Down
9 changes: 8 additions & 1 deletion lib/faraday_middleware/gzip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ module FaradayMiddleware
# - em_http
class Gzip < Faraday::Middleware
dependency 'zlib'
dependency 'brotli'

ACCEPT_ENCODING = 'Accept-Encoding'.freeze
CONTENT_ENCODING = 'Content-Encoding'.freeze
CONTENT_LENGTH = 'Content-Length'.freeze
SUPPORTED_ENCODINGS = 'gzip,deflate'.freeze
SUPPORTED_ENCODINGS = 'gzip,deflate,br'.freeze
RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)

def call(env)
Expand All @@ -27,6 +28,8 @@ def call(env)
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
Expand Down Expand Up @@ -60,5 +63,9 @@ def inflate(body)
inflate.close
end
end

def brotli_inflate(body)
Brotli.inflate(body)
end
end
end
12 changes: 11 additions & 1 deletion spec/unit/gzip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
context 'request' do
it 'sets the Accept-Encoding request header' do
env = process('').env
expect(env[:request_headers][:accept_encoding]).to eq('gzip,deflate')
expect(env[:request_headers][:accept_encoding]).to eq('gzip,deflate,br')
end

it 'doesnt overwrite existing Accept-Encoding request header' do
Expand Down Expand Up @@ -45,6 +45,9 @@
z.close
compressed_body
}
let(:brotlied_body) {
Brotli.deflate(uncompressed_body)
}

shared_examples 'compressed response' do
it 'uncompresses the body' do
Expand Down Expand Up @@ -81,6 +84,13 @@
it_behaves_like 'compressed response'
end

context 'brotlied response' do
let(:body) { brotlied_body }
let(:headers) { {'Content-Encoding' => 'br', 'Content-Length' => body.length } }

it_behaves_like 'compressed response'
end

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

Expand Down