Skip to content

Commit

Permalink
Fix Content-Length overwrite for multibyte characters by using Rack::…
Browse files Browse the repository at this point in the history
…Utils.bytesize
  • Loading branch information
Amiel Martin committed Jul 20, 2012
1 parent 8b56cd9 commit 3758c8d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/jsonp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call(env)
json = ""
body.each { |s| json << s }
body = ["#{callback}(#{json});"]
headers['Content-Length'] = body[0].length.to_s
headers['Content-Length'] = Rack::Utils.bytesize(body[0]).to_s
headers['Content-Type'] = 'application/javascript'
end

Expand Down
30 changes: 30 additions & 0 deletions spec/jsonp_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

require 'spec_helper.rb'

describe Rack::JSONP do
Expand Down Expand Up @@ -43,6 +45,34 @@

end

describe 'when a valid jsonp request is made with multibyte characters' do

before :each do
@response_body = ['{"key":"√alue"}']
@request = Rack::MockRequest.env_for("/action.jsonp?callback=#{@callback}")
@jsonp_response = Rack::JSONP.new(@app).call(@request)
@jsonp_response_status, @jsonp_response_headers, @jsonp_response_body = @jsonp_response
end

it 'should not modify the response status code' do
@jsonp_response_status.should equal @response_status
end

it 'should update the response content length to the new value' do
@jsonp_response_headers['Content-Length'].should == '34'
end

it 'should set the response content type as application/javascript' do
@jsonp_response_headers['Content-Type'].should == 'application/javascript'
end

it 'should wrap the response body in the Javasript callback' do
@jsonp_response_body.should == ["#{@callback}(#{@response_body.first});"]
end

end


describe 'when a jsonp request is made wihtout a callback parameter present' do

before :each do
Expand Down

0 comments on commit 3758c8d

Please sign in to comment.