Skip to content

Commit

Permalink
Ensure correct encoding on chunked responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dim committed Nov 30, 2011
1 parent f6ee254 commit a81e9e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rack/chunked.rb
Expand Up @@ -23,6 +23,8 @@ def each
@body.each do |chunk|
size = bytesize(chunk)
next if size == 0

chunk = chunk.dup.force_encoding(Encoding::BINARY) if chunk.respond_to?(:force_encoding)
yield [size.to_s(16), term, chunk, term].join
end
yield TAIL
Expand Down
10 changes: 10 additions & 0 deletions test/spec_chunked.rb
Expand Up @@ -23,6 +23,16 @@
response.body.should.equal "0\r\n\r\n"
end

should 'chunks encoded bodies properly' do
body = ["\uFFFEHello", " ", "World"].map {|t| t.encode("UTF-16LE") }
app = lambda { |env| [200, {}, body] }
response = Rack::MockResponse.new(*Rack::Chunked.new(app).call(@env))
response.headers.should.not.include 'Content-Length'
response.headers['Transfer-Encoding'].should.equal 'chunked'
response.body.encoding.to_s.should == "ASCII-8BIT"
response.body.should.equal "c\r\n\xFE\xFFH\x00e\x00l\x00l\x00o\x00\r\n2\r\n \x00\r\na\r\nW\x00o\x00r\x00l\x00d\x00\r\n0\r\n\r\n"
end if RUBY_VERSION >= "1.9"

should 'not modify response when Content-Length header present' do
app = lambda { |env| [200, {'Content-Length'=>'12'}, ['Hello', ' ', 'World!']] }
status, headers, body = Rack::Chunked.new(app).call(@env)
Expand Down

0 comments on commit a81e9e1

Please sign in to comment.