diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2020-01-06 21:24:33 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2020-01-06 21:24:33 -0800 |
commit | 72959ebc2f300f3b2ccb7ae2aae9f199e611dfb6 (patch) | |
tree | 3e46809128e23a88065714fdc2b7ca0ebecbbc7b | |
parent | 7fecaee81f59926b6e1913511c90650e76673b38 (diff) | |
download | rack-rm_to_ary_from_response.tar.gz |
Remove `to_ary` from Responserm_to_ary_from_response
Responses are not arrays, so we should not allow them to be implicitly
coerced to an array. If you would like you response to be converted to
an array, you must explicitly do it with the `to_a` method.
This also prevents creation of unnecessary body proxies
-rw-r--r-- | lib/rack/body_proxy.rb | 5 | ||||
-rw-r--r-- | lib/rack/response.rb | 3 | ||||
-rw-r--r-- | test/spec_body_proxy.rb | 4 | ||||
-rw-r--r-- | test/spec_response.rb | 4 |
4 files changed, 5 insertions, 11 deletions
diff --git a/lib/rack/body_proxy.rb b/lib/rack/body_proxy.rb index 15e4a84f..cb161980 100644 --- a/lib/rack/body_proxy.rb +++ b/lib/rack/body_proxy.rb @@ -9,10 +9,6 @@ module Rack end def respond_to?(method_name, include_all = false) - case method_name - when :to_ary, 'to_ary' - return false - end super or @body.respond_to?(method_name, include_all) end @@ -39,7 +35,6 @@ module Rack end def method_missing(method_name, *args, &block) - super if :to_ary == method_name @body.__send__(method_name, *args, &block) end end diff --git a/lib/rack/response.rb b/lib/rack/response.rb index 58f9e5d6..f39848cc 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -72,11 +72,10 @@ module Rack close [status.to_i, header, []] else - [status.to_i, header, BodyProxy.new(self){}] + [status.to_i, header, self] end end alias to_a finish # For *response - alias to_ary finish # For implicit-splat on Ruby 1.9.2 def each(&callback) @body.each(&callback) diff --git a/test/spec_body_proxy.rb b/test/spec_body_proxy.rb index 6be79f8b..d3853e1e 100644 --- a/test/spec_body_proxy.rb +++ b/test/spec_body_proxy.rb @@ -63,8 +63,8 @@ describe Rack::BodyProxy do body.respond_to?(:to_ary).must_equal true proxy = Rack::BodyProxy.new(body) { } - proxy.respond_to?(:to_ary).must_equal false - proxy.respond_to?("to_ary").must_equal false + x = [proxy] + assert_equal x, x.flatten end it 'not close more than one time' do diff --git a/test/spec_response.rb b/test/spec_response.rb index 3cd56664..ff7cba6f 100644 --- a/test/spec_response.rb +++ b/test/spec_response.rb @@ -466,8 +466,8 @@ describe Rack::Response do it "wraps the body from #to_ary to prevent infinite loops" do res = Rack::Response.new - res.finish.last.wont_respond_to(:to_ary) - lambda { res.finish.last.to_ary }.must_raise NoMethodError + x = res.finish + assert_equal x, x.flatten end end |