http.publish.tests.server.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. function equals(a, b) {
  2. return !!(EJSON.stringify(a) === EJSON.stringify(b));
  3. }
  4. Tinytest.add('http-publish - server - test environment', function(test) {
  5. test.isTrue(typeof _publishHTTP !== 'undefined', 'test environment not initialized _publishHTTP');
  6. test.isTrue(typeof HTTP !== 'undefined', 'test environment not initialized HTTP');
  7. test.isTrue(typeof HTTP.publish !== 'undefined', 'test environment not initialized HTTP.publish');
  8. test.isTrue(typeof HTTP.unpublish !== 'undefined', 'test environment not initialized HTTP.unpublish');
  9. test.isTrue(typeof HTTP.publishFormats !== 'undefined', 'test environment not initialized HTTP.publishFormats');
  10. });
  11. list = new Meteor.Collection('list');
  12. console.log('Server url: ' + Meteor.absoluteUrl());
  13. list.allow({
  14. insert: function() { return true; },
  15. update: function() { return true; },
  16. remove: function() { return true; }
  17. });
  18. console.log('Rig publish');
  19. HTTP.publish({collection: list}, function() {
  20. return list.find();
  21. });
  22. // Test custom prefix, too
  23. HTTP.publish({collection: list, name: '/api2/list'}, function() {
  24. return list.find();
  25. });
  26. Meteor.methods({
  27. clearTest: function() {
  28. console.log('Client called clearTest');
  29. // Empty test db
  30. list.remove({});
  31. // Insert one text
  32. list.insert({ text: 'OK' });
  33. // Count
  34. var count = list.find().count();
  35. return !!(count === 1);
  36. },
  37. unmountCustom: function() {
  38. console.log('Client called unmountCustom');
  39. _publishHTTP.unpublish('/api2/list');
  40. return true;
  41. }
  42. });
  43. Tinytest.add('http-publish - server - getMethodHandler', function(test) {
  44. try {
  45. var methodHandler = _publishHTTP.getMethodHandler(list, 'insert');
  46. test.isTrue(typeof methodHandler === 'function', 'expected getMethodHandler to return a function');
  47. } catch(err) {
  48. test.fail(err.message);
  49. }
  50. });
  51. Tinytest.add('http-publish - server - formatHandlers', function(test) {
  52. test.isTrue(typeof _publishHTTP.formatHandlers.json === 'function', 'Cant find formatHandler for json');
  53. var testScope = {
  54. code: 0,
  55. setContentType: function(code) {
  56. this.code = code;
  57. }
  58. };
  59. var resultFormatHandler = _publishHTTP.formatHandlers.json.apply(testScope, [{test:'ok'}]);
  60. test.equal(testScope.code, 'application/json', 'json formatHandler have not set setContentType');
  61. test.equal(resultFormatHandler, '{"test":"ok"}', 'json formatHandler returned a bad result');
  62. });
  63. Tinytest.add('http-publish - server - getPublishScope', function(test) {
  64. var oldScope = {
  65. userId: '1',
  66. params: '2',
  67. query: '3',
  68. oldStuff: 'hmmm'
  69. };
  70. var newScope = _publishHTTP.getPublishScope(oldScope);
  71. test.isUndefined(newScope.oldStuff, 'This oldStuff should not be in the new scope');
  72. test.equal(newScope.userId, '1', 'userId not set in the new scope');
  73. test.equal(newScope.params, '2', 'params not set in the new scope');
  74. test.equal(newScope.query, '3', 'query not set in the new scope');
  75. });
  76. Tinytest.add('http-publish - server - formatResult', function(test) {
  77. var oldScope = {
  78. statusCode: 200,
  79. userId: '1',
  80. params: '2',
  81. query: '3',
  82. oldStuff: 'hmmm',
  83. setStatusCode: function(code) {
  84. this.statusCode = code;
  85. },
  86. code: 0,
  87. setContentType: function(code) {
  88. this.code = code;
  89. }
  90. };
  91. var result = _publishHTTP.formatResult({test: 'ok'}, oldScope);
  92. test.equal(oldScope.code, 'application/json', 'json formatHandler have not set setContentType');
  93. test.equal(result, '{"test":"ok"}', 'json formatHandler returned a bad result');
  94. });
  95. //Test API:
  96. //test.isFalse(v, msg)
  97. //test.isTrue(v, msg)
  98. //test.equalactual, expected, message, not
  99. //test.length(obj, len)
  100. //test.include(s, v)
  101. //test.isNaN(v, msg)
  102. //test.isUndefined(v, msg)
  103. //test.isNotNull
  104. //test.isNull
  105. //test.throws(func)
  106. //test.instanceOf(obj, klass)
  107. //test.notEqual(actual, expected, message)
  108. //test.runId()
  109. //test.exception(exception)
  110. //test.expect_fail()
  111. //test.ok(doc)
  112. //test.fail(doc)
  113. //test.equal(a, b, msg)