From 967f5ae999b5914a783c7ac3684eda7f410b405d Mon Sep 17 00:00:00 2001 From: Joe Francis Date: Wed, 6 Dec 2017 19:26:09 -0600 Subject: [PATCH] return correct content-length when response includes multibyte characters --- lib/rack/contrib/not_found.rb | 2 +- test/spec_rack_not_found.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rack/contrib/not_found.rb b/lib/rack/contrib/not_found.rb index d49e9ab7..cbf9a60e 100644 --- a/lib/rack/contrib/not_found.rb +++ b/lib/rack/contrib/not_found.rb @@ -19,7 +19,7 @@ def initialize(path = nil, content_type = 'text/html') else @content = F.read(path) end - @length = @content.size.to_s + @length = @content.bytesize.to_s @content_type = content_type end diff --git a/test/spec_rack_not_found.rb b/test/spec_rack_not_found.rb index bc331fa9..052159b6 100644 --- a/test/spec_rack_not_found.rb +++ b/test/spec_rack_not_found.rb @@ -1,6 +1,7 @@ require 'minitest/autorun' require 'rack/mock' require 'rack/contrib/not_found' +require 'tempfile' describe "Rack::NotFound" do @@ -40,4 +41,18 @@ response.status.must_equal(404) end + specify "should return correct size" do + Tempfile.open('test') do |f| + f.write '' + f.write '' + f.write '☃ snowman' + f.close + app = Rack::Builder.new do + use Rack::Lint + run Rack::NotFound.new(f.path) + end + response = Rack::MockRequest.new(app).get('/') + response.headers['Content-Length'].must_equal('46') + end + end end