Skip to content

Commit

Permalink
Merge branch 'response_request_v411' into deploy
Browse files Browse the repository at this point in the history
This mirrors httprb#546, which has not been
merged yet.
  • Loading branch information
joshuaflanagan committed Apr 24, 2019
2 parents ab289d3 + e95593d commit b5a0622
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/http/client.rb
Expand Up @@ -82,7 +82,7 @@ def perform(req, options)
:proxy_headers => @connection.proxy_response_headers,
:connection => @connection,
:encoding => options.encoding,
:uri => req.uri
:request => req
)

res = options.features.inject(res) do |response, (_name, feature)|
Expand Down
3 changes: 2 additions & 1 deletion lib/http/features/auto_inflate.rb
Expand Up @@ -17,7 +17,8 @@ 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),
:request => response.request
}

options[:uri] = response.uri if response.uri
Expand Down
4 changes: 4 additions & 0 deletions lib/http/request.rb
Expand Up @@ -199,6 +199,10 @@ def inspect
"#<#{self.class}/#{@version} #{verb.to_s.upcase} #{uri}>"
end

def self.example
new(:verb => :get, :uri => "http://example.com")
end

private

# @!attribute [r] host
Expand Down
6 changes: 5 additions & 1 deletion lib/http/response.rb
Expand Up @@ -29,6 +29,9 @@ class Response
# @return [URI, nil]
attr_reader :uri

# @return [Request]
attr_reader :request

# @return [Hash]
attr_reader :proxy_headers

Expand All @@ -44,7 +47,8 @@ class Response
# @option opts [String] :uri
def initialize(opts)
@version = opts.fetch(:version)
@uri = HTTP::URI.parse(opts.fetch(:uri)) if opts.include? :uri
@request = opts.fetch(:request)
@uri = HTTP::URI.parse(opts[:uri] || @request.uri)
@status = HTTP::Response::Status.new(opts.fetch(:status))
@headers = HTTP::Headers.coerce(opts[:headers] || {})
@proxy_headers = HTTP::Headers.coerce(opts[:proxy_headers] || {})
Expand Down
14 changes: 12 additions & 2 deletions spec/lib/http/client_spec.rb
Expand Up @@ -31,15 +31,17 @@ def redirect_response(location, status = 302)
:status => status,
:version => "1.1",
:headers => {"Location" => location},
:body => ""
:body => "",
:request => HTTP::Request.example
)
end

def simple_response(body, status = 200)
HTTP::Response.new(
:status => status,
:version => "1.1",
:body => body
:body => body,
:request => HTTP::Request.example
)
end

Expand Down Expand Up @@ -316,6 +318,14 @@ def simple_response(body, status = 200)
client.get(dummy.endpoint).to_s
end

it "provides access to the Request from the Response" do
unique_value = "20190424"
response = client.headers("X-Value" => unique_value).get(dummy.endpoint)

expect(response.request).to be_a(HTTP::Request)
expect(response.request.headers["X-Value"]).to eq(unique_value)
end

context "with HEAD request" do
it "does not iterates through body" do
expect_any_instance_of(HTTP::Connection).to_not receive(:readpartial)
Expand Down
6 changes: 4 additions & 2 deletions spec/lib/http/features/auto_inflate_spec.rb
Expand Up @@ -11,7 +11,8 @@
:version => "1.1",
:status => 200,
:headers => headers,
:connection => connection
:connection => connection,
:request => HTTP::Request.example
)
end

Expand Down Expand Up @@ -73,7 +74,8 @@
:status => 200,
:headers => {:content_encoding => "gzip"},
:connection => connection,
:uri => "https://example.com"
:uri => "https://example.com",
:request => HTTP::Request.example
)
end

Expand Down
3 changes: 2 additions & 1 deletion spec/lib/http/features/instrumentation_spec.rb
Expand Up @@ -28,7 +28,8 @@
:uri => "https://example.com",
:status => 200,
:headers => {:content_type => "application/json"},
:body => '{"success": true}'
:body => '{"success": true}',
:request => HTTP::Request.example
)
end

Expand Down
3 changes: 2 additions & 1 deletion spec/lib/http/features/logging_spec.rb
Expand Up @@ -47,7 +47,8 @@
:uri => "https://example.com",
:status => 200,
:headers => {:content_type => "application/json"},
:body => '{"success": true}'
:body => '{"success": true}',
:request => HTTP::Request.example
)
end

Expand Down
3 changes: 2 additions & 1 deletion spec/lib/http/redirector_spec.rb
Expand Up @@ -6,7 +6,8 @@ def simple_response(status, body = "", headers = {})
:status => status,
:version => "1.1",
:headers => headers,
:body => body
:body => body,
:request => HTTP::Request.example
)
end

Expand Down
6 changes: 4 additions & 2 deletions spec/lib/http/response_spec.rb
Expand Up @@ -11,7 +11,8 @@
:version => "1.1",
:headers => headers,
:body => body,
:uri => uri
:uri => uri,
:request => HTTP::Request.example
)
end

Expand Down Expand Up @@ -165,7 +166,8 @@
HTTP::Response.new(
:version => "1.1",
:status => 200,
:connection => connection
:connection => connection,
:request => HTTP::Request.example
)
end

Expand Down

0 comments on commit b5a0622

Please sign in to comment.