diff --git a/lib/rack/test.rb b/lib/rack/test.rb index aba7b3d..412fff9 100644 --- a/lib/rack/test.rb +++ b/lib/rack/test.rb @@ -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}" diff --git a/spec/rack/test_spec.rb b/spec/rack/test_spec.rb index 5ceae15..7ab04d8 100644 --- a/spec/rack/test_spec.rb +++ b/spec/rack/test_spec.rb @@ -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'