test_ModuleHTTP.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. using namespace mdd;
  11. using namespace httplib;
  12. TEST(ModuleHTTP, updateLayout_create){
  13. Server svr;
  14. svr.Get("/inputs", [](const Request& req, Response& res) {
  15. json input_body;
  16. for (int i = 0; i < 5; ++i) {
  17. json entity;
  18. entity["type"] = "INPUT";
  19. entity["value"] = i;
  20. input_body.push_back(entity);
  21. }
  22. std::cout<<"RECIVED: /inputs" <<std::endl;
  23. res.set_content(input_body.dump(), "application/json");
  24. auto data = new std::string(input_body.dump());
  25. res.set_content_provider(
  26. data->size(), // Content length
  27. [data](size_t offset, size_t length, DataSink &sink) {
  28. const auto &d = *data;
  29. sink.write(&d[offset], std::min(length, length));
  30. return true; // return 'false' if you want to cancel the process.
  31. },
  32. [data] { delete data; });
  33. });
  34. svr.Get("/outputs", [](const Request& req, Response& res) {
  35. json output_body;
  36. for (int i = 0; i < 3; ++i) {
  37. json entity;
  38. entity["type"] = "OUTPUT";
  39. entity["value"] = i*2;
  40. output_body.push_back(entity);
  41. }
  42. std::cout<<"RECIVED: /outputs" <<std::endl;
  43. res.set_content(output_body.dump(), "application/json");
  44. });
  45. svr.listen("localhost",8888);
  46. json input_body;
  47. for (int i = 0; i < 5; ++i) {
  48. json entity;
  49. entity["type"] = "INPUT";
  50. entity["value"] = i;
  51. input_body.push_back(entity);
  52. }
  53. ModuleHTTP module("","localhost",8888);
  54. auto inputs_types = module.getInputs();
  55. auto inputs_ids = module.getInputIDs();
  56. for(int i = 0; i < 5; ++i){
  57. EXPECT_EQ(inputs_types[i], "INPUT");
  58. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
  59. }
  60. auto outputs_types = module.getOutputs();
  61. auto outputs_ids = module.getOutputIDs();
  62. for(int i = 0; i < 3; ++i){
  63. EXPECT_EQ(outputs_types[i], "OUTPUT");
  64. EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i*2);
  65. }
  66. }