null+****@clear*****
null+****@clear*****
2012年 6月 5日 (火) 17:37:25 JST
Haruka Yoshihara 2012-06-05 17:37:25 +0900 (Tue, 05 Jun 2012) New Revision: d08c4b5ae94ca06f85c2fb5e52b25868855ab3dd Log: Translater: Fix translate_command to escape http command Modified files: lib/groonga/tester.rb test/test-executor.rb Modified: lib/groonga/tester.rb (+7 -11) =================================================================== --- lib/groonga/tester.rb 2012-06-05 16:47:53 +0900 (3bfa687) +++ lib/groonga/tester.rb 2012-06-05 17:37:25 +0900 (77a2cb0) @@ -620,7 +620,8 @@ module Groonga command = command.chomp return "" if command.empty? - return command if command =~ /\A(?!\s+)\W/ + return command if command =~ /\A\s*\#/ + return Rack::Utils.escape(command) if command =~ /\A(?!\s+)\W/ now_command, *arguments = Shellwords.split(command) @@ -689,16 +690,11 @@ module Groonga end end - def build_http_command(now_command, arguments) - translated_command = "/d/#{now_command}" - unless arguments.empty? - translated_command << "?" - query = arguments.collect do |parameter, value| - "#{parameter}=#{value}" - end - translated_command << query.join("&") - end - translated_command + def build_http_command(command, arguments) + http_command = "/d/#{command}" + query = Rack::Utils.build_query(arguments) + http_command << "?#{query}" unless query.empty? + http_command end end Modified: test/test-executor.rb (+34 -11) =================================================================== --- test/test-executor.rb 2012-06-05 16:47:53 +0900 (760f09b) +++ test/test-executor.rb 2012-06-05 17:37:25 +0900 (117bfa9) @@ -14,6 +14,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. require "stringio" +require "rack/utils" require "groonga/tester" class TestExecutor < Test::Unit::TestCase @@ -59,25 +60,29 @@ class TestExecutor < Test::Unit::TestCase def test_command command = "table_create Site TABLE_HASH_KEY ShortText" - expected_command = - "/d/table_create?name=Site&flags=TABLE_HASH_KEY&key_type=ShortText" + arguments = { + "name" => "Site", + "flags" => "TABLE_HASH_KEY", + "key_type" => "ShortText", + } actual_command = translate(command) + expected_command = build_http_command("table_create", arguments) assert_equal(expected_command, actual_command) end def test_command_with_argument_name command = "select --table Sites" - expected_command = "/d/select?table=Sites" actual_command = translate(command) + expected_command = build_http_command("select", "table" => "Sites") assert_equal(expected_command, actual_command) end def test_command_without_arguments command = "dump" - expected_command = "/d/dump" actual_command = translate(command) + expected_command = build_http_command(command, {}) assert_equal(expected_command, actual_command) end @@ -91,14 +96,14 @@ class TestExecutor < Test::Unit::TestCase ["razil","http://razil.jp/"] ] EOF - load_values = load_values.chomp commands = "#{load_command}\n#{load_values}" - - expected_command = "/d/load?table=Sites&values=\n#{load_values}" actual_commands = commands.lines.collect do |line| translate(line) end + expected_command = build_http_command("load", "table" => "Sites") + expected_command << load_values_query(load_values) + assert_equal(expected_command, actual_commands.join("\n")) end @@ -109,21 +114,25 @@ EOF {"_key": "ruby", "uri": "http://ruby-lang.org/"} ] EOF - load_values = load_values.chomp commands = "#{load_command}\n#{load_values}" - - expected_command = "/d/load?table=Sites&values=\n#{load_values}" actual_commands = commands.lines.collect do |line| translate(line) end + expected_command = build_http_command("load", "table" => "Sites") + expected_command << load_values_query(load_values) + assert_equal(expected_command, actual_commands.join("\n")) end def test_command_with_single_quote command = "select Sites --output_columns '_key, uri'" - expected_command = "/d/select?table=Sites&output_columns=_key,uri" + arguments = { + "table" => "Sites", + "output_columns" => "_key,uri", + } actual_command = translate(command) + expected_command = build_http_command("select", arguments) assert_equal(expected_command, actual_command) end @@ -139,5 +148,19 @@ EOF def translate(command) @translater.translate_command(command) end + + def build_http_command(command, arguments) + http_command = "/d/#{command}" + query = Rack::Utils.build_query(arguments) + http_command << "?#{query}" unless query.empty? + http_command + end + + def load_values_query(values) + escaped_values = values.lines.collect do |line| + "#{Rack::Utils.escape(line.chomp)}" + end + "&values=\n#{escaped_values.join("\n")}" + end end end