Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming requests broken after 0.2.4. #380

Merged
merged 4 commits into from
Apr 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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