http.methods.tests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function equals(a, b) {
  2. return EJSON.stringify(a) === EJSON.stringify(b);
  3. }
  4. Tinytest.add('http-methods - test environment', function(test) {
  5. test.isTrue(typeof _methodHTTP !== 'undefined', 'test environment not initialized _methodHTTP');
  6. test.isTrue(typeof HTTP !== 'undefined', 'test environment not initialized HTTP');
  7. test.isTrue(typeof HTTP.methods !== 'undefined', 'test environment not initialized HTTP.methods');
  8. });
  9. Tinytest.add('http-methods - nameFollowsConventions', function(test) {
  10. test.isFalse(_methodHTTP.nameFollowsConventions(), 'Tested methods naming convention 1');
  11. test.isFalse(_methodHTTP.nameFollowsConventions(''), 'Tested methods naming convention 2');
  12. test.isFalse(_methodHTTP.nameFollowsConventions({}), 'Tested methods naming convention 3');
  13. test.isFalse(_methodHTTP.nameFollowsConventions([1]), 'Tested methods naming convention 4');
  14. test.isFalse(_methodHTTP.nameFollowsConventions(-1), 'Tested methods naming convention 5');
  15. test.isFalse(_methodHTTP.nameFollowsConventions(1), 'Tested methods naming convention 6');
  16. test.isFalse(_methodHTTP.nameFollowsConventions(0.1), 'Tested methods naming convention 7');
  17. test.isFalse(_methodHTTP.nameFollowsConventions(-0.1), 'Tested methods naming convention 8');
  18. test.isTrue(_methodHTTP.nameFollowsConventions('/test/test'), 'Tested methods naming convention leading slash');
  19. test.isTrue(_methodHTTP.nameFollowsConventions('test/test'), 'Tested methods naming convention');
  20. });
  21. Tinytest.add('http-methods - getNameList', function(test) {
  22. test.equal(EJSON.stringify(_methodHTTP.getNameList()), '[]', 'Name list failed');
  23. test.equal(EJSON.stringify(_methodHTTP.getNameList('')), '[]', 'Name list failed');
  24. test.equal(EJSON.stringify(_methodHTTP.getNameList('/')), '[]', 'Name list failed');
  25. test.equal(EJSON.stringify(_methodHTTP.getNameList('//')), '["",""]', 'Name list failed');
  26. test.equal(EJSON.stringify(_methodHTTP.getNameList('/1/')), '["1",""]', 'Name list failed');
  27. test.equal(EJSON.stringify(_methodHTTP.getNameList('/1/2')), '["1","2"]', 'Name list failed');
  28. test.equal(EJSON.stringify(_methodHTTP.getNameList('/1/:name/2')), '["1",":name","2"]', 'Name list failed');
  29. test.equal(EJSON.stringify(_methodHTTP.getNameList('/1//2')), '["1","","2"]', 'Name list failed');
  30. });
  31. Tinytest.add('http-methods - createObject', function(test) {
  32. test.equal(EJSON.stringify(_methodHTTP.createObject()), '{}', 'createObject failed');
  33. test.equal(EJSON.stringify(_methodHTTP.createObject(2, 4)), '{}', 'createObject failed');
  34. test.equal(EJSON.stringify(_methodHTTP.createObject(['foo'], [])), '{"foo":""}', 'createObject failed');
  35. test.equal(EJSON.stringify(_methodHTTP.createObject(['foo'], ['bar'])), '{"foo":"bar"}', 'createObject failed');
  36. test.equal(EJSON.stringify(_methodHTTP.createObject(['foo'], [3])), '{"foo":"3"}', 'createObject failed');
  37. test.equal(EJSON.stringify(_methodHTTP.createObject(['foo'], ['bar', 3])), '{"foo":"bar"}', 'createObject failed');
  38. test.equal(EJSON.stringify(_methodHTTP.createObject(['foo', 'foo'], ['bar', 3])), '{"foo":"3"}', 'createObject failed');
  39. test.equal(EJSON.stringify(_methodHTTP.createObject([''], ['bar', 3])), '{"":"bar"}', 'createObject failed');
  40. test.equal(EJSON.stringify(_methodHTTP.createObject(['', ''], ['bar', 3])), '{"":"3"}', 'createObject failed');
  41. });
  42. Tinytest.add('http-methods - addToMethodTree', function(test) {
  43. var original = _methodHTTP.methodTree;
  44. _methodHTTP.methodTree = {};
  45. _methodHTTP.addToMethodTree('login');
  46. test.equal(EJSON.stringify(_methodHTTP.methodTree), '{"login":{":ref":{"name":"/login/","params":[]}}}', 'addToMethodTree failed');
  47. _methodHTTP.methodTree = {};
  48. _methodHTTP.addToMethodTree('/foo/bar');
  49. test.equal(EJSON.stringify(_methodHTTP.methodTree), '{"foo":{"bar":{":ref":{"name":"/foo/bar/","params":[]}}}}', 'addToMethodTree failed');
  50. _methodHTTP.methodTree = {};
  51. _methodHTTP.addToMethodTree('/foo/:name/bar');
  52. test.equal(EJSON.stringify(_methodHTTP.methodTree), '{"foo":{":value":{"bar":{":ref":{"name":"/foo/:value/bar/","params":["name"]}}}}}', 'addToMethodTree failed');
  53. _methodHTTP.addToMethodTree('/foo/:name/bar');
  54. test.equal(EJSON.stringify(_methodHTTP.methodTree), '{"foo":{":value":{"bar":{":ref":{"name":"/foo/:value/bar/","params":["name"]}}}}}', 'addToMethodTree failed');
  55. _methodHTTP.addToMethodTree('/foo/name/bar');
  56. test.equal(EJSON.stringify(_methodHTTP.methodTree), '{"foo":{":value":{"bar":{":ref":{"name":"/foo/:value/bar/","params":["name"]}}},"name":{"bar":{":ref":{"name":"/foo/name/bar/","params":[]}}}}}', 'addToMethodTree failed');
  57. _methodHTTP.methodTree = original;
  58. });
  59. Tinytest.add('http-methods - getMethod', function(test) {
  60. // Basic tests
  61. test.equal(EJSON.stringify(_methodHTTP.getMethod('')), 'null', 'getMethod failed');
  62. test.equal(EJSON.stringify(_methodHTTP.getMethod('//')), 'null', 'getMethod failed');
  63. _methodHTTP.addToMethodTree('login');
  64. test.equal(EJSON.stringify(_methodHTTP.getMethod('login')), '{"name":"/login/","params":{}}', 'getMethod failed');
  65. test.equal(EJSON.stringify(_methodHTTP.getMethod('/login')), '{"name":"/login/","params":{}}', 'getMethod failed');
  66. test.equal(EJSON.stringify(_methodHTTP.getMethod('login/')), 'null', 'getMethod failed');
  67. test.equal(EJSON.stringify(_methodHTTP.getMethod('/login/')), 'null', 'getMethod failed');
  68. test.equal(EJSON.stringify(_methodHTTP.getMethod('login/test')), 'null', 'getMethod failed');
  69. _methodHTTP.addToMethodTree('/login/');
  70. test.equal(EJSON.stringify(_methodHTTP.getMethod('login')), '{"name":"/login/","params":{}}', 'getMethod failed');
  71. //
  72. _methodHTTP.addToMethodTree('/login/foo');
  73. test.equal(EJSON.stringify(_methodHTTP.getMethod('login/foo')), '{"name":"/login/foo/","params":{}}', 'getMethod failed');
  74. _methodHTTP.addToMethodTree('/login/:name/foo');
  75. test.equal(EJSON.stringify(_methodHTTP.getMethod('login/bar/foo')), '{"name":"/login/:value/foo/","params":{"name":"bar"}}', 'getMethod failed');
  76. test.equal(EJSON.stringify(_methodHTTP.getMethod('login/foo')), '{"name":"/login/foo/","params":{}}', 'getMethod failed');
  77. });
  78. //Test API:
  79. //test.isFalse(v, msg)
  80. //test.isTrue(v, msg)
  81. //test.equal(actual, expected, message, not)
  82. //test.length(obj, len)
  83. //test.include(s, v)
  84. //test.isNaN(v, msg)
  85. //test.isUndefined(v, msg)
  86. //test.isNotNull
  87. //test.isNull
  88. //test.throws(func)
  89. //test.instanceOf(obj, klass)
  90. //test.notEqual(actual, expected, message)
  91. //test.runId()
  92. //test.exception(exception)
  93. //test.expect_fail()
  94. //test.ok(doc)
  95. //test.fail(doc)