Add first implementation of decode_www_form_18
Seperates now foo=bar&bar=foo;baz=foo queries. But does not fix url encoded strings and such things. Also added testcase for an empty query string.
This commit is contained in:
parent
443958f885
commit
bfca378f8f
@ -14,7 +14,32 @@ module URI
|
|||||||
end
|
end
|
||||||
|
|
||||||
# else split the query self
|
# else split the query self
|
||||||
return []
|
return self.decode_www_form_18 query
|
||||||
|
end
|
||||||
|
|
||||||
|
# Own implementation of decode_www_form.
|
||||||
|
# Shall behave almost like the original method, but without any encoding
|
||||||
|
# stuff.
|
||||||
|
#
|
||||||
|
# @param [String] query The query string
|
||||||
|
# @return [Array] Parsed query
|
||||||
|
#
|
||||||
|
def self.decode_www_form_18(query)
|
||||||
|
return [] if query.empty?
|
||||||
|
unless query.match '='
|
||||||
|
raise ArgumentError,
|
||||||
|
"invalid data of application/x-www-form-urlencoded (#{query})"
|
||||||
|
end
|
||||||
|
parsed = []
|
||||||
|
# breakes the string at & and ;
|
||||||
|
query.split(/[&;]/).each do |query_part|
|
||||||
|
# breakes the string parts at =
|
||||||
|
key, value = query_part.split('=')
|
||||||
|
# append the key value pair on the result array
|
||||||
|
parsed << [key, value]
|
||||||
|
end
|
||||||
|
# return result array
|
||||||
|
return parsed
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
@ -108,4 +108,12 @@ describe URI, '#parse_query_string' do
|
|||||||
result.should eq([['foo[]', 'foo'], ['foo[]', 'bar']])
|
result.should eq([['foo[]', 'foo'], ['foo[]', 'bar']])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'returns an empty array, if query string is empty' do
|
||||||
|
result = URI::parse_query_string("")
|
||||||
|
|
||||||
|
result.should eq([])
|
||||||
|
end
|
||||||
|
|
||||||
|
# what happend on more than one = without an & or ; in between?
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user