Skip to content

Commit

Permalink
Create deflated body right before streaming starts
Browse files Browse the repository at this point in the history
This makes it easier for 3rd-party libraries to inspect and log the
request body, because they can rely on HTTP::Request#body always
returning a HTTP::Request::Body object, instead of a DeflatedBody object
when :auto_deflate is enabled.

We also define HTTP::Request::Body#== for easier testing.
  • Loading branch information
janko committed Nov 11, 2017
1 parent 2ad324f commit 9a28682
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def initialize(opts)
raise(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme)

@proxy = opts[:proxy] || {}
@body = request_body(opts[:body], opts)
@body = Request::Body.new(opts[:body])
@deflate = opts[:auto_deflate]
@version = opts[:version] || "1.1"
@headers = HTTP::Headers.coerce(opts[:headers] || {})

Expand All @@ -100,18 +101,20 @@ def redirect(uri, verb = @verb)
headers.delete(Headers::HOST)

self.class.new(
:verb => verb,
:uri => @uri.join(uri),
:headers => headers,
:proxy => proxy,
:body => body,
:version => version
:verb => verb,
:uri => @uri.join(uri),
:headers => headers,
:proxy => proxy,
:body => body.source,
:auto_deflate => @deflate,
:version => version
)
end

# Stream the request to a socket
def stream(socket)
include_proxy_headers if using_proxy? && !@uri.https?
body = @deflate ? @deflate.deflated_body(self.body) : self.body
Request::Writer.new(socket, body, headers, headline).stream
end

Expand Down Expand Up @@ -186,13 +189,6 @@ def socket_port

private

# Transforms body to an object suitable for streaming.
def request_body(body, opts)
body = Request::Body.new(body) unless body.is_a?(Request::Body)
body = opts[:auto_deflate].deflated_body(body) if opts[:auto_deflate]
body
end

# @!attribute [r] host
# @return [String]
def_delegator :@uri, :host
Expand Down
5 changes: 5 additions & 0 deletions lib/http/request/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def each(&block)
end
end

# Request bodies are equivalent when they have the same source.
def ==(other)
self.class == other.class && self.source == other.source # rubocop:disable Style/RedundantSelf
end

private

def validate_source_type!
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/http/request/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,33 @@
end
end
end

describe "#==" do
context "when sources are equivalent" do
let(:body1) { HTTP::Request::Body.new("content") }
let(:body2) { HTTP::Request::Body.new("content") }

it "returns true" do
expect(body1).to eq body2
end
end

context "when sources are not equivalent" do
let(:body1) { HTTP::Request::Body.new("content") }
let(:body2) { HTTP::Request::Body.new(nil) }

it "returns false" do
expect(body1).not_to eq body2
end
end

context "when objects are not of the same class" do
let(:body1) { HTTP::Request::Body.new("content") }
let(:body2) { "content" }

it "returns false" do
expect(body1).not_to eq body2
end
end
end
end

0 comments on commit 9a28682

Please sign in to comment.