Fix response not changing with parallel requests

In this order the @env would not be changed by the "on complete"
callbacks, meaning that the response would not change after parallel
requests finish (e.g. JSON would not be parsed).

This would work

  def on_complete(env)
    env[:body].upcase!
  end

But this wouldn't

  def on_complete(env)
    env[:body] = env[:body].upcase
  end
This commit is contained in:
Janko Marohnić 2014-05-21 01:28:03 +02:00
parent 3dc615a72e
commit edacd5eb57
2 changed files with 9 additions and 1 deletions

View File

@ -61,8 +61,8 @@ module Faraday
def finish(env)
raise "response already finished" if finished?
@env = Env.from(env)
@on_complete_callbacks.each { |callback| callback.call(env) }
@env = Env.from(env)
return self
end

View File

@ -166,6 +166,14 @@ class ResponseTest < Faraday::TestCase
end
end
def test_body_is_parsed_on_finish
response = Faraday::Response.new
response.on_complete { |env| env[:body] = env[:body].upcase }
response.finish(@env)
assert_equal "YIKES", response.body
end
def test_not_success
assert !@response.success?
end