Skip to content

Commit

Permalink
Support :mulitpart env key or multipart CONTENT_TYPE to Rack::Test::S…
Browse files Browse the repository at this point in the history
…ession#env_for to force multipart input
  • Loading branch information
jeremyevans committed Jun 1, 2022
1 parent cf9d2e0 commit 5b4bbff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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
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

0 comments on commit 5b4bbff

Please sign in to comment.