Skip to content

Commit d1fc372

Browse files
committed
fix: preProcessedFileSubstitution replacing to ""
preProcessedFileSubstitution did not work correctly if an empty map was given. In this case `variables["ACCESS_TOKEN"]` would be read as `""` as that key was not present in the map. Poco::replaceInPlace would in turn replace the `%ACCESS_TOKEN%` with `""`, while it should just keep the not specified parts as is. I chose to add a function rather than ifs in the body to prevent future mistakes from happening. Signed-off-by: Robbert Gurdeep Singh <[email protected]>
1 parent 510f789 commit d1fc372

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

test/FileServeWhiteBoxTests.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class FileServeTests : public CPPUNIT_NS::TestFixture
4343

4444
void preProcessedFileSubstitution(const std::string& testname,
4545
std::unordered_map<std::string, std::string> variables);
46+
// helper for replacements with the empty map
47+
std::string& replaceOrKeep(std::string& str, const std::string& from, const std::string& to);
4648
};
4749

4850
void FileServeTests::testUIDefaults()
@@ -391,30 +393,41 @@ void FileServeTests::preProcessedFileSubstitution(
391393

392394
const std::string recon = ppf.substitute(variables);
393395

394-
Poco::replaceInPlace(orig, std::string("%ACCESS_TOKEN%"), variables["ACCESS_TOKEN"]);
395-
Poco::replaceInPlace(orig, std::string("%ACCESS_TOKEN_TTL%"),
396+
replaceOrKeep(orig, std::string("%ACCESS_TOKEN%"), variables["ACCESS_TOKEN"]);
397+
replaceOrKeep(orig, std::string("%ACCESS_TOKEN_TTL%"),
396398
variables["ACCESS_TOKEN_TTL"]);
397-
Poco::replaceInPlace(orig, std::string("%ACCESS_HEADER%"), variables["ACCESS_HEADER"]);
398-
Poco::replaceInPlace(orig, std::string("%UI_DEFAULTS%"), variables["UI_DEFAULTS"]);
399-
Poco::replaceInPlace(orig, std::string("<!--%CSS_VARIABLES%-->"),
399+
replaceOrKeep(orig, std::string("%ACCESS_HEADER%"), variables["ACCESS_HEADER"]);
400+
replaceOrKeep(orig, std::string("%UI_DEFAULTS%"), variables["UI_DEFAULTS"]);
401+
replaceOrKeep(orig, std::string("<!--%CSS_VARIABLES%-->"),
400402
variables["CSS_VARIABLES"]);
401-
Poco::replaceInPlace(orig, std::string("%POSTMESSAGE_ORIGIN%"),
403+
replaceOrKeep(orig, std::string("%POSTMESSAGE_ORIGIN%"),
402404
variables["POSTMESSAGE_ORIGIN"]);
403-
Poco::replaceInPlace(orig, std::string("%BRANDING_THEME%"),
405+
replaceOrKeep(orig, std::string("%BRANDING_THEME%"),
404406
variables["BRANDING_THEME"]);
405-
Poco::replaceInPlace(orig, std::string("<!--%BRANDING_JS%-->"),
407+
replaceOrKeep(orig, std::string("<!--%BRANDING_JS%-->"),
406408
variables["BRANDING_JS"]);
407-
Poco::replaceInPlace(orig, std::string("%FOOTER%"), variables["FOOTER"]);
408-
Poco::replaceInPlace(orig, std::string("%CHECK_FILE_INFO_OVERRIDE%"),
409+
replaceOrKeep(orig, std::string("%FOOTER%"), variables["FOOTER"]);
410+
replaceOrKeep(orig, std::string("%CHECK_FILE_INFO_OVERRIDE%"),
409411
variables["CHECK_FILE_INFO_OVERRIDE"]);
410-
Poco::replaceInPlace(orig, std::string("%BUYPRODUCT_URL%"),
412+
replaceOrKeep(orig, std::string("%BUYPRODUCT_URL%"),
411413
variables["BUYPRODUCT_URL"]);
412414

413415
LOK_ASSERT_EQUAL(orig, recon);
414416
}
415417
}
416418
}
417419

420+
// Replace each occurence of from in str to to except if to is the empty string
421+
std::string& FileServeTests::replaceOrKeep(std::string& str, const std::string& from,
422+
const std::string& to)
423+
{
424+
if (to.empty())
425+
{
426+
return str;
427+
}
428+
return Poco::replaceInPlace(str, from, to);
429+
}
430+
418431
void FileServeTests::testPreProcessedFileSubstitution()
419432
{
420433
constexpr auto testname = __func__;

0 commit comments

Comments
 (0)