e2e.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var http = require('http');
  2. var verbose = false;
  3. if (process.argv.length >= 2) {
  4. verbose = process.argv[2] === "v" || process.argv[2] === "verbose";
  5. }
  6. function _get(host, path, callback) {
  7. return http.get({
  8. host: "127.0.0.1",
  9. port: "3000",
  10. path: "/" + path + "/",
  11. }, function(response) {
  12. var body = '';
  13. response.on('data', function(d) {
  14. body += d;
  15. });
  16. response.on('end', function() {
  17. callback({
  18. result: JSON.parse(body)
  19. });
  20. });
  21. });
  22. };
  23. function _post(host, path, data, callback) {
  24. var options = {
  25. host: "127.0.0.1",
  26. port: "3000",
  27. path: "/"+path+"/",
  28. method: 'POST',
  29. headers: {
  30. 'Content-Type': 'application/json'
  31. }
  32. };
  33. var postReq = http.request(options, function(res) {
  34. var body = '';
  35. res.on('data', function(d) {
  36. body += d;
  37. });
  38. res.on('end', function() {
  39. callback({
  40. result: JSON.parse(body)
  41. });
  42. });
  43. });
  44. postReq.write(JSON.stringify(data));
  45. postReq.end();
  46. };
  47. function client(path) {
  48. var ip = "localhost:3000";
  49. return {
  50. get: function(callback) {
  51. _get(ip, path, callback);
  52. },
  53. post: function(data, callback) {
  54. _post(ip, path, data, callback);
  55. }
  56. }
  57. }
  58. function result(pass, data, msg, raw) {
  59. this.pass = pass;
  60. this.data = data;
  61. this.msg = msg;
  62. this.raw = raw;
  63. this.format = function() {
  64. return (this.pass ? "Success ::" : "Fail ::" ) +this.msg+ ":::"+
  65. (verbose ? ("data: " + JSON.stringify(this.data)) +
  66. "raw: " + JSON.stringify(this.raw) : "");
  67. };
  68. }
  69. function testInsert(data, svc_name, cb) {
  70. var svc = client(svc_name);
  71. if (data.length > 0) {
  72. var results = [];
  73. for (var i=0;i<data.length;i++) {
  74. svc.post(data[i], function (res) {
  75. var test_result = new result(res.result.id.length > 0, res.result, "Put " + svc_name, res)
  76. results.push(test_result)
  77. if (results.length == data.length) {
  78. cb({results:results})
  79. }
  80. })
  81. }
  82. } else {
  83. svc.post(data, function (res) {
  84. var test_result = new result(res.result.id.length > 0, res.result, "Put " + svc_name, res)
  85. cb(test_result)
  86. })
  87. }
  88. }
  89. function testGet(svc_name, key, cb) {
  90. var path = key.length == 0 ? svc_name+'s' : (svc_name + "/" + key )
  91. var svc = client(path);
  92. svc.get(function (res) {
  93. var pass = res.result.length > 0 || (res.result.id.length > 0 && res.result.id == key)
  94. var test_result = new result(pass, res.result, "Get " + svc_name, res)
  95. cb(test_result)
  96. })
  97. }
  98. var user = {"id":"","name":"jake","lname":"jakeson","dob":"147324123","password":"super secret", "email":"jake@jake.com","phone":"1239567","active":1,"products":[]};
  99. var product = {"id":"", "name":"MOUNTAIN DEWWWW", "price":"", "images":[]};
  100. var products = [product, {"id":"", "name":"CRACK ROX", "price":"", "images":[]}];
  101. var image = {"id":"", "name":"Super dope haircut", "url":"http://i.imgur.com/oOtZe.jpg"};
  102. var price = {"id":"", "user_id":"xxx", "product_id":"zzzz", "currency_code":"USD", "price":34};
  103. var appointment = {"id":"", "name":"xxx", "time":147324123, "confirmed":true, "user_id":"xxxx", "vendor_id":"xxxx", "product_id":"xxxx", "price_id":"xxxx"};
  104. var session = {"id":"", "user_id":"1", "data":"{root:1}","created":""};
  105. function T(res) { for (var i=0;i<res.length;i++) { console.log(res[i].format()); }}
  106. function TS(svc_name, data) {
  107. return function(callback) {
  108. testInsert(data, svc_name, function (insertResult) {
  109. if (insertResult.results) {
  110. for (var i = 0; i < insertResult.results.length; i++) {
  111. testGet(svc_name, insertResult.results[i].data.id, function (getResult) {
  112. testGet(svc_name, "", function (getManyResult) {
  113. if (i == insertResult.results.length) {
  114. callback(insertResult.results.concat([getResult, getManyResult]))
  115. }
  116. });
  117. });
  118. }
  119. } else {
  120. testGet(svc_name, insertResult.data.id, function (getResult) {
  121. testGet(svc_name, "", function (getManyResult) {
  122. callback([insertResult, getResult, getManyResult])
  123. });
  124. });
  125. }
  126. });
  127. }
  128. }
  129. http.get({
  130. host: "127.0.0.1",
  131. port: "3000",
  132. path: "/reset",
  133. }, function(response) {
  134. TS("user", user)(T);
  135. TS("image", image)(T);
  136. TS("price", price)(T);
  137. TS("product", product)(T);
  138. TS("product", products)(T);
  139. TS("appointment", appointment)(T);
  140. TS("session", session)(T);
  141. });