handlerService.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var should = require('should');
  2. var HandlerService = require('../../lib/common/service/handlerService');
  3. var mockApp = {
  4. serverType: 'connector',
  5. get: function(key) {
  6. return this[key];
  7. }
  8. };
  9. var mockSession = {
  10. exportSession: function() {
  11. return this;
  12. }
  13. };
  14. var mockMsg = {key: 'some request message'};
  15. var mockRouteRecord = {serverType: 'connector', handler: 'testHandler', method: 'testMethod'};
  16. describe('handler service test', function() {
  17. describe('handle', function() {
  18. it('should dispatch the request to the handler if the route match current server type', function(done) {
  19. var invoke1Count = 0, invoke2Count = 0;
  20. // mock datas
  21. var mockHandlers = {
  22. testHandler: {
  23. testMethod: function(msg, session, next) {
  24. invoke1Count++;
  25. msg.should.eql(mockMsg);
  26. next();
  27. }
  28. },
  29. test2Handler: {
  30. testMethod: function(msg, session, next) {
  31. invoke2Count++;
  32. next();
  33. }
  34. }
  35. };
  36. var mockOpts = {};
  37. var service = new HandlerService(mockApp, mockOpts);
  38. service.handlerMap = {connector: mockHandlers};
  39. service.handle(mockRouteRecord, mockMsg, mockSession, function() {
  40. invoke1Count.should.equal(1);
  41. invoke2Count.should.equal(0);
  42. done();
  43. });
  44. });
  45. it('should return an error if can not find the appropriate handler locally', function(done) {
  46. var mockHandlers = {};
  47. var mockOpts = {};
  48. var service = new HandlerService(mockApp, mockOpts);
  49. service.handlerMap = {connector: mockHandlers};
  50. service.handle(mockRouteRecord, mockMsg, mockSession, function(err) {
  51. should.exist(err);
  52. done();
  53. });
  54. });
  55. });
  56. });