diff --git a/lib/http/client.rb b/lib/http/client.rb index f6f73e22..c069fa89 100644 --- a/lib/http/client.rb +++ b/lib/http/client.rb @@ -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)| diff --git a/lib/http/features/auto_inflate.rb b/lib/http/features/auto_inflate.rb index 38066708..8c404fec 100644 --- a/lib/http/features/auto_inflate.rb +++ b/lib/http/features/auto_inflate.rb @@ -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 diff --git a/lib/http/response.rb b/lib/http/response.rb index 600e6d4e..8b86df05 100644 --- a/lib/http/response.rb +++ b/lib/http/response.rb @@ -29,6 +29,9 @@ class Response # @return [URI, nil] attr_reader :uri + # @return [Request] + attr_reader :request + # @return [Hash] attr_reader :proxy_headers @@ -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) diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index 3c1491b7..3442b0bb 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -31,7 +31,8 @@ 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 @@ -39,7 +40,8 @@ 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 @@ -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) diff --git a/spec/lib/http/features/auto_inflate_spec.rb b/spec/lib/http/features/auto_inflate_spec.rb index c93e41d3..37211b3d 100644 --- a/spec/lib/http/features/auto_inflate_spec.rb +++ b/spec/lib/http/features/auto_inflate_spec.rb @@ -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 @@ -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 diff --git a/spec/lib/http/features/instrumentation_spec.rb b/spec/lib/http/features/instrumentation_spec.rb index a15d2e2c..bbe2e325 100644 --- a/spec/lib/http/features/instrumentation_spec.rb +++ b/spec/lib/http/features/instrumentation_spec.rb @@ -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 diff --git a/spec/lib/http/features/logging_spec.rb b/spec/lib/http/features/logging_spec.rb index fc19c875..cf7cbffe 100644 --- a/spec/lib/http/features/logging_spec.rb +++ b/spec/lib/http/features/logging_spec.rb @@ -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 diff --git a/spec/lib/http/redirector_spec.rb b/spec/lib/http/redirector_spec.rb index 99c9e9cb..ddf1da75 100644 --- a/spec/lib/http/redirector_spec.rb +++ b/spec/lib/http/redirector_spec.rb @@ -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 diff --git a/spec/lib/http/response_spec.rb b/spec/lib/http/response_spec.rb index 8297c0cd..3a562571 100644 --- a/spec/lib/http/response_spec.rb +++ b/spec/lib/http/response_spec.rb @@ -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 @@ -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