Skip to content

Commit

Permalink
Merge pull request #380 from gatherdigital/chunked_encoding
Browse files Browse the repository at this point in the history
Streaming requests broken after 0.2.4.
  • Loading branch information
dblock committed Apr 14, 2013
2 parents 06808bd + b5387eb commit b03753c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next Release
============

* [#378](https://github.com/intridea/grape/pull/378): Fix: stop rescuing all exceptions during formatting - [@kbarrette](https://github.com/kbarrette).
* [#380](https://github.com/intridea/grape/pull/380): Fix: Formatter#read_body_input when transfer encoding is chunked - [@paulnicholon](https://github.com/paulnicholson).
* Your contribution here.

0.4.1 (4/1/2013)
Expand Down
5 changes: 4 additions & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def after

# store read input in env['api.request.input']
def read_body_input
if (request.post? || request.put? || request.patch?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
if (request.post? || request.put? || request.patch?) &&
(! request.form_data?) && (! request.parseable_data?) &&
(request.content_length.to_i > 0 || request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')

if env['rack.input'] && (body = (env['api.request.input'] = env['rack.input'].read)).length > 0
read_rack_input body
end
Expand Down
11 changes: 11 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ def subject.enable_root_route!
last_response.status.should == (verb == :post ? 201 : 200)
last_response.body.should eql MultiJson.dump(object).to_json
end
context "chunked transfer encoding" do
it "stores input in api.request.input" do
subject.format :json
subject.send(verb) do
env['api.request.input']
end
send verb, '/', MultiJson.dump(object), { 'CONTENT_TYPE' => 'application/json', 'HTTP_TRANSFER_ENCODING' => 'chunked', 'CONTENT_LENGTH' => nil }
last_response.status.should == (verb == :post ? 201 : 200)
last_response.body.should eql MultiJson.dump(object).to_json
end
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ def to_xml
end
end
end
it "parses the chunked body from #{method} and copies values into rack.request.from_hash" do
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
subject.call({
'PATH_INFO' => '/infol',
'REQUEST_METHOD' => method,
'CONTENT_TYPE' => 'application/json',
'rack.input' => io,
'HTTP_TRANSFER_ENCODING' => 'chunked'
})
subject.env['rack.request.form_hash']['is_boolean'].should be_true
subject.env['rack.request.form_hash']['string'].should == 'thing'
end
it 'parses the body from an xml #{method} and copies values into rack.request.from_hash' do
io = StringIO.new('<thing><name>Test</name></thing>')
subject.call({
Expand Down

0 comments on commit b03753c

Please sign in to comment.