[Groonga-commit] groonga/express-kotoumi [master] test: Fix tests for REST adaptor

Back to archive index

YUKI Hiroshi null+****@clear*****
Tue Feb 5 19:04:07 JST 2013


YUKI Hiroshi	2013-02-05 19:04:07 +0900 (Tue, 05 Feb 2013)

  New Revision: fb81480feac19a4dab199572360c59f58fa91188
  https://github.com/groonga/express-kotoumi/commit/fb81480feac19a4dab199572360c59f58fa91188

  Log:
    test: Fix tests for REST adaptor

  Modified files:
    test/frontend-rest-adaptor.test.js

  Modified: test/frontend-rest-adaptor.test.js (+71 -19)
===================================================================
--- test/frontend-rest-adaptor.test.js    2013-02-05 19:01:16 +0900 (dcdf2e1)
+++ test/frontend-rest-adaptor.test.js    2013-02-05 19:04:07 +0900 (f58d656)
@@ -1,5 +1,6 @@
 var assert = require('chai').assert;
 var nodemock = require('nodemock');
+var Deferred = require('jsdeferred').Deferred;
 
 var utils = require('./test-utils');
 
@@ -55,6 +56,12 @@ suite('REST API', function() {
       ]
     });
 
+    registeredCommands = registeredCommands.map(function(command) {
+      return {
+        command: command.command,
+        definition: command.definition
+      };
+    });
     assert.deepEqual(registeredCommands,
                      [{ command: 'search',
                         definition: restCommands.search },
@@ -81,29 +88,60 @@ suite('REST API', function() {
       api: defineCommand('api', '/path/to/api')
     };
 
+    var connection;
+    var application;
     var server;
+
+    setup(function(done) {
+      connection = utils.createMockedBackendConnection();
+      application = express();
+      utils.setupServer(application)
+        .next(function(newServer) {
+          server = newServer;
+          done();
+        });
+    });
+
     teardown(function() {
+      if (connection) {
+        utils.readyToDestroyMockedConnection(connection);
+        connection = undefined;
+      }
       if (server) {
         server.close();
+        server = undefined;
       }
-      server = undefined;
     });
 
     test('to the document root', function(done) {
-      var fakeConnection = utils.createStubbedBackendConnection();
-      var application = express();
+      var onReceive = {};
+      connection
+        .mock('emitMessage')
+          .takes('api', 'api requested', function() {}, { 'timeout': null })
+          .ctrl(2, onReceive);
+
       restAdaptor.register(application, {
         prefix:     '',
-        connection: fakeConnection,
+        connection: connection,
         plugins:    [testPlugin]
       });
-      utils.setupServer(application)
-        .next(function(newServer) {
-          server = newServer;
+
+      var responseBody;
+      Deferred
+        .wait(0.01)
+        .next(function() {
+          utils.get('/path/to/api')
+            .next(function(response) {
+              responseBody = response.body;
+            });
+        })
+        .wait(0.01)
+        .next(function() {
+          onReceive.trigger(null, { body: 'API OK?' });
         })
-        .get('/path/to/api')
-        .next(function(response) {
-          assert.equal(response.body, 'api OK');
+        .wait(0.01)
+        .next(function() {
+          assert.equal(responseBody, 'api OK');
           done();
         })
         .error(function(error) {
@@ -112,20 +150,34 @@ suite('REST API', function() {
     });
 
     test('under specified path', function(done) {
-      var fakeConnection = utils.createStubbedBackendConnection();
-      var application = express();
+      var onReceive = {};
+      connection
+        .mock('emitMessage')
+          .takes('api', 'api requested', function() {}, { 'timeout': null })
+          .ctrl(2, onReceive);
+
       restAdaptor.register(application, {
         prefix:     '/path/to/kotoumi',
-        connection: fakeConnection,
+        connection: connection,
         plugins:    [testPlugin]
       });
-      utils.setupServer(application)
-        .next(function(newServer) {
-          server = newServer;
+
+      var responseBody;
+      Deferred
+        .wait(0.01)
+        .next(function() {
+          utils.get('/path/to/kotoumi/path/to/api')
+            .next(function(response) {
+              responseBody = response.body;
+            });
+        })
+        .wait(0.01)
+        .next(function() {
+          onReceive.trigger(null, { body: 'API OK?' });
         })
-        .get('/path/to/kotoumi/path/to/api')
-        .next(function(response) {
-          assert.equal(response.body, 'api OK');
+        .wait(0.01)
+        .next(function() {
+          assert.equal(responseBody, 'api OK');
           done();
         })
         .error(function(error) {
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index