Permalink
Browse files

Added graceful handling of non-alphanumeric names and misplaced brack…

…ets in input parameters [bitsweat]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@39 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 79e85ed commit 50f333b203756009acff2457b6d1c9da3b532cad @dhh dhh committed Dec 1, 2004
View
@@ -1,5 +1,7 @@
*SVN*
+* Added graceful handling of non-alphanumeric names and misplaced brackets in input parameters [bitsweat]
+
* Added a new container for cookies that makes them more intuative to use. The old methods of cookie and @cookies have been deprecated.
Examples for writing:
@@ -63,29 +63,27 @@ def CGIMethods.get_typed_value(value)
end
end
- def CGIMethods.get_levels(key_string)
- return [] if key_string.nil? or key_string.empty?
-
- levels = []
- main, existance = /(\w+)(\[)?.?/.match(key_string).captures
- levels << main
-
- unless existance.nil?
- hash_part = key_string.sub(/\w+\[/, "")
- hash_part.slice!(-1, 1)
- levels += hash_part.split(/\]\[/)
+ PARAMS_HASH_RE = /^([^\[]+)(\[.*\])?(.)?.*$/
+ def CGIMethods.get_levels(key)
+ all, main, bracketed, trailing = PARAMS_HASH_RE.match(key).to_a
+ if main.nil?
+ []
+ elsif trailing
+ [key]
+ elsif bracketed
+ [main] + bracketed.slice(1...-1).split('][')
+ else
+ [main]
end
-
- levels
end
-
+
def CGIMethods.build_deep_hash(value, hash, levels)
if levels.length == 0
- value;
+ value
elsif hash.nil?
{ levels.first => CGIMethods.build_deep_hash(value, nil, levels[1..-1]) }
else
hash.update({ levels.first => CGIMethods.build_deep_hash(value, hash[levels.first], levels[1..-1]) })
end
end
-end
+end
@@ -133,10 +133,40 @@ def test_parse_params_with_file
end
def test_parse_params_with_array
- input = { "selected[]" => [ "1", "2", "3" ] }
+ input = { "selected[]" => [ "1", "2", "3" ] }
- expected_output = { "selected" => [ "1", "2", "3" ] }
+ expected_output = { "selected" => [ "1", "2", "3" ] }
- assert_equal expected_output, CGIMethods.parse_request_parameters(input)
+ assert_equal expected_output, CGIMethods.parse_request_parameters(input)
+ end
+
+ def test_parse_params_with_non_alphanumeric_name
+ input = { "a/b[c]" => %w(d) }
+ expected = { "a/b" => { "c" => "d" }}
+ assert_equal expected, CGIMethods.parse_request_parameters(input)
+ end
+
+ def test_parse_params_with_single_brackets_in_middle
+ input = { "a/b[c]d" => %w(e) }
+ expected = { "a/b[c]d" => "e" }
+ assert_equal expected, CGIMethods.parse_request_parameters(input)
+ end
+
+ def test_parse_params_with_separated_brackets
+ input = { "a/b@[c]d[e]" => %w(f) }
+ expected = { "a/b@" => { "c]d[e" => "f" }}
+ assert_equal expected, CGIMethods.parse_request_parameters(input)
+ end
+
+ def test_parse_params_with_separated_brackets_and_array
+ input = { "a/b@[c]d[e][]" => %w(f) }
+ expected = { "a/b@" => { "c]d[e" => ["f"] }}
+ assert_equal expected , CGIMethods.parse_request_parameters(input)
+ end
+
+ def test_parse_params_with_unmatched_brackets_and_array
+ input = { "a/b@[c][d[e][]" => %w(f) }
+ expected = { "a/b@" => { "c" => { "d[e" => ["f"] }}}
+ assert_equal expected, CGIMethods.parse_request_parameters(input)
end
end

0 comments on commit 50f333b

Please sign in to comment.