diff --git a/Modules/REST/test/mitkRESTServerHttpLibTest.cpp b/Modules/REST/test/mitkRESTServerHttpLibTest.cpp index 1f823f6549..e77d14da3b 100644 --- a/Modules/REST/test/mitkRESTServerHttpLibTest.cpp +++ b/Modules/REST/test/mitkRESTServerHttpLibTest.cpp @@ -1,94 +1,100 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include #include #include #include #include #include #include class mitkRESTServerHttpLibTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkRESTServerHttpLibTestSuite); MITK_TEST(OpenListener_Succeed); MITK_TEST(OpenListenerGetRequestSamePath_ReturnExpectedJSON); CPPUNIT_TEST_SUITE_END(); std::vector> m_ft; public: void OpenListener_Succeed() { httplib::Server svr; svr.Get("/get", [](const httplib::Request &, httplib::Response &res) { res.set_content("Hello World from MITK!", "text/plain"); }); std::future ft = std::async(std::launch::async, [&]() { svr.listen("localhost", 8080); }); while (!svr.is_running()) ; CPPUNIT_ASSERT_MESSAGE("Server is running", svr.is_running()); svr.stop(); } void OpenListenerGetRequestSamePath_ReturnExpectedJSON() { httplib::Server svr; std::map msgdb; msgdb[0] = "hello_MITK"; svr.Get(R"(/msg/(\d+))", [&](const httplib::Request &req, httplib::Response &res) { auto n = req.matches[1]; nlohmann::json jRes; try { jRes["id"] = std::stoi(n); jRes["msg"] = msgdb[std::stoi(n)]; res.set_content(jRes.dump(), "application/json"); } catch (const std::exception &) { res.set_content("Cannot find the requested message.", "text/plain"); } }); std::future ft = std::async(std::launch::async, [&]() { svr.listen("localhost", 8080); }); while (!svr.is_running()) ; httplib::Client cli("http://localhost:8080"); - auto response = cli.Get("/msg/0"); try { - if (NULL == response) + if (auto response = cli.Get("/msg/0")) { - CPPUNIT_ASSERT_MESSAGE("A connection duty cannot be established", false); + if (response->status == 200) + { + auto js = nlohmann::json::parse(response->body); + std::string msg = js["msg"]; + CPPUNIT_ASSERT_MESSAGE("Result is the expected JSON value", msg == msgdb[0]); + } + else + { + CPPUNIT_ASSERT_MESSAGE("A connection cannot be established", false); + } } - if (response->status == 200) + else { - auto js = nlohmann::json::parse(response->body); - std::string msg = js["msg"]; - CPPUNIT_ASSERT_MESSAGE("Result is the expected JSON value", msg == msgdb[0]); + CPPUNIT_ASSERT_MESSAGE("A connection cannot be established", false); } } catch (const std::exception &e) { svr.stop(); CPPUNIT_ASSERT_MESSAGE(e.what(), false); } svr.stop(); } }; MITK_TEST_SUITE_REGISTRATION(mitkRESTServerHttpLib)