Skip to content

Commit

Permalink
Fix missing request URI on response auto inflate
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Jan 18, 2019
1 parent 57549c3 commit 76a9f69
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
23 changes: 20 additions & 3 deletions lib/http/features/auto_inflate.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# frozen_string_literal: true

require "set"

module HTTP
module Features
class AutoInflate < Feature
SUPPORTED_ENCODING = Set.new(%w[deflate gzip x-gzip]).freeze
private_constant :SUPPORTED_ENCODING

def wrap_response(response)
return response unless %w[deflate gzip x-gzip].include?(response.headers[:content_encoding])
Response.new(
return response unless supported_encoding?(response)

options = {
:status => response.status,
:version => response.version,
:headers => response.headers,
:proxy_headers => response.proxy_headers,
:connection => response.connection,
:body => stream_for(response.connection)
)
}

options[:uri] = response.uri if response.uri

Response.new(options)
end

def stream_for(connection)
Response::Body.new(Response::Inflater.new(connection))
end

private

def supported_encoding?(response)
content_encoding = response.headers.get(Headers::CONTENT_ENCODING).first
content_encoding && SUPPORTED_ENCODING.include?(content_encoding)
end

HTTP::Options.register_feature(:auto_inflate, self)
end
end
Expand Down
24 changes: 22 additions & 2 deletions spec/lib/http/features/auto_inflate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

RSpec.describe HTTP::Features::AutoInflate do
subject(:feature) { HTTP::Features::AutoInflate.new }

let(:connection) { double }
let(:headers) { {} }
let(:headers) { {} }

let(:response) do
HTTP::Response.new(
:version => "1.1",
Expand All @@ -13,7 +15,7 @@
)
end

describe "wrap_response" do
describe "#wrap_response" do
subject(:result) { feature.wrap_response(response) }

context "when there is no Content-Encoding header" do
Expand Down Expand Up @@ -61,5 +63,23 @@
expect(result.body).to be_instance_of HTTP::Response::Body
end
end

# TODO(ixti): We should refactor API to either make uri non-optional,
# or add reference to request into response object (better).
context "when response has uri" do
let(:response) do
HTTP::Response.new(
:version => "1.1",
:status => 200,
:headers => {:content_encoding => "gzip"},
:connection => connection,
:uri => "https://example.com"
)
end

it "preserves uri in wrapped response" do
expect(result.uri).to eq HTTP::URI.parse("https://example.com")
end
end
end
end

0 comments on commit 76a9f69

Please sign in to comment.