# HG changeset patch # User Sebastien Jodogne # Date 1730829057 -3600 # Node ID 1dd0356099f031a81003a2be3781d167eb16b295 # Parent 3ddd1b0231e9bc8ab5fa7efdb36cdc47c08d02e0 fix unit tests Lua.Http and HttpClient.Basic diff -r 3ddd1b0231e9 -r 1dd0356099f0 OrthancFramework/Sources/Lua/LuaContext.h --- a/OrthancFramework/Sources/Lua/LuaContext.h Tue Nov 05 13:21:23 2024 +0100 +++ b/OrthancFramework/Sources/Lua/LuaContext.h Tue Nov 05 18:50:57 2024 +0100 @@ -124,5 +124,17 @@ lua_State* state, int top, bool keyToLowerCase); + +#if ORTHANC_ENABLE_CURL == 1 + void SetHttpsVerifyPeers(bool verify) + { + httpClient_.SetHttpsVerifyPeers(verify); + } + + bool IsHttpsVerifyPeers() const + { + return httpClient_.IsHttpsVerifyPeers(); + } +#endif }; } diff -r 3ddd1b0231e9 -r 1dd0356099f0 OrthancFramework/UnitTestsSources/RestApiTests.cpp --- a/OrthancFramework/UnitTestsSources/RestApiTests.cpp Tue Nov 05 13:21:23 2024 +0100 +++ b/OrthancFramework/UnitTestsSources/RestApiTests.cpp Tue Nov 05 18:50:57 2024 +0100 @@ -73,19 +73,24 @@ ASSERT_TRUE(c.IsVerbose()); c.SetVerbose(false); ASSERT_FALSE(c.IsVerbose()); + ASSERT_TRUE(c.IsRedirectionFollowed()); + c.SetRedirectionFollowed(false); + ASSERT_FALSE(c.IsRedirectionFollowed()); #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 - // The "http://www.orthanc-server.com/downloads/third-party/" does - // not automatically redirect to HTTPS, so we cas use it even if the - // OpenSSL/HTTPS support is disabled in curl - const std::string BASE = "http://www.orthanc-server.com/downloads/third-party/"; + // The "http://httpbin.org/get" URL does not automatically redirect + // to HTTPS, so we can use it even if the OpenSSL/HTTPS support is + // disabled in curl + const std::string URL = "http://httpbin.org/get"; + Json::Value v; - c.SetUrl(BASE + "Product.json"); + c.SetUrl(URL); c.Apply(v); ASSERT_TRUE(v.type() == Json::objectValue); - ASSERT_TRUE(v.isMember("Description")); + ASSERT_TRUE(v.isMember("url")); + ASSERT_EQ(URL, v["url"].asString()); #endif } #endif diff -r 3ddd1b0231e9 -r 1dd0356099f0 OrthancServer/UnitTestsSources/LuaServerTests.cpp --- a/OrthancServer/UnitTestsSources/LuaServerTests.cpp Tue Nov 05 13:21:23 2024 +0100 +++ b/OrthancServer/UnitTestsSources/LuaServerTests.cpp Tue Nov 05 18:50:57 2024 +0100 @@ -138,38 +138,43 @@ TEST(Lua, Http) { Orthanc::LuaContext lua; - -#if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 - // The "http://www.orthanc-server.com/downloads/third-party/" does - // not automatically redirect to HTTPS, so we use it even if the - // OpenSSL/HTTPS support is disabled in curl - const std::string BASE = "http://www.orthanc-server.com/downloads/third-party/"; - -#if LUA_VERSION_NUM >= 502 - // Since Lua >= 5.2.0, the function "loadstring" has been replaced by "load" - lua.Execute("JSON = load(HttpGet('" + BASE + "JSON.lua')) ()"); -#else - lua.Execute("JSON = loadstring(HttpGet('" + BASE + "JSON.lua')) ()"); -#endif - - const std::string url(BASE + "Product.json"); -#endif + ASSERT_TRUE(lua.IsHttpsVerifyPeers()); + lua.SetHttpsVerifyPeers(false); + ASSERT_FALSE(lua.IsHttpsVerifyPeers()); std::string s; lua.Execute(s, "print(HttpGet({}))"); ASSERT_EQ("nil", Orthanc::Toolbox::StripSpaces(s)); -#if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 - lua.Execute(s, "print(string.len(HttpGet(\"" + url + "\")))"); - ASSERT_LE(100, boost::lexical_cast(Orthanc::Toolbox::StripSpaces(s))); +#if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 + // The "http://httpbin.org/get" URL does not automatically redirect + // to HTTPS, so we can use it even if the OpenSSL/HTTPS support is + // disabled in curl - // Parse a JSON file - lua.Execute(s, "print(JSON:decode(HttpGet(\"" + url + "\")) ['Product'])"); - ASSERT_EQ("OrthancClient", Orthanc::Toolbox::StripSpaces(s)); + const std::string URL = "http://httpbin.org/get"; + lua.Execute(s, "print(HttpGet(\"" + URL + "\"))"); + + Json::Value json; + Orthanc::Toolbox::ReadJson(json, s); + + ASSERT_TRUE(json.type() == Json::objectValue); + ASSERT_TRUE(json.isMember("url")); + ASSERT_EQ(URL, json["url"].asString()); #if 0 // This part of the test can only be executed if one instance of - // Orthanc is running on the localhost + // Orthanc is running on the localhost, with configuration option + // "ExecuteLuaEnabled" equals to "true" + + const std::string JSON_LUA_TOOLBOX = "https://regex.info/code/JSON.lua"; + lua.Execute("HttpGet('" + JSON_LUA_TOOLBOX + "')"); + +#if LUA_VERSION_NUM >= 502 + // Since Lua >= 5.2.0, the function "loadstring" has been replaced by "load" + lua.Execute("JSON = load(HttpGet('" + JSON_LUA_TOOLBOX + "')) ()"); +#else + lua.Execute("JSON = loadstring(HttpGet('" + JSON_LUA_TOOLBOX + "')) ()"); +#endif lua.Execute("modality = {}"); lua.Execute("table.insert(modality, 'ORTHANC')");