diff --git a/lib/faraday/response.rb b/lib/faraday/response.rb index d1fa9320..738c8498 100644 --- a/lib/faraday/response.rb +++ b/lib/faraday/response.rb @@ -33,6 +33,10 @@ def body finished? ? env.body : nil end + def url + finished? ? env.url : nil + end + def finished? !!env end @@ -60,9 +64,9 @@ def success? def to_hash { - status: env.status, body: env.body, - response_headers: env.response_headers, - url: env.url + status: status, body: body, + response_headers: headers, + url: url } end diff --git a/spec/faraday/response_spec.rb b/spec/faraday/response_spec.rb index e3e2c237..2050da00 100644 --- a/spec/faraday/response_spec.rb +++ b/spec/faraday/response_spec.rb @@ -13,6 +13,7 @@ it { expect(subject.success?).to be_falsey } it { expect(subject.status).to eq(404) } it { expect(subject.body).to eq('yikes') } + it { expect(subject.url).to eq(URI('https://lostisland.github.io/faraday')) } it { expect(subject.headers['Content-Type']).to eq('text/plain') } it { expect(subject['content-type']).to eq('text/plain') } @@ -31,6 +32,12 @@ it { expect(hash[:response_headers]).to eq(subject.headers) } it { expect(hash[:body]).to eq(subject.body) } it { expect(hash[:url]).to eq(subject.env.url) } + + context 'when response is not finished' do + subject { Faraday::Response.new.to_hash } + + it { is_expected.to eq({ status: nil, body: nil, response_headers: {}, url: nil }) } + end end describe 'marshal serialization support' do