mirror of
https://github.com/lostisland/faraday.git
synced 2025-11-22 00:08:56 -05:00
Separate Request and Response bodies (#847)
This commit is contained in:
parent
6e4a2aaa05
commit
6e3c40f159
@ -13,6 +13,10 @@ Please note `Faraday::ClientError` was previously used for both.
|
|||||||
* Faraday::ProxyAuthError (407). Please note this raised a `Faraday::ConnectionFailed` before.
|
* Faraday::ProxyAuthError (407). Please note this raised a `Faraday::ConnectionFailed` before.
|
||||||
* Faraday::UnprocessableEntityError (422)
|
* Faraday::UnprocessableEntityError (422)
|
||||||
|
|
||||||
|
### Custom adapters
|
||||||
|
If you have written a custom adapter, please be aware that `env.body` is now an alias to the two new properties `request_body` and `response_body`.
|
||||||
|
This should work without you noticing if your adapter inherits from `Faraday::Adapter` and calls `save_response`, but if it doesn't, then please ensure you set the `status` BEFORE the `body` while processing the response.
|
||||||
|
|
||||||
### Others
|
### Others
|
||||||
* Dropped support for jruby and Rubinius.
|
* Dropped support for jruby and Rubinius.
|
||||||
* Officially supports Ruby 2.3+
|
* Officially supports Ruby 2.3+
|
||||||
|
|||||||
@ -43,9 +43,9 @@ module Faraday
|
|||||||
#
|
#
|
||||||
# @!attribute reason_phrase
|
# @!attribute reason_phrase
|
||||||
# @return [String]
|
# @return [String]
|
||||||
class Env < Options.new(:method, :body, :url, :request, :request_headers,
|
class Env < Options.new(:method, :request_body, :url, :request, :request_headers,
|
||||||
:ssl, :parallel_manager, :params, :response, :response_headers, :status,
|
:ssl, :parallel_manager, :params, :response, :response_headers, :status,
|
||||||
:reason_phrase)
|
:reason_phrase, :response_body)
|
||||||
|
|
||||||
ContentLength = 'Content-Length'.freeze
|
ContentLength = 'Content-Length'.freeze
|
||||||
StatusesWithoutBody = Set.new [204, 304]
|
StatusesWithoutBody = Set.new [204, 304]
|
||||||
@ -76,6 +76,7 @@ module Faraday
|
|||||||
|
|
||||||
# @param key [Object]
|
# @param key [Object]
|
||||||
def [](key)
|
def [](key)
|
||||||
|
return self[current_body] if key == :body
|
||||||
if in_member_set?(key)
|
if in_member_set?(key)
|
||||||
super(key)
|
super(key)
|
||||||
else
|
else
|
||||||
@ -86,6 +87,7 @@ module Faraday
|
|||||||
# @param key [Object]
|
# @param key [Object]
|
||||||
# @param value [Object]
|
# @param value [Object]
|
||||||
def []=(key, value)
|
def []=(key, value)
|
||||||
|
return super(current_body, value) if key == :body
|
||||||
if in_member_set?(key)
|
if in_member_set?(key)
|
||||||
super(key, value)
|
super(key, value)
|
||||||
else
|
else
|
||||||
@ -93,6 +95,18 @@ module Faraday
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def current_body
|
||||||
|
!!status ? :response_body : :request_body
|
||||||
|
end
|
||||||
|
|
||||||
|
def body
|
||||||
|
self[:body]
|
||||||
|
end
|
||||||
|
|
||||||
|
def body=(value)
|
||||||
|
self[:body] = value
|
||||||
|
end
|
||||||
|
|
||||||
# @return [Boolean] true if status is in the set of {SuccessfulStatuses}.
|
# @return [Boolean] true if status is in the set of {SuccessfulStatuses}.
|
||||||
def success?
|
def success?
|
||||||
SuccessfulStatuses.include?(status)
|
SuccessfulStatuses.include?(status)
|
||||||
|
|||||||
@ -31,8 +31,6 @@ module Faraday
|
|||||||
|
|
||||||
attr_reader :env
|
attr_reader :env
|
||||||
|
|
||||||
def_delegators :env, :to_hash
|
|
||||||
|
|
||||||
def status
|
def status
|
||||||
finished? ? env.status : nil
|
finished? ? env.status : nil
|
||||||
end
|
end
|
||||||
@ -74,12 +72,16 @@ module Faraday
|
|||||||
finished? && env.success?
|
finished? && env.success?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
{
|
||||||
|
:status => env.status, :body => env.body,
|
||||||
|
:response_headers => env.response_headers
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
# because @on_complete_callbacks cannot be marshalled
|
# because @on_complete_callbacks cannot be marshalled
|
||||||
def marshal_dump
|
def marshal_dump
|
||||||
!finished? ? nil : {
|
finished? ? to_hash : nil
|
||||||
:status => @env.status, :body => @env.body,
|
|
||||||
:response_headers => @env.response_headers
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def marshal_load(env)
|
def marshal_load(env)
|
||||||
|
|||||||
@ -1,21 +1,22 @@
|
|||||||
RSpec.describe Faraday::Env do
|
RSpec.describe Faraday::Env do
|
||||||
subject { Faraday::Env.new }
|
subject(:env) { described_class.new }
|
||||||
|
|
||||||
it 'allows to access members' do
|
it 'allows to access members' do
|
||||||
expect(subject.method).to be_nil
|
expect(env.method).to be_nil
|
||||||
subject.method = :get
|
env.method = :get
|
||||||
expect(subject.method).to eq(:get)
|
expect(env.method).to eq(:get)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'allows to access symbol non members' do
|
it 'allows to access symbol non members' do
|
||||||
expect(subject[:custom]).to be_nil
|
expect(env[:custom]).to be_nil
|
||||||
subject[:custom] = :boom
|
env[:custom] = :boom
|
||||||
expect(subject[:custom]).to eq(:boom)
|
expect(env[:custom]).to eq(:boom)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'allows to access string non members' do
|
it 'allows to access string non members' do
|
||||||
expect(subject['custom']).to be_nil
|
expect(env['custom']).to be_nil
|
||||||
subject['custom'] = :boom
|
env['custom'] = :boom
|
||||||
expect(subject['custom']).to eq(:boom)
|
expect(env['custom']).to eq(:boom)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'ignores false when fetching' do
|
it 'ignores false when fetching' do
|
||||||
@ -25,13 +26,43 @@ RSpec.describe Faraday::Env do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'retains custom members' do
|
it 'retains custom members' do
|
||||||
subject[:foo] = "custom 1"
|
env[:foo] = "custom 1"
|
||||||
subject[:bar] = :custom_2
|
env[:bar] = :custom_2
|
||||||
env2 = Faraday::Env.from(subject)
|
env2 = Faraday::Env.from(env)
|
||||||
env2[:baz] = "custom 3"
|
env2[:baz] = "custom 3"
|
||||||
|
|
||||||
expect(env2[:foo]).to eq("custom 1")
|
expect(env2[:foo]).to eq("custom 1")
|
||||||
expect(env2[:bar]).to eq(:custom_2)
|
expect(env2[:bar]).to eq(:custom_2)
|
||||||
expect(subject[:baz]).to be_nil
|
expect(env[:baz]).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#body' do
|
||||||
|
subject(:env) { described_class.from(body: { foo: 'bar' }) }
|
||||||
|
|
||||||
|
context 'when response is not finished yet' do
|
||||||
|
it 'returns the request body' do
|
||||||
|
expect(env.body).to eq({ foo: 'bar' })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when response is finished' do
|
||||||
|
before do
|
||||||
|
env.status = 200
|
||||||
|
env.body = { bar: 'foo' }
|
||||||
|
env.response = Faraday::Response.new(env)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns the response body' do
|
||||||
|
expect(env.body).to eq({ bar: 'foo' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'allows to access request_body' do
|
||||||
|
expect(env.request_body).to eq({ foo: 'bar' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'allows to access response_body' do
|
||||||
|
expect(env.response_body).to eq({ bar: 'foo' })
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -25,7 +25,6 @@ RSpec.describe Faraday::Response do
|
|||||||
let(:hash) { subject.to_hash }
|
let(:hash) { subject.to_hash }
|
||||||
|
|
||||||
it { expect(hash).to be_a(Hash) }
|
it { expect(hash).to be_a(Hash) }
|
||||||
it { expect(hash).to eq(env.to_hash) }
|
|
||||||
it { expect(hash[:status]).to eq(subject.status) }
|
it { expect(hash[:status]).to eq(subject.status) }
|
||||||
it { expect(hash[:response_headers]).to eq(subject.headers) }
|
it { expect(hash[:response_headers]).to eq(subject.headers) }
|
||||||
it { expect(hash[:body]).to eq(subject.body) }
|
it { expect(hash[:body]).to eq(subject.body) }
|
||||||
|
|||||||
@ -167,7 +167,6 @@ shared_examples 'a request method' do |http_method|
|
|||||||
it 'handles requests with proxy' do
|
it 'handles requests with proxy' do
|
||||||
conn_options[:proxy] = 'http://google.co.uk'
|
conn_options[:proxy] = 'http://google.co.uk'
|
||||||
|
|
||||||
# binding.pry
|
|
||||||
res = conn.public_send(http_method, '/')
|
res = conn.public_send(http_method, '/')
|
||||||
expect(res.status).to eq(200)
|
expect(res.status).to eq(200)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user