Skip to content

Commit

Permalink
Don't raise error with empty params and multipart header
Browse files Browse the repository at this point in the history
POST request with empty params and multipart header raises a `Rack::EmptyContentError` since 5b4bbff.
https://github.com/rack/rack/blob/v3.0.4.1/lib/rack/multipart/parser.rb#L415-L419
  • Loading branch information
tricknotes committed Feb 6, 2023
1 parent febbea4 commit 5a2a3bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def env_for(uri, env)
multipart = env.has_key?(:multipart) ? env.delete(:multipart) : env['CONTENT_TYPE'].start_with?('multipart/')

if params.is_a?(Hash)
if data = build_multipart(params, false, multipart)
if !params.empty? && data = build_multipart(params, false, multipart)
env[:input] = data
env['CONTENT_LENGTH'] ||= data.length.to_s
env['CONTENT_TYPE'] = "#{multipart_content_type(env)}; boundary=#{MULTIPART_BOUNDARY}"
Expand Down
7 changes: 7 additions & 0 deletions spec/rack/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@
last_request.env['rack.input'].read.must_include 'content-disposition: form-data; name="foo"'
end

it 'supports multipart CONTENT_TYPE when using empty :params for POST to be empty body' do
request '/', method: :post, params: {}, 'CONTENT_TYPE'=>'multipart/form-data'
last_request.POST.must_be_empty
last_request.env['rack.input'].rewind
last_request.env['rack.input'].read.must_be_empty
end

it 'supports sending :query_params for POST' do
request '/', method: :post, query_params: { 'foo' => 'bar' }
last_request.GET['foo'].must_equal 'bar'
Expand Down

0 comments on commit 5a2a3bf

Please sign in to comment.