Testing of the viewer settings endpoint

This commit is contained in:
Veloman Yunkan 2022-09-21 16:50:41 +04:00
parent 0994a8f1b0
commit 0a0f52f1e2
2 changed files with 79 additions and 9 deletions

View File

@ -104,7 +104,7 @@ TEST(indexTemplateStringTest, emptyIndexTemplate) {
"./test/corner_cases.zim" "./test/corner_cases.zim"
}; };
ZimFileServer zfs(PORT, /*withTaskbar=*/true, ZIMFILES, ""); ZimFileServer zfs(PORT, ZimFileServer::DEFAULT_OPTIONS, ZIMFILES, "");
EXPECT_EQ(200, zfs.GET("/ROOT/")->status); EXPECT_EQ(200, zfs.GET("/ROOT/")->status);
} }
@ -115,7 +115,7 @@ TEST(indexTemplateStringTest, indexTemplateCheck) {
"./test/corner_cases.zim" "./test/corner_cases.zim"
}; };
ZimFileServer zfs(PORT, /*withTaskbar=*/true, ZIMFILES, "<!DOCTYPE html><head>" ZimFileServer zfs(PORT, ZimFileServer::DEFAULT_OPTIONS, ZIMFILES, "<!DOCTYPE html><head>"
"<title>Welcome to kiwix library</title>" "<title>Welcome to kiwix library</title>"
"</head>" "</head>"
"</html>"); "</html>");
@ -1090,7 +1090,7 @@ TEST_F(ServerTest, ETagIsTheSameAcrossHeadAndGet)
TEST_F(ServerTest, DifferentServerInstancesProduceDifferentETags) TEST_F(ServerTest, DifferentServerInstancesProduceDifferentETags)
{ {
ZimFileServer zfs2(SERVER_PORT + 1, /*withTaskbar=*/true, ZIMFILES); ZimFileServer zfs2(SERVER_PORT + 1, ZimFileServer::DEFAULT_OPTIONS, ZIMFILES);
for ( const Resource& res : all200Resources() ) { for ( const Resource& res : all200Resources() ) {
if ( !res.etag_expected ) continue; if ( !res.etag_expected ) continue;
const auto h1 = zfs1_->HEAD(res.url); const auto h1 = zfs1_->HEAD(res.url);
@ -1481,3 +1481,54 @@ TEST_F(ServerTest, suggestions_in_range)
ASSERT_EQ(currCount, 0); ASSERT_EQ(currCount, 0);
} }
} }
TEST_F(ServerTest, viewerSettings)
{
const auto JS_CONTENT_TYPE = "application/javascript; charset=utf-8";
{
resetServer(ZimFileServer::NO_TASKBAR_NO_LINK_BLOCKING);
const auto r = zfs1_->GET("/ROOT/viewer_settings.js");
ASSERT_EQ(r->status, 200);
ASSERT_EQ(getHeaderValue(r->headers, "Content-Type"), JS_CONTENT_TYPE);
ASSERT_EQ(r->body,
R"(const viewerSettings = {
toolbarEnabled: false,
linkBlockingEnabled: false,
libraryButtonEnabled: false
}
)");
}
{
resetServer(ZimFileServer::BLOCK_EXTERNAL_LINKS);
ASSERT_EQ(zfs1_->GET("/ROOT/viewer_settings.js")->body,
R"(const viewerSettings = {
toolbarEnabled: false,
linkBlockingEnabled: true,
libraryButtonEnabled: false
}
)");
}
{
resetServer(ZimFileServer::WITH_TASKBAR);
ASSERT_EQ(zfs1_->GET("/ROOT/viewer_settings.js")->body,
R"(const viewerSettings = {
toolbarEnabled: true,
linkBlockingEnabled: false,
libraryButtonEnabled: false
}
)");
}
{
resetServer(ZimFileServer::WITH_TASKBAR_AND_LIBRARY_BUTTON);
ASSERT_EQ(zfs1_->GET("/ROOT/viewer_settings.js")->body,
R"(const viewerSettings = {
toolbarEnabled: true,
linkBlockingEnabled: false,
libraryButtonEnabled: true
}
)");
}
}

View File

@ -54,10 +54,23 @@ public: // types
typedef std::shared_ptr<httplib::Response> Response; typedef std::shared_ptr<httplib::Response> Response;
typedef std::vector<std::string> FilePathCollection; typedef std::vector<std::string> FilePathCollection;
enum Options
{
NO_TASKBAR_NO_LINK_BLOCKING = 0,
WITH_TASKBAR = 1 << 1,
WITH_LIBRARY_BUTTON = 1 << 2,
BLOCK_EXTERNAL_LINKS = 1 << 3,
WITH_TASKBAR_AND_LIBRARY_BUTTON = WITH_TASKBAR | WITH_LIBRARY_BUTTON,
DEFAULT_OPTIONS = WITH_TASKBAR | WITH_LIBRARY_BUTTON
};
public: // functions public: // functions
ZimFileServer(int serverPort, std::string libraryFilePath); ZimFileServer(int serverPort, std::string libraryFilePath);
ZimFileServer(int serverPort, ZimFileServer(int serverPort,
bool withTaskbar, Options options,
const FilePathCollection& zimpaths, const FilePathCollection& zimpaths,
std::string indexTemplateString = ""); std::string indexTemplateString = "");
~ZimFileServer(); ~ZimFileServer();
@ -81,7 +94,7 @@ private: // data
std::unique_ptr<kiwix::HumanReadableNameMapper> nameMapper; std::unique_ptr<kiwix::HumanReadableNameMapper> nameMapper;
std::unique_ptr<kiwix::Server> server; std::unique_ptr<kiwix::Server> server;
std::unique_ptr<httplib::Client> client; std::unique_ptr<httplib::Client> client;
const bool withTaskbar = true; const Options options = DEFAULT_OPTIONS;
}; };
ZimFileServer::ZimFileServer(int serverPort, std::string libraryFilePath) ZimFileServer::ZimFileServer(int serverPort, std::string libraryFilePath)
@ -94,11 +107,11 @@ ZimFileServer::ZimFileServer(int serverPort, std::string libraryFilePath)
} }
ZimFileServer::ZimFileServer(int serverPort, ZimFileServer::ZimFileServer(int serverPort,
bool _withTaskbar, Options _options,
const FilePathCollection& zimpaths, const FilePathCollection& zimpaths,
std::string indexTemplateString) std::string indexTemplateString)
: manager(&this->library) : manager(&this->library)
, withTaskbar(_withTaskbar) , options(_options)
{ {
for ( const auto& zimpath : zimpaths ) { for ( const auto& zimpath : zimpaths ) {
if (!manager.addBookFromPath(zimpath, zimpath, "", false)) if (!manager.addBookFromPath(zimpath, zimpath, "", false))
@ -117,7 +130,8 @@ void ZimFileServer::run(int serverPort, std::string indexTemplateString)
server->setPort(serverPort); server->setPort(serverPort);
server->setNbThreads(2); server->setNbThreads(2);
server->setVerbose(false); server->setVerbose(false);
server->setTaskbar(withTaskbar, withTaskbar); server->setTaskbar(options & WITH_TASKBAR, options & WITH_LIBRARY_BUTTON);
server->setBlockExternalLinks(options & BLOCK_EXTERNAL_LINKS);
server->setMultiZimSearchLimit(3); server->setMultiZimSearchLimit(3);
if (!indexTemplateString.empty()) { if (!indexTemplateString.empty()) {
server->setIndexTemplateString(indexTemplateString); server->setIndexTemplateString(indexTemplateString);
@ -148,7 +162,12 @@ protected:
protected: protected:
void SetUp() override { void SetUp() override {
zfs1_.reset(new ZimFileServer(SERVER_PORT, /*withTaskbar=*/true, ZIMFILES)); resetServer(ZimFileServer::DEFAULT_OPTIONS);
}
void resetServer(ZimFileServer::Options options) {
zfs1_.reset();
zfs1_.reset(new ZimFileServer(SERVER_PORT, options, ZIMFILES));
} }
void TearDown() override { void TearDown() override {