Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access the HTTP::Request from the HTTP::Response #546

Merged
merged 2 commits into from Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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.new(:verb => :get, :uri => "http://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://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://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 => "https://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 => "https://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 => "https://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://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://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://example.com")
)
end

Expand Down