Skip to content

Commit f72135b

Browse files
FreakenKdblock
authored andcommitted
Fix: request body parameters from a PATCH request not avaialble in params.
1 parent 87f7a7f commit f72135b

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

CHANGELOG.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Next Release
1515
* [#60](https://github.com/intridea/grape/issues/60): Fix: mounting of a Grape API onto a path - [@dblock](http://github.com/dblock).
1616
* [#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).
1717
* Added `do_not_route_options!`, which disables the automatic creation of the `OPTIONS` route - [@dblock](http://github.com/dblock).
18+
* [#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).
1819
* Your contribution here.
1920

2021
0.2.6 (01/11/2013)

lib/grape/middleware/formatter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def after
3838
private
3939

4040
def read_body_input
41-
if (request.post? || request.put?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
41+
if (request.post? || request.put? || request.patch?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
4242
if env['rack.input'] && (body = env['rack.input'].read).length > 0
4343
begin
4444
fmt = mime_types[request.media_type] if request.media_type

spec/grape/middleware/formatter_spec.rb

+37-32
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def to_xml
3737
end
3838

3939
context 'detection' do
40-
40+
4141
it 'uses the extension if one is provided' do
4242
subject.call({'PATH_INFO' => '/info.xml'})
4343
subject.env['api.format'].should == :xml
@@ -149,43 +149,48 @@ def to_xml
149149
end
150150

151151
context 'input' do
152-
[ "application/json", "application/json; charset=utf-8" ].each do |content_type|
153-
it 'parses the body from a POST/PUT and put the contents into rack.request.form_hash for #{content_type}' do
154-
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
155-
subject.call({
156-
'PATH_INFO' => '/info',
157-
'REQUEST_METHOD' => 'POST',
158-
'CONTENT_TYPE' => content_type,
159-
'rack.input' => io,
160-
'CONTENT_LENGTH' => io.length
161-
})
162-
subject.env['rack.request.form_hash']['is_boolean'].should be_true
163-
subject.env['rack.request.form_hash']['string'].should == 'thing'
152+
[ "POST", "PATCH", "PUT" ].each do |method|
153+
[ "application/json", "application/json; charset=utf-8" ].each do |content_type|
154+
context content_type do
155+
it 'parses the body from #{method} and copies values into rack.request.form_hash' do
156+
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
157+
subject.call({
158+
'PATH_INFO' => '/info',
159+
'REQUEST_METHOD' => method,
160+
'CONTENT_TYPE' => content_type,
161+
'rack.input' => io,
162+
'CONTENT_LENGTH' => io.length
163+
})
164+
subject.env['rack.request.form_hash']['is_boolean'].should be_true
165+
subject.env['rack.request.form_hash']['string'].should == 'thing'
166+
end
167+
end
164168
end
165-
end
166-
it 'parses the body from an xml POST/PUT and put the contents into rack.request.from_hash' do
167-
io = StringIO.new('<thing><name>Test</name></thing>')
168-
subject.call({
169-
'PATH_INFO' => '/info.xml',
170-
'REQUEST_METHOD' => 'POST',
171-
'CONTENT_TYPE' => 'application/xml',
172-
'rack.input' => io,
173-
'CONTENT_LENGTH' => io.length
174-
})
175-
subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
176-
end
177-
[ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type|
178-
it "ignores #{content_type}" do
179-
io = StringIO.new('name=Other+Test+Thing')
169+
it 'parses the body from an xml #{method} and copies values into rack.request.from_hash' do
170+
io = StringIO.new('<thing><name>Test</name></thing>')
180171
subject.call({
181-
'PATH_INFO' => '/info',
182-
'REQUEST_METHOD' => 'POST',
183-
'CONTENT_TYPE' => content_type,
172+
'PATH_INFO' => '/info.xml',
173+
'REQUEST_METHOD' => method,
174+
'CONTENT_TYPE' => 'application/xml',
184175
'rack.input' => io,
185176
'CONTENT_LENGTH' => io.length
186177
})
187-
subject.env['rack.request.form_hash'].should be_nil
178+
subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
179+
end
180+
[ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type|
181+
it "ignores #{content_type}" do
182+
io = StringIO.new('name=Other+Test+Thing')
183+
subject.call({
184+
'PATH_INFO' => '/info',
185+
'REQUEST_METHOD' => method,
186+
'CONTENT_TYPE' => content_type,
187+
'rack.input' => io,
188+
'CONTENT_LENGTH' => io.length
189+
})
190+
subject.env['rack.request.form_hash'].should be_nil
191+
end
188192
end
189193
end
190194
end
195+
191196
end

0 commit comments

Comments
 (0)