Skip to content

Commit

Permalink
Do not set Content-Type if params are explicitly set to nil
Browse files Browse the repository at this point in the history
The breaking change was introduced with d016695
that disallows to send the payload with DELETE requests.

Make the request method skip setting default Content-Type header
if params are explicitly set to `nil`.
  • Loading branch information
barthez committed Nov 30, 2017
1 parent 56fdf0c commit 3b2c2ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
18 changes: 9 additions & 9 deletions lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,31 +217,31 @@ def env_for(uri, env)
# Stringifying and upcasing methods has be commit upstream
env['REQUEST_METHOD'] ||= env[:method] ? env[:method].to_s.upcase : 'GET'

if %w[GET DELETE].include?(env['REQUEST_METHOD'])
params = env.delete(:params) { {} }

if env['REQUEST_METHOD'] == 'GET'
# merge :params with the query string
if params = env[:params]
if params = params
params = parse_nested_query(params) if params.is_a?(String)

uri.query = [uri.query, build_nested_query(params)].compact.reject { |v| v == '' }.join('&')
end
elsif !env.key?(:input)
env['CONTENT_TYPE'] ||= 'application/x-www-form-urlencoded'
env['CONTENT_TYPE'] ||= 'application/x-www-form-urlencoded' unless params.nil?

if env[:params].is_a?(Hash)
if data = build_multipart(env[:params])
if params.is_a?(Hash)
if data = build_multipart(params)
env[:input] = data
env['CONTENT_LENGTH'] ||= data.length.to_s
env['CONTENT_TYPE'] = "multipart/form-data; boundary=#{MULTIPART_BOUNDARY}"
else
env[:input] = params_to_string(env[:params])
env[:input] = params_to_string(params)
end
else
env[:input] = env[:params]
env[:input] = params
end
end

env.delete(:params)

set_cookie(env.delete(:cookie), uri) if env.key?(:cookie)

Rack::MockRequest.env_for(uri.to_s, env)
Expand Down
11 changes: 5 additions & 6 deletions spec/rack/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ def close
expect(last_request.env['HTTP_USER_AGENT']).to eq('Rack::Test')
end

it 'does not set CONTENT_TYPE if params are explicitly set to nil' do
public_send(verb, '/', nil)
expect(last_request.env['CONTENT_TYPE']).to be_nil
end

it 'yields the response to a given block' do
yielded = false

Expand Down Expand Up @@ -601,12 +606,6 @@ def verb
def verb
'delete'
end

it 'does not set a content type' do
delete '/'

expect(last_request.env['CONTENT_TYPE']).to be_nil
end
end

describe '#options' do
Expand Down

0 comments on commit 3b2c2ff

Please sign in to comment.