diff --git a/lib/rack/response.rb b/lib/rack/response.rb index c2046105d..f24683bcb 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -102,11 +102,16 @@ def chunked? CHUNKED == get_header(TRANSFER_ENCODING) end + def no_entity_body? + # The response body is an enumerable body and it is not allowed to have an entity body. + @body.respond_to?(:each) && STATUS_WITH_NO_ENTITY_BODY[@status] + end + # Generate a response array consistent with the requirements of the SPEC. # @return [Array] a 3-tuple suitable of `[status, headers, body]` # which is suitable to be returned from the middleware `#call(env)` method. def finish(&block) - if STATUS_WITH_NO_ENTITY_BODY[@status] + if no_entity_body? delete_header CONTENT_TYPE delete_header CONTENT_LENGTH close diff --git a/test/spec_response.rb b/test/spec_response.rb index 144adb521..ef8aa481c 100644 --- a/test/spec_response.rb +++ b/test/spec_response.rb @@ -642,6 +642,16 @@ def obj.each res.body.wont_be :closed? end + it "doesn't clear #body when 101 and streaming" do + res = Rack::Response.new + + streaming_body = proc{|stream| stream.close} + res.body = streaming_body + res.status = 101 + res.finish + res.body.must_equal streaming_body + end + it "flatten doesn't cause infinite loop" do # https://github.com/rack/rack/issues/419 res = Rack::Response.new("Hello World")