diff options
author | Ryan Tomayko <rtomayko@gmail.com> | 2009-03-05 21:20:58 -0800 |
---|---|---|
committer | Ryan Tomayko <rtomayko@gmail.com> | 2009-03-12 16:18:33 -0700 |
commit | 86ddc7a6ec68d7b6951c2dbd07947c4254e8bc0d (patch) | |
tree | a0152e88d80873196604e6783dd1189cbaf8f757 | |
parent | 1270a6db3d535f3abddba1c9566feb09535c3f56 (diff) | |
download | rack-86ddc7a6ec68d7b6951c2dbd07947c4254e8bc0d.tar.gz |
Rack::Lint no longer requires a Content-Length response header
-rw-r--r-- | lib/rack/lint.rb | 54 | ||||
-rw-r--r-- | test/spec_rack_lint.rb | 14 |
2 files changed, 19 insertions, 49 deletions
diff --git a/lib/rack/lint.rb b/lib/rack/lint.rb index ec4dac96..44a33ce3 100644 --- a/lib/rack/lint.rb +++ b/lib/rack/lint.rb @@ -374,59 +374,43 @@ module Rack ## === The Content-Length def check_content_length(status, headers, env) - chunked_response = false - headers.each { |key, value| - if key.downcase == 'transfer-encoding' - chunked_response = value.downcase != 'identity' - end - } - headers.each { |key, value| if key.downcase == 'content-length' - ## There must be a <tt>Content-Length</tt>, except when the - ## +Status+ is 1xx, 204 or 304, in which case there must be none - ## given. + ## There must not be a <tt>Content-Length</tt> header when the + ## +Status+ is 1xx, 204 or 304. assert("Content-Length header found in #{status} response, not allowed") { not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i } - assert('Content-Length header should not be used if body is chunked') { - not chunked_response - } - bytes = 0 string_body = true - @body.each { |part| - unless part.kind_of?(String) - string_body = false - break - end + if @body.respond_to?(:to_ary) + @body.each { |part| + unless part.kind_of?(String) + string_body = false + break + end - bytes += Rack::Utils.bytesize(part) - } - - if env["REQUEST_METHOD"] == "HEAD" - assert("Response body was given for HEAD request, but should be empty") { - bytes == 0 + bytes += Rack::Utils.bytesize(part) } - else - if string_body - assert("Content-Length header was #{value}, but should be #{bytes}") { - value == bytes.to_s + + if env["REQUEST_METHOD"] == "HEAD" + assert("Response body was given for HEAD request, but should be empty") { + bytes == 0 } + else + if string_body + assert("Content-Length header was #{value}, but should be #{bytes}") { + value == bytes.to_s + } + end end end return end } - - if [ String, Array ].include?(@body.class) && !chunked_response - assert('No Content-Length header found') { - Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i - } - end end ## === The Body diff --git a/test/spec_rack_lint.rb b/test/spec_rack_lint.rb index 2e78748d..0b4d6fb4 100644 --- a/test/spec_rack_lint.rb +++ b/test/spec_rack_lint.rb @@ -222,13 +222,6 @@ context "Rack::Lint" do end specify "notices content-length errors" do - lambda { - Rack::Lint.new(lambda { |env| - [200, {"Content-type" => "text/plain"}, []] - }).call(env({})) - }.should.raise(Rack::Lint::LintError). - message.should.match(/No Content-Length/) - [100, 101, 204, 304].each do |status| lambda { Rack::Lint.new(lambda { |env| @@ -240,13 +233,6 @@ context "Rack::Lint" do lambda { Rack::Lint.new(lambda { |env| - [200, {"Content-type" => "text/plain", "Content-Length" => "0", "Transfer-Encoding" => "chunked"}, []] - }).call(env({})) - }.should.raise(Rack::Lint::LintError). - message.should.match(/Content-Length header should not be used/) - - lambda { - Rack::Lint.new(lambda { |env| [200, {"Content-type" => "text/plain", "Content-Length" => "1"}, []] }).call(env({})) }.should.raise(Rack::Lint::LintError). |