Skip to content

Commit b735637

Browse files
committed
Add HTTP::Response#connection
1 parent f3a9798 commit b735637

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

lib/http/response.rb

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def initialize(opts)
7171
# (see HTTP::Response::Body#readpartial)
7272
def_delegator :body, :readpartial
7373

74+
# @!method connection
75+
# (see HTTP::Response::Body#connection)
76+
def_delegator :body, :connection
77+
7478
# Returns an Array ala Rack: `[status, headers, body]`
7579
#
7680
# @return [Array(Fixnum, Hash, String)]

lib/http/response/body.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ class Body
1010
include Enumerable
1111
def_delegator :to_s, :empty?
1212

13-
def initialize(client, encoding = Encoding::BINARY)
14-
@client = client
15-
@streaming = nil
16-
@contents = nil
17-
@encoding = encoding
13+
# The connection object used to make the corresponding request.
14+
#
15+
# @return [HTTP::Connection]
16+
attr_reader :connection
17+
18+
def initialize(connection, encoding = Encoding::BINARY)
19+
@connection = connection
20+
@streaming = nil
21+
@contents = nil
22+
@encoding = encoding
1823
end
1924

2025
# (see HTTP::Client#readpartial)
2126
def readpartial(*args)
2227
stream!
23-
@client.readpartial(*args)
28+
@connection.readpartial(*args)
2429
end
2530

2631
# Iterate over the body, allowing it to be enumerable
@@ -47,7 +52,7 @@ def to_s
4752
@streaming = false
4853
@contents = String.new("").force_encoding(encoding)
4954

50-
while (chunk = @client.readpartial)
55+
while (chunk = @connection.readpartial)
5156
@contents << chunk.force_encoding(encoding)
5257
end
5358
rescue

spec/lib/http/response/body_spec.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# frozen_string_literal: true
22
RSpec.describe HTTP::Response::Body do
3-
let(:client) { double(:sequence_id => 0) }
4-
let(:chunks) { [String.new("Hello, "), String.new("World!")] }
3+
let(:connection) { double(:sequence_id => 0) }
4+
let(:chunks) { [String.new("Hello, "), String.new("World!")] }
55

6-
before { allow(client).to receive(:readpartial) { chunks.shift } }
6+
before { allow(connection).to receive(:readpartial) { chunks.shift } }
77

8-
subject(:body) { described_class.new(client, Encoding::UTF_8) }
8+
subject(:body) { described_class.new(connection, Encoding::UTF_8) }
99

1010
it "streams bodies from responses" do
1111
expect(subject.to_s).to eq("Hello, World!")
@@ -21,8 +21,8 @@
2121

2222
describe "#readpartial" do
2323
context "with size given" do
24-
it "passes value to underlying client" do
25-
expect(client).to receive(:readpartial).with(42)
24+
it "passes value to underlying connection" do
25+
expect(connection).to receive(:readpartial).with(42)
2626
body.readpartial 42
2727
end
2828
end
@@ -32,8 +32,8 @@
3232
expect { body.readpartial }.to_not raise_error
3333
end
3434

35-
it "calls underlying client readpartial without specific size" do
36-
expect(client).to receive(:readpartial).with no_args
35+
it "calls underlying connection readpartial without specific size" do
36+
expect(connection).to receive(:readpartial).with no_args
3737
body.readpartial
3838
end
3939
end

spec/lib/http/response_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,20 @@
157157
expect(jar.count { |c| "c" == c.name }).to eq 0
158158
end
159159
end
160+
161+
describe "#connection" do
162+
let(:connection) { double }
163+
164+
subject(:response) do
165+
HTTP::Response.new(
166+
:version => "1.1",
167+
:status => 200,
168+
:connection => connection
169+
)
170+
end
171+
172+
it "returns the connection object used to instantiate the response" do
173+
expect(response.connection).to eq connection
174+
end
175+
end
160176
end

0 commit comments

Comments
 (0)