diff --git a/Modules/REST/test/files.cmake b/Modules/REST/test/files.cmake index 7299b4bcce..dc836ff9f9 100644 --- a/Modules/REST/test/files.cmake +++ b/Modules/REST/test/files.cmake @@ -1,4 +1,6 @@ set(MODULE_TESTS + mitkRESTClientHttpLibTest.cpp + mitkRESTServerHttpLibTest.cpp mitkRESTClientTest.cpp mitkRESTServerTest.cpp ) \ No newline at end of file diff --git a/Modules/REST/test/mitkRESTClientHttpLibTest.cpp b/Modules/REST/test/mitkRESTClientHttpLibTest.cpp new file mode 100644 index 0000000000..a33b437a7f --- /dev/null +++ b/Modules/REST/test/mitkRESTClientHttpLibTest.cpp @@ -0,0 +1,39 @@ +/*============================================================================ + +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 + +class mitkRESTClientHttpLibTestSuite : public mitk::TestFixture +{ + CPPUNIT_TEST_SUITE(mitkRESTClientHttpLibTestSuite); + MITK_TEST(GetRequestValidURI_ReturnsExpectedJSON); + CPPUNIT_TEST_SUITE_END(); + +public: + void GetRequestValidURI_ReturnsExpectedJSON() + { + httplib::Client cli("http://eu.httpbin.org"); + auto response = cli.Get("/get"); + if (response->status == 200) + { + auto js = nlohmann::json::parse(response->body); + std::string userAgentWithVersion = js["headers"]["User-Agent"]; + std::string userAgent = userAgentWithVersion.substr(0, userAgentWithVersion.find("/")); + CPPUNIT_ASSERT_MESSAGE("Result is the expected JSON value", userAgent == "cpp-httplib"); + } + } +}; + +MITK_TEST_SUITE_REGISTRATION(mitkRESTClientHttpLib) diff --git a/Modules/REST/test/mitkRESTServerHttpLibTest.cpp b/Modules/REST/test/mitkRESTServerHttpLibTest.cpp new file mode 100644 index 0000000000..03c8f4cb6a --- /dev/null +++ b/Modules/REST/test/mitkRESTServerHttpLibTest.cpp @@ -0,0 +1,96 @@ +/*============================================================================ + +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(); + +public: + void OpenListener_Succeed() + { + httplib::Server svr; + auto serverlambda = [&]() { svr.listen("localhost", 8080); }; + 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, serverlambda); + while (!svr.is_running()) + ; + CPPUNIT_ASSERT_MESSAGE("Server is running", svr.is_running()); + svr.stop(); + } + + void OpenListenerGetRequestSamePath_ReturnExpectedJSON() + { + httplib::Server svr; + auto serverlambda = [&]() { svr.listen("localhost", 8080); }; + 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, serverlambda); + while (!svr.is_running()) + ; + + httplib::Client cli("http://localhost:8080"); + auto response = cli.Get("/msg/1"); + try + { + if (NULL == response) + { + 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]); + } + } + catch (const std::exception &e) + { + svr.stop(); + CPPUNIT_ASSERT_MESSAGE(e.what(), false); + } + svr.stop(); + } + }; + +MITK_TEST_SUITE_REGISTRATION(mitkRESTServerHttpLib)