test_ModuleHTTP.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. #include <httplib.h>
  4. //#define private public
  5. #include <ProcessorManual.h>
  6. #include <ModuleHTTP.h>
  7. #include <ModuleMath.h>
  8. #include <ModuleSwitch.h>
  9. #include <math.h>
  10. #include <thread>
  11. using namespace mdd;
  12. using namespace httplib;
  13. void serverThread()
  14. {
  15. Server svr;
  16. svr.Get("/inputs", [](const Request& req, Response& res) {
  17. json input_body;
  18. for (int i = 0; i < 5; ++i) {
  19. json entity;
  20. entity["type"] = "INPUT";
  21. entity["value"] = i;
  22. input_body.push_back(entity);
  23. }
  24. res.set_content(input_body.dump(), "application/json");
  25. });
  26. svr.Get("/outputs", [](const Request& req, Response& res) {
  27. json output_body;
  28. for (int i = 0; i < 3; ++i) {
  29. json entity;
  30. entity["type"] = "OUTPUT";
  31. entity["value"] = i*2;
  32. output_body.push_back(entity);
  33. }
  34. res.set_content(output_body.dump(), "application/json");
  35. });
  36. svr.Get("/status", [](const Request& req, Response& res) {
  37. json body;
  38. body["status"] = "ready";
  39. res.set_content(body.dump(), "application/json");
  40. });
  41. svr.Get("/stop", [&](const Request& req, Response& res) {
  42. svr.stop();
  43. });
  44. svr.listen("localhost", 8888);
  45. }
  46. TEST(ModuleHTTP, updateLayout_intern){
  47. std::thread server (serverThread);
  48. ModuleHTTP module("", "localhost", 8888);
  49. Client cli("localhost",8888);
  50. cli.Get("/stop");
  51. server.join();
  52. auto inputs_types = module.getInputs();
  53. auto inputs_ids = module.getInputIDs();
  54. for (int i = 0; i < 5; ++i) {
  55. EXPECT_EQ(inputs_types[i], "INPUT");
  56. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
  57. }
  58. auto outputs_types = module.getOutputs();
  59. auto outputs_ids = module.getOutputIDs();
  60. for (int i = 0; i < 3; ++i) {
  61. EXPECT_EQ(outputs_types[i], "OUTPUT");
  62. EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i * 2);
  63. }
  64. }
  65. TEST(ModuleHTTP, updateLayout_extern){
  66. ModuleHTTP module("../../../lib/test/server/server.py","localhost",8888);
  67. auto inputs_types = module.getInputs();
  68. auto inputs_ids = module.getInputIDs();
  69. for(int i = 0; i < 5; ++i){
  70. EXPECT_EQ(inputs_types[i], "INPUT");
  71. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
  72. }
  73. auto outputs_types = module.getOutputs();
  74. auto outputs_ids = module.getOutputIDs();
  75. for(int i = 0; i < 3; ++i){
  76. EXPECT_EQ(outputs_types[i], "OUTPUT");
  77. EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i*2);
  78. }
  79. Client cli("localhost",8888);
  80. cli.Get("/stop");
  81. }