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

Add support for :query_params to set query parameters for non-GET requests #287

Merged
merged 2 commits into from
May 2, 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
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ Metrics/BlockLength:
- spec/**/*
- rack-test.gemspec

# Rationale: Enforcing maximum module length makes code worse, without exception
# Rationale: Enforcing maximum length/complexity makes code worse, without exception
Metrics/ModuleLength:
Enabled: false
Metrics/ClassLength:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false

# Rationale: allow Weirich-style blocks, but do not enforce them.
Style/BlockDelimiters:
Expand Down
20 changes: 15 additions & 5 deletions lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,17 @@ def env_for(uri, env)
# Stringifying and upcasing methods has be commit upstream
env['REQUEST_METHOD'] ||= env[:method] ? env[:method].to_s.upcase : 'GET'

params = env.delete(:params) do {} end
params = env.delete(:params)
query_array = [uri.query]

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

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

if params.is_a?(Hash)
if data = build_multipart(params)
Expand All @@ -257,11 +257,21 @@ def env_for(uri, env)
end
end

if query_params = env.delete(:query_params)
append_query_params(query_array, query_params)
end
uri.query = query_array.compact.reject { |v| v == '' }.join('&')

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

Rack::MockRequest.env_for(uri.to_s, env)
end

def append_query_params(query_array, query_params)
query_params = parse_nested_query(query_params) if query_params.is_a?(String)
query_array << build_nested_query(query_params)
end

def multipart_content_type(env)
requested_content_type = env['CONTENT_TYPE']
if requested_content_type.start_with?('multipart/')
Expand Down
29 changes: 28 additions & 1 deletion spec/rack/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,38 @@
end
end

it 'supports sending :params' do
it 'supports sending :params for GET' do
request '/', params: { 'foo' => 'bar' }
expect(last_request.GET['foo']).to eq('bar')
end

it 'supports sending :query_params for GET' do
request '/', query_params: { 'foo' => 'bar' }
expect(last_request.GET['foo']).to eq('bar')
end

it 'supports sending both :params and :query_params for GET' do
request '/', query_params: { 'foo' => 'bar' }, params: { 'foo2' => 'bar2' }
expect(last_request.GET['foo']).to eq('bar')
expect(last_request.GET['foo2']).to eq('bar2')
end

it 'supports sending :params for POST' do
request '/', method: :post, params: { 'foo' => 'bar' }
expect(last_request.POST['foo']).to eq('bar')
end

it 'supports sending :query_params for POST' do
request '/', method: :post, query_params: { 'foo' => 'bar' }
expect(last_request.GET['foo']).to eq('bar')
end

it 'supports sending both :params and :query_params for POST' do
request '/', method: :post, query_params: { 'foo' => 'bar' }, params: { 'foo2' => 'bar2' }
expect(last_request.GET['foo']).to eq('bar')
expect(last_request.POST['foo2']).to eq('bar2')
end

it "doesn't follow redirects by default" do
request '/redirect'
expect(last_response).to be_redirect
Expand Down