[Groonga-commit] groonga/groonga [master] [test][http] support MessagePack test but it fails.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 12月 15日 (木) 12:49:06 JST


Kouhei Sutou	2011-12-15 03:49:06 +0000 (Thu, 15 Dec 2011)

  New Revision: ef2b0c4b4446724e290c639a6ad2db1f2a7af50f

  Log:
    [test][http] support MessagePack test but it fails.
    
    We should put return_code into response on MessagePack
    format in src/groonga.c.

  Modified files:
    test/unit/http/test-http.rb
    test/unit/lib/ruby/groonga-http-test-utils.rb

  Modified: test/unit/http/test-http.rb (+59 -22)
===================================================================
--- test/unit/http/test-http.rb    2011-12-15 02:42:41 +0000 (5ea5a0b)
+++ test/unit/http/test-http.rb    2011-12-15 03:49:06 +0000 (b647f47)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2009  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2009-2011  Kouhei Sutou <kou****@clear*****>
 # Copyright (C) 2009  Yuto HAYAMIZU <y.hay****@gmail*****>
 # Copyright (C) 2009  Ryo Onodera <onode****@clear*****>
 #
@@ -17,56 +17,93 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-class HTTPTest < Test::Unit::TestCase
+module HTTPTests
   include GroongaHTTPTestUtils
 
-  def setup
-    setup_server
-  end
-
-  def teardown
-    teardown_server
+  class << self
+    def included(base)
+      base.setup :setup_server
+      base.teardown :teardown_server
+    end
   end
 
   def test_status
-    response = get(command_path(:status))
-    assert_equal("application/json", response.content_type)
+    response = get(command_path(:status, :output_type => output_type))
+    assert_equal(content_type, response.content_type)
     assert_equal(["alloc_count", "cache_hit_rate", "command_version",
                   "default_command_version", "max_command_version", "n_queries",
                   "starttime", "uptime", "version"],
-                 JSON.parse(response.body)[1].keys.sort)
+                 parse(response.body)[1].keys.sort)
   end
 
   def test_status_command_version
-    response = get(command_path(:status, :command_version => 1))
-    assert_equal("application/json", response.content_type)
-    assert_equal(1, JSON.parse(response.body)[1]["command_version"])
+    response = get(command_path(:status,
+                                :output_type => output_type,
+                                :command_version => 1))
+    assert_equal(content_type, response.content_type)
+    assert_equal(1, parse(response.body)[1]["command_version"])
   end
 
   def test_quit
-    response = get(command_path(:quit))
+    response = get(command_path(:quit, :output_type => output_type))
     assert_success_response(response,
-                            :content_type => "application/json")
+                            :content_type => content_type)
 
     assert_nothing_raised do
-      get(command_path(:quit))
+      get(command_path(:quit, :output_type => output_type))
     end
   end
 
   def test_shutdown
-    response = get(command_path(:shutdown))
+    response = get(command_path(:shutdown, :output_type => output_type))
     assert_success_response(response,
-                            :content_type => "application/json")
+                            :content_type => content_type)
     @groonga_pid = nil
 
     assert_raise(Errno::ECONNREFUSED, EOFError) do
-      get(command_path(:shutdown))
+      get(command_path(:shutdown, :output_type => output_type))
     end
   end
+end
+
+class JSONHTTPTest < Test::Unit::TestCase
+  include HTTPTests
 
   def test_jsonp
-    response = get(command_path(:status, :callback => "func"))
-    assert_equal("application/json", response.content_type)
+    response = get(command_path(:status,
+                                :output_type => output_type,
+                                :callback => "func"))
+    assert_equal(content_type, response.content_type)
     assert_match(/\Afunc\(.+\);\z/, response.body)
   end
+
+  private
+  def output_type
+    nil
+  end
+
+  def content_type
+    "application/json"
+  end
+
+  def parse(response)
+    JSON.parse(response)
+  end
+end
+
+class MessagePackHTTPTest < Test::Unit::TestCase
+  include HTTPTests
+
+  private
+  def output_type
+    "msgpack"
+  end
+
+  def content_type
+    "application/x-msgpack"
+  end
+
+  def parse(response)
+    MessagePack.unpack(response)
+  end
 end

  Modified: test/unit/lib/ruby/groonga-http-test-utils.rb (+15 -3)
===================================================================
--- test/unit/lib/ruby/groonga-http-test-utils.rb    2011-12-15 02:42:41 +0000 (3a99449)
+++ test/unit/lib/ruby/groonga-http-test-utils.rb    2011-12-15 03:49:06 +0000 (f76fdc3)
@@ -227,10 +227,15 @@ module GroongaHTTPTestUtils
         raise "JSON ParserError #{e.message}\nJSON is ...\n" \
               "---\n#{response.body}\n---"
       end
-      if actual[0][0].is_a?(Integer)
-        actual[0][1..2] = [0.0, 0.0]
-        actual[0][4] = nil if actual[0][4]
+      normalize_structured_response(actual)
+    when "application/x-msgpack"
+      begin
+        actual = MessagePack.unpack(response.body)
+      rescue MessagePack::UnpackError => e
+        raise "MessagePack UnpackError #{e.message}\nMessagePack is ...\n" \
+              "---\n#{response.body}\n---"
       end
+      normalize_structured_response(actual)
     when "text/html"
       actual = utf8(response.body)
     when "text/xml"
@@ -285,4 +290,11 @@ module GroongaHTTPTestUtils
       xml
     end
   end
+
+  def normalize_structured_response(response)
+    if response[0][0].is_a?(Integer)
+      response[0][1..2] = [0.0, 0.0]
+      response[0][4] = nil if response[0][4]
+    end
+  end
 end




Groonga-commit メーリングリストの案内
Back to archive index