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

Support :mulitpart env key to env_for to force multipart input #303

Merged
merged 1 commit into from
Jun 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ def env_for(uri, env)
elsif !env.key?(:input)
env['CONTENT_TYPE'] ||= 'application/x-www-form-urlencoded'
params ||= {}
multipart = env.has_key?(:multipart) ? env.delete(:multipart) : env['CONTENT_TYPE'].start_with?('multipart/')

if params.is_a?(Hash)
if data = build_multipart(params)
if 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
21 changes: 21 additions & 0 deletions spec/rack/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@
last_request.POST['foo'].must_equal 'bar'
end

it 'does not use multipart input for :params for POST by default' do
request '/', method: :post, params: { 'foo' => 'bar' }
last_request.POST['foo'].must_equal 'bar'
last_request.env['rack.input'].rewind
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
last_request.env['rack.input'].read.must_equal 'foo=bar'
end

it 'supports :multipart when using :params for POST to force multipart input' do
request '/', method: :post, params: { 'foo' => 'bar' }, multipart: true
last_request.POST['foo'].must_equal 'bar'
last_request.env['rack.input'].rewind
last_request.env['rack.input'].read.must_include 'content-disposition: form-data; name="foo"'
end

it 'supports multipart CONTENT_TYPE when using :params for POST to force multipart input' do
request '/', method: :post, params: { 'foo' => 'bar' }, 'CONTENT_TYPE'=>'multipart/form-data'
last_request.POST['foo'].must_equal 'bar'
last_request.env['rack.input'].rewind
last_request.env['rack.input'].read.must_include 'content-disposition: form-data; name="foo"'
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