test_ModuleHTTP.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. std::cout<<"RECIVED: /inputs" <<std::endl;
  25. res.set_content(input_body.dump(), "application/json");
  26. auto data = new std::string(input_body.dump());
  27. res.set_content_provider(
  28. data->size(), // Content length
  29. [data](size_t offset, size_t length, DataSink &sink) {
  30. const auto &d = *data;
  31. sink.write(&d[offset], std::min(length, length));
  32. return true; // return 'false' if you want to cancel the process.
  33. },
  34. [data] { delete data; });
  35. });
  36. svr.Get("/outputs", [](const Request& req, Response& res) {
  37. json output_body;
  38. for (int i = 0; i < 3; ++i) {
  39. json entity;
  40. entity["type"] = "OUTPUT";
  41. entity["value"] = i*2;
  42. output_body.push_back(entity);
  43. }
  44. std::cout<<"RECIVED: /outputs" <<std::endl;
  45. res.set_content(output_body.dump(), "application/json");
  46. });
  47. svr.Get("/stop", [&](const Request& req, Response& res) {
  48. svr.stop();
  49. });
  50. svr.listen("localhost", 8888);
  51. }
  52. TEST(ModuleHTTP, updateLayout_intern){
  53. std::thread server (serverThread);
  54. ModuleHTTP module("", "localhost", 8888);
  55. Client cli("localhost",8888);
  56. cli.Get("/stop");
  57. server.join();
  58. auto inputs_types = module.getInputs();
  59. auto inputs_ids = module.getInputIDs();
  60. for (int i = 0; i < 5; ++i) {
  61. EXPECT_EQ(inputs_types[i], "INPUT");
  62. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
  63. std::cout << module.getInput(inputs_ids[i])->getValue().dump() << std::endl;
  64. }
  65. auto outputs_types = module.getOutputs();
  66. auto outputs_ids = module.getOutputIDs();
  67. for (int i = 0; i < 3; ++i) {
  68. EXPECT_EQ(outputs_types[i], "OUTPUT");
  69. EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i * 2);
  70. std::cout << module.getOutput(outputs_ids[i])->getValue().dump() << std::endl;
  71. }
  72. }
  73. TEST(ModuleHTTP, updateLayout_extern){
  74. ModuleHTTP module("../../../lib/test/server/server.py","localhost",8888);
  75. auto inputs_types = module.getInputs();
  76. auto inputs_ids = module.getInputIDs();
  77. for(int i = 0; i < 5; ++i){
  78. EXPECT_EQ(inputs_types[i], "INPUT");
  79. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
  80. std::cout << module.getInput(inputs_ids[i])->getValue().dump() << std::endl;
  81. }
  82. auto outputs_types = module.getOutputs();
  83. auto outputs_ids = module.getOutputIDs();
  84. for(int i = 0; i < 3; ++i){
  85. EXPECT_EQ(outputs_types[i], "OUTPUT");
  86. EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i*2);
  87. std::cout << module.getOutput(outputs_ids[i])->getValue().dump() << std::endl;
  88. }
  89. Client cli("localhost",8888);
  90. cli.Get("/stop");
  91. }