12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include <gtest/gtest.h>
- #include <json.hpp>
- #include <httplib.h>
- //#define private public
- #include <ProcessorManual.h>
- #include <ModuleHTTP.h>
- #include <ModuleMath.h>
- #include <ModuleSwitch.h>
- #include <math.h>
- #include <thread>
- using namespace mdd;
- using namespace httplib;
- void serverThread()
- {
- Server svr;
- svr.Get("/inputs", [](const Request& req, Response& res) {
- json input_body;
- for (int i = 0; i < 5; ++i) {
- json entity;
- entity["type"] = "INPUT";
- entity["value"] = i;
- input_body.push_back(entity);
- }
- res.set_content(input_body.dump(), "application/json");
- });
- svr.Get("/outputs", [](const Request& req, Response& res) {
- json output_body;
- for (int i = 0; i < 3; ++i) {
- json entity;
- entity["type"] = "OUTPUT";
- entity["value"] = i*2;
- output_body.push_back(entity);
- }
- res.set_content(output_body.dump(), "application/json");
- });
- svr.Get("/status", [](const Request& req, Response& res) {
- json body;
- body["status"] = "ready";
- res.set_content(body.dump(), "application/json");
- });
- svr.Get("/stop", [&](const Request& req, Response& res) {
- svr.stop();
- });
- svr.listen("localhost", 8888);
- }
- TEST(ModuleHTTP, updateLayout_intern){
- std::thread server (serverThread);
- ModuleHTTP module("", "localhost", 8888);
- Client cli("localhost",8888);
- cli.Get("/stop");
- server.join();
- auto inputs_types = module.getInputs();
- auto inputs_ids = module.getInputIDs();
- for (int i = 0; i < 5; ++i) {
- EXPECT_EQ(inputs_types[i], "INPUT");
- EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
- }
- auto outputs_types = module.getOutputs();
- auto outputs_ids = module.getOutputIDs();
- for (int i = 0; i < 3; ++i) {
- EXPECT_EQ(outputs_types[i], "OUTPUT");
- EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i * 2);
- }
- }
- TEST(ModuleHTTP, updateLayout_extern){
- ModuleHTTP module("../../../lib/test/server/server.py","localhost",8888);
- auto inputs_types = module.getInputs();
- auto inputs_ids = module.getInputIDs();
- for(int i = 0; i < 5; ++i){
- EXPECT_EQ(inputs_types[i], "INPUT");
- EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
- }
- auto outputs_types = module.getOutputs();
- auto outputs_ids = module.getOutputIDs();
- for(int i = 0; i < 3; ++i){
- EXPECT_EQ(outputs_types[i], "OUTPUT");
- EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i*2);
- }
- Client cli("localhost",8888);
- cli.Get("/stop");
- }
|