Skip to content

Commit 5bb01ce

Browse files
Access the HTTP::Request from the HTTP::Response
This fixes #463 This will greatly enhance the abilities of pluggable `Features` by being able to reference the Request that initiated a Response.
1 parent 6240672 commit 5bb01ce

10 files changed

+38
-11
lines changed

lib/http/client.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def perform(req, options)
8282
:proxy_headers => @connection.proxy_response_headers,
8383
:connection => @connection,
8484
:encoding => options.encoding,
85-
:uri => req.uri
85+
:uri => req.uri,
86+
:request => req
8687
)
8788

8889
res = options.features.inject(res) do |response, (_name, feature)|

lib/http/features/auto_inflate.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def wrap_response(response)
1717
:headers => response.headers,
1818
:proxy_headers => response.proxy_headers,
1919
:connection => response.connection,
20-
:body => stream_for(response.connection)
20+
:body => stream_for(response.connection),
21+
:request => response.request
2122
}
2223

2324
options[:uri] = response.uri if response.uri

lib/http/request.rb

+4
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ def inspect
199199
"#<#{self.class}/#{@version} #{verb.to_s.upcase} #{uri}>"
200200
end
201201

202+
def self.example
203+
new(:verb => :get, :uri => "http://example.com")
204+
end
205+
202206
private
203207

204208
# @!attribute [r] host

lib/http/response.rb

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Response
2929
# @return [URI, nil]
3030
attr_reader :uri
3131

32+
# @return [Request]
33+
attr_reader :request
34+
3235
# @return [Hash]
3336
attr_reader :proxy_headers
3437

@@ -48,6 +51,7 @@ def initialize(opts)
4851
@status = HTTP::Response::Status.new(opts.fetch(:status))
4952
@headers = HTTP::Headers.coerce(opts[:headers] || {})
5053
@proxy_headers = HTTP::Headers.coerce(opts[:proxy_headers] || {})
54+
@request = opts.fetch(:request)
5155

5256
if opts.include?(:body)
5357
@body = opts.fetch(:body)

spec/lib/http/client_spec.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ def redirect_response(location, status = 302)
3131
:status => status,
3232
:version => "1.1",
3333
:headers => {"Location" => location},
34-
:body => ""
34+
:body => "",
35+
:request => HTTP::Request.example
3536
)
3637
end
3738

3839
def simple_response(body, status = 200)
3940
HTTP::Response.new(
4041
:status => status,
4142
:version => "1.1",
42-
:body => body
43+
:body => body,
44+
:request => HTTP::Request.example
4345
)
4446
end
4547

@@ -316,6 +318,14 @@ def simple_response(body, status = 200)
316318
client.get(dummy.endpoint).to_s
317319
end
318320

321+
it "provides access to the Request from the Response" do
322+
unique_value = "20190424"
323+
response = client.headers("X-Value" => unique_value).get(dummy.endpoint)
324+
325+
expect(response.request).to be_a(HTTP::Request)
326+
expect(response.request.headers["X-Value"]).to eq(unique_value)
327+
end
328+
319329
context "with HEAD request" do
320330
it "does not iterates through body" do
321331
expect_any_instance_of(HTTP::Connection).to_not receive(:readpartial)

spec/lib/http/features/auto_inflate_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
:version => "1.1",
1212
:status => 200,
1313
:headers => headers,
14-
:connection => connection
14+
:connection => connection,
15+
:request => HTTP::Request.example
1516
)
1617
end
1718

@@ -73,7 +74,8 @@
7374
:status => 200,
7475
:headers => {:content_encoding => "gzip"},
7576
:connection => connection,
76-
:uri => "https://example.com"
77+
:uri => "https://example.com",
78+
:request => HTTP::Request.example
7779
)
7880
end
7981

spec/lib/http/features/instrumentation_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
:uri => "https://example.com",
2929
:status => 200,
3030
:headers => {:content_type => "application/json"},
31-
:body => '{"success": true}'
31+
:body => '{"success": true}',
32+
:request => HTTP::Request.example
3233
)
3334
end
3435

spec/lib/http/features/logging_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
:uri => "https://example.com",
4848
:status => 200,
4949
:headers => {:content_type => "application/json"},
50-
:body => '{"success": true}'
50+
:body => '{"success": true}',
51+
:request => HTTP::Request.example
5152
)
5253
end
5354

spec/lib/http/redirector_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ def simple_response(status, body = "", headers = {})
66
:status => status,
77
:version => "1.1",
88
:headers => headers,
9-
:body => body
9+
:body => body,
10+
:request => HTTP::Request.example
1011
)
1112
end
1213

spec/lib/http/response_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
:version => "1.1",
1212
:headers => headers,
1313
:body => body,
14-
:uri => uri
14+
:uri => uri,
15+
:request => HTTP::Request.example
1516
)
1617
end
1718

@@ -152,7 +153,8 @@
152153
HTTP::Response.new(
153154
:version => "1.1",
154155
:status => 200,
155-
:connection => connection
156+
:connection => connection,
157+
:request => HTTP::Request.example
156158
)
157159
end
158160

0 commit comments

Comments
 (0)