Skip to content

Commit

Permalink
Access the HTTP::Request from the HTTP::Response
Browse files Browse the repository at this point in the history
This fixes #463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
  • Loading branch information
joshuaflanagan committed Apr 24, 2019
1 parent f37a10e commit 99ac40f
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/http/client.rb
Expand Up @@ -82,7 +82,8 @@ def perform(req, options)
:proxy_headers => @connection.proxy_response_headers,
:connection => @connection,
:encoding => options.encoding,
:uri => req.uri
: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/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 @@ -48,6 +51,7 @@ def initialize(opts)
@status = HTTP::Response::Status.new(opts.fetch(:status))
@headers = HTTP::Headers.coerce(opts[:headers] || {})
@proxy_headers = HTTP::Headers.coerce(opts[:proxy_headers] || {})
@request = opts.fetch(:request)

if opts.include?(:body)
@body = opts.fetch(:body)
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.new(verb: :get, uri: "http://www.example.com")
)
end

def simple_response(body, status = 200)
HTTP::Response.new(
:status => status,
:version => "1.1",
:body => body
:body => body,
:request => HTTP::Request.new(verb: :get, uri: "http://www.example.com")
)
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.new(verb: :get, uri: "http://www.example.com")
)
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.new(verb: :get, uri: "http://www.example.com")
)
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.new(verb: :get, uri: "http://www.example.com")
)
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.new(verb: :get, uri: "http://www.example.com")
)
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.new(verb: :get, uri: "http://www.example.com")
)
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.new(verb: :get, uri: "http://www.example.com")
)
end

Expand Down Expand Up @@ -152,7 +153,8 @@
HTTP::Response.new(
:version => "1.1",
:status => 200,
:connection => connection
:connection => connection,
:request => HTTP::Request.new(verb: :get, uri: "http://www.example.com")
)
end

Expand Down

0 comments on commit 99ac40f

Please sign in to comment.