diff --git a/lib/http/features/auto_inflate.rb b/lib/http/features/auto_inflate.rb index e1d383ef..3f253341 100644 --- a/lib/http/features/auto_inflate.rb +++ b/lib/http/features/auto_inflate.rb @@ -17,15 +17,15 @@ def wrap_response(response) :headers => response.headers, :proxy_headers => response.proxy_headers, :connection => response.connection, - :body => stream_for(response.connection), + :body => stream_for(response.connection, :encoding => response.body.encoding), :request => response.request } Response.new(options) end - def stream_for(connection) - Response::Body.new(Response::Inflater.new(connection)) + def stream_for(connection, encoding: Encoding::BINARY) + Response::Body.new(Response::Inflater.new(connection), :encoding => encoding) end private diff --git a/lib/http/response/body.rb b/lib/http/response/body.rb index d4406a80..f41a6550 100644 --- a/lib/http/response/body.rb +++ b/lib/http/response/body.rb @@ -16,6 +16,11 @@ class Body # @return [HTTP::Connection] attr_reader :connection + # The charset encoding determined by the Response + # + # @return [Encoding] + attr_reader :encoding + def initialize(stream, encoding: Encoding::BINARY) @stream = stream @connection = stream.is_a?(Inflater) ? stream.connection : stream diff --git a/spec/lib/http/features/auto_inflate_spec.rb b/spec/lib/http/features/auto_inflate_spec.rb index f6c345d0..b87430d2 100644 --- a/spec/lib/http/features/auto_inflate_spec.rb +++ b/spec/lib/http/features/auto_inflate_spec.rb @@ -65,6 +65,14 @@ end end + context "for gzip Content-Encoding header with charset" do + let(:headers) { {:content_encoding => "gzip", :content_type => "text/html; charset=Shift_JIS"} } + + it "returns a HTTP::Response with the encoding from the response charset" do + expect(result.body.encoding).to be Encoding::Shift_JIS + 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 diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index f7a1bb21..3b97c577 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -456,6 +456,15 @@ def setsockopt(*args) expect(response.to_s).to eq("") end + + it "returns decoded body with the correct charset" do + encoding = Encoding::Shift_JIS + client = HTTP.use(:auto_inflate).headers("Accept-Encoding" => "gzip").encoding(encoding) + body = "もしもし!".encode(encoding, Encoding::UTF_8) + response = client.post("#{dummy.endpoint}/encoded-body", :body => body) + + expect(response.to_s).to eq("#{body}-gzipped") + end end context "with :normalize_uri" do