From acdc1dfb2745b529262eaccfe37b74e1a7f0e31c Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 3 Apr 2022 21:41:37 +0400 Subject: [PATCH] New unit-test ServerTest.CacheIdsOfStaticResources Introduced a new unit-test which will ensure that static resources of kiwix-serve have the cache ids applied to them in the links embedded into the HTML code. At this point there are no cache ids. The new unit-test will help to visualize how they come into existence. --- test/server.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/test/server.cpp b/test/server.cpp index 9f4356b5a..d95f15fad 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -288,6 +288,85 @@ TEST_F(ServerTest, UncompressibleContentIsNotCompressed) } } + +// Selects from text only the lines containing the specified (fixed string) +// pattern +std::string fgrep(const std::string& pattern, const std::string& text) +{ + std::istringstream iss(text); + std::string line; + std::string result; + while ( getline(iss, line) ) { + if ( line.find(pattern) != std::string::npos ) { + result += line + "\n"; + } + } + return result; +} + +TEST_F(ServerTest, CacheIdsOfStaticResources) +{ + typedef std::pair UrlAndExpectedResult; + const std::vector testData{ + { + /* url */ "/ROOT/", +R"EXPECTEDRESULT( src="/ROOT/skin/jquery-ui/external/jquery/jquery.js" + src="/ROOT/skin/jquery-ui/jquery-ui.min.js" + href="/ROOT/skin/jquery-ui/jquery-ui.min.css" + href="/ROOT/skin/jquery-ui/jquery-ui.theme.min.css" + href="/ROOT/skin/index.css" + src: url("/ROOT/skin/fonts/Poppins.ttf") format("truetype"); + src: url("/ROOT/skin/fonts/Roboto.ttf") format("truetype"); + + + +)EXPECTEDRESULT" + }, + { + /* url */ "/ROOT/skin/index.js", +R"EXPECTEDRESULT( direct download + download hash + download magnet + download torrent +)EXPECTEDRESULT" + }, + { + /* url */ "/ROOT/zimfile/A/index", +R"EXPECTEDRESULT( + + + + + + +)EXPECTEDRESULT" + }, + { + // Searching in a ZIM file without a full-text index returns + // a page rendered from static/templates/no_search_result_html + /* url */ "/ROOT/search?content=poor&pattern=whatever", +R"EXPECTEDRESULT( + + + + + + + +)EXPECTEDRESULT" + }, + }; + + for ( const auto& urlAndExpectedResult : testData ) { + const std::string url = urlAndExpectedResult.first; + const std::string expectedResult = urlAndExpectedResult.second; + const TestContext ctx{ {"url", url} }; + const auto r = zfs1_->GET(url.c_str()); + EXPECT_EQ(r->body.find("KIWIXCACHEID"), std::string::npos) << ctx; + EXPECT_EQ(fgrep("/skin/", r->body), expectedResult) << ctx; + } +} + const char* urls400[] = { "/ROOT/search", "/ROOT/search?content=zimfile",