make it possible to push lists as paramters
This makes it possible to push lists as paramters by naming the variables with `[]` at the end, for example `foo[]`.
This commit is contained in:
parent
d92dea4695
commit
30215c37d1
|
@ -21,6 +21,8 @@ module Zero
|
|||
'application/x-www-form-urlencoded',
|
||||
'multipart/form-data'
|
||||
].to_set
|
||||
# match keys for list attribute
|
||||
REGEX_MATCH_LIST = /\[\]$/
|
||||
|
||||
# get the query parameters
|
||||
attr_reader :query
|
||||
|
@ -95,7 +97,16 @@ module Zero
|
|||
# @param query [String] the query string
|
||||
# @return [Hash] the key/valuie pairs
|
||||
def parse_string(query)
|
||||
Hash[URI.decode_www_form(query)]
|
||||
result = {}
|
||||
URI.decode_www_form(query).each do |p|
|
||||
if p.first.match(REGEX_MATCH_LIST)
|
||||
result[p.first] ||= []
|
||||
result[p.first] << p.last
|
||||
else
|
||||
result[p.first] = p.last
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,6 +31,25 @@ describe Zero::Request::Parameter, '#payload' do
|
|||
its(:payload) { should == {'bar' => 'foo bar'} }
|
||||
end
|
||||
|
||||
context 'with multiple parameters' do
|
||||
let(:env) do
|
||||
EnvGenerator.post('/foo', {
|
||||
:input => 'bar=foo&foo=bar', 'CONTENT_TYPE' => 'multipart/form-data'
|
||||
})
|
||||
end
|
||||
its(:payload) { should == {'foo' => 'bar', 'bar' => 'foo'} }
|
||||
end
|
||||
|
||||
context 'with a list' do
|
||||
let(:env) do
|
||||
EnvGenerator.post('/foo', {
|
||||
:input => 'bar[]=foo&bar[]=bar',
|
||||
'CONTENT_TYPE' => 'multipart/form-data'
|
||||
})
|
||||
end
|
||||
its(:payload) { should == {'bar[]' => ['foo', 'bar']} }
|
||||
end
|
||||
|
||||
# TODO behaves like this, but is this really good like this?
|
||||
context 'with a post body and content type application/json' do
|
||||
let(:env) do
|
||||
|
|
Loading…
Reference in New Issue