From 72959ebc2f300f3b2ccb7ae2aae9f199e611dfb6 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 6 Jan 2020 21:24:33 -0800 Subject: Remove `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 --- lib/rack/body_proxy.rb | 5 ----- lib/rack/response.rb | 3 +-- test/spec_body_proxy.rb | 4 ++-- 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 -- cgit v1.2.1