Skip to content

Commit

Permalink
Fix: request body parameters from a PATCH request not avaialble in pa…
Browse files Browse the repository at this point in the history
…rams.
  • Loading branch information
FreakenK authored and dblock committed Feb 13, 2013
1 parent 87f7a7f commit f72135b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Next Release
* [#60](https://github.com/intridea/grape/issues/60): Fix: mounting of a Grape API onto a path - [@dblock](http://github.com/dblock).
* [#190](https://github.com/intridea/grape/issues/190): When you add a `GET` route for a resource, a route for the `HEAD` method will also be added automatically. You can disable this behavior with `do_not_route_head!` - [@dblock](http://github.com/dblock).
* Added `do_not_route_options!`, which disables the automatic creation of the `OPTIONS` route - [@dblock](http://github.com/dblock).
* [#335](https://github.com/intridea/grape/pull/335): Fix: request body parameters from a `PATCH` request not available in `params` - [@FreakenK](http://github.com/FreakenK).
* Your contribution here.

0.2.6 (01/11/2013)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def after
private

def read_body_input
if (request.post? || request.put?) && (! 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)
if env['rack.input'] && (body = env['rack.input'].read).length > 0
begin
fmt = mime_types[request.media_type] if request.media_type
Expand Down
69 changes: 37 additions & 32 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def to_xml
end

context 'detection' do

it 'uses the extension if one is provided' do
subject.call({'PATH_INFO' => '/info.xml'})
subject.env['api.format'].should == :xml
Expand Down Expand Up @@ -149,43 +149,48 @@ def to_xml
end

context 'input' do
[ "application/json", "application/json; charset=utf-8" ].each do |content_type|
it 'parses the body from a POST/PUT and put the contents into rack.request.form_hash for #{content_type}' do
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
subject.call({
'PATH_INFO' => '/info',
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => content_type,
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
subject.env['rack.request.form_hash']['is_boolean'].should be_true
subject.env['rack.request.form_hash']['string'].should == 'thing'
[ "POST", "PATCH", "PUT" ].each do |method|
[ "application/json", "application/json; charset=utf-8" ].each do |content_type|
context content_type do
it 'parses the body from #{method} and copies values into rack.request.form_hash' do
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
subject.call({
'PATH_INFO' => '/info',
'REQUEST_METHOD' => method,
'CONTENT_TYPE' => content_type,
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
subject.env['rack.request.form_hash']['is_boolean'].should be_true
subject.env['rack.request.form_hash']['string'].should == 'thing'
end
end
end
end
it 'parses the body from an xml POST/PUT and put the contents into rack.request.from_hash' do
io = StringIO.new('<thing><name>Test</name></thing>')
subject.call({
'PATH_INFO' => '/info.xml',
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => 'application/xml',
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
end
[ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type|
it "ignores #{content_type}" do
io = StringIO.new('name=Other+Test+Thing')
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({
'PATH_INFO' => '/info',
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => content_type,
'PATH_INFO' => '/info.xml',
'REQUEST_METHOD' => method,
'CONTENT_TYPE' => 'application/xml',
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
subject.env['rack.request.form_hash'].should be_nil
subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
end
[ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type|
it "ignores #{content_type}" do
io = StringIO.new('name=Other+Test+Thing')
subject.call({
'PATH_INFO' => '/info',
'REQUEST_METHOD' => method,
'CONTENT_TYPE' => content_type,
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
subject.env['rack.request.form_hash'].should be_nil
end
end
end
end

end

0 comments on commit f72135b

Please sign in to comment.