Skip to content

Commit edd8cc5

Browse files
authored
Fix Faraday::Response#to_hash when request is not finished yet (#1639)
1 parent 0e008c5 commit edd8cc5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/faraday/response.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def body
3333
finished? ? env.body : nil
3434
end
3535

36+
def url
37+
finished? ? env.url : nil
38+
end
39+
3640
def finished?
3741
!!env
3842
end
@@ -60,9 +64,9 @@ def success?
6064

6165
def to_hash
6266
{
63-
status: env.status, body: env.body,
64-
response_headers: env.response_headers,
65-
url: env.url
67+
status: status, body: body,
68+
response_headers: headers,
69+
url: url
6670
}
6771
end
6872

spec/faraday/response_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
it { expect(subject.success?).to be_falsey }
1414
it { expect(subject.status).to eq(404) }
1515
it { expect(subject.body).to eq('yikes') }
16+
it { expect(subject.url).to eq(URI('https://lostisland.github.io/faraday')) }
1617
it { expect(subject.headers['Content-Type']).to eq('text/plain') }
1718
it { expect(subject['content-type']).to eq('text/plain') }
1819

@@ -31,6 +32,12 @@
3132
it { expect(hash[:response_headers]).to eq(subject.headers) }
3233
it { expect(hash[:body]).to eq(subject.body) }
3334
it { expect(hash[:url]).to eq(subject.env.url) }
35+
36+
context 'when response is not finished' do
37+
subject { Faraday::Response.new.to_hash }
38+
39+
it { is_expected.to eq({ status: nil, body: nil, response_headers: {}, url: nil }) }
40+
end
3441
end
3542

3643
describe 'marshal serialization support' do

0 commit comments

Comments
 (0)