From d1b85192c0c83859442f757c989fcc4aad242fd8 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 21 Oct 2022 19:55:38 +0400 Subject: [PATCH] ServerSearchTest.searchInMultilanguageBookSetIsDenied --- test/server_search.cpp | 87 +++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/test/server_search.cpp b/test/server_search.cpp index ca6bc1651..3932602ca 100644 --- a/test/server_search.cpp +++ b/test/server_search.cpp @@ -7,16 +7,6 @@ #include "server_testing_tools.h" -class ServerSearchTest : public ServerTest -{ - void SetUp() override { - zfs1_.reset(new ZimFileServer(SERVER_PORT, - ZimFileServer::DEFAULT_OPTIONS, - "./test/lib_for_server_search_test.xml") - ); - } -}; - std::string makeSearchResultsHtml(const std::string& pattern, const std::string& header, const std::string& results, @@ -927,7 +917,7 @@ struct TestData } }; -TEST_F(ServerSearchTest, searchResults) +TEST(ServerSearchTest, searchResults) { const TestData testData[] = { { @@ -1468,15 +1458,86 @@ TEST_F(ServerSearchTest, searchResults) }, }; + ZimFileServer zfs(SERVER_PORT, ZimFileServer::DEFAULT_OPTIONS, + "./test/lib_for_server_search_test.xml"); + for ( const auto& t : testData ) { const std::string htmlSearchUrl = t.url(); - const auto htmlRes = zfs1_->GET(htmlSearchUrl.c_str()); + const auto htmlRes = zfs.GET(htmlSearchUrl.c_str()); EXPECT_EQ(htmlRes->status, 200); t.checkHtml(htmlRes->body); const std::string xmlSearchUrl = t.xmlSearchUrl(); - const auto xmlRes = zfs1_->GET(xmlSearchUrl.c_str()); + const auto xmlRes = zfs.GET(xmlSearchUrl.c_str()); EXPECT_EQ(xmlRes->status, 200); t.checkXml(xmlRes->body); } } + +std::string expectedConfusionOfTonguesErrorHtml(std::string url) +{ + return R"( + + + + Invalid request + + + +

Invalid request

+

+ The requested URL ")" + url + R"(" is not a valid request. +

+

+ Two or more books in different languages would participate in search, which may lead to confusing results. +

+ + +)"; +} + +std::string expectedConfusionOfTonguesErrorXml(std::string url) +{ + return R"( +Invalid request +The requested URL ")" + url + R"(" is not a valid request. +Two or more books in different languages would participate in search, which may lead to confusing results. +)"; +} + +TEST(ServerSearchTest, searchInMultilanguageBookSetIsDenied) +{ + const std::string testQueries[] = { + "pattern=towerofbabel", + "pattern=babylon&books.filter.maxsize=1000000", + "pattern=baby&books.id=" RAYCHARLESZIMID "&books.id=" EXAMPLEZIMID, + }; + + // The default limit on the number of books in a multi-zim search is 3 + const ZimFileServer::FilePathCollection ZIMFILES{ + "./test/zimfile.zim", // eng + "./test/example.zim", // en + "./test/corner_cases.zim" // =en + }; + + ZimFileServer zfs(SERVER_PORT, ZimFileServer::DEFAULT_OPTIONS, ZIMFILES); + for ( const auto& q : testQueries ) { + { + // HTML mode + const std::string url = "/ROOT/search?" + q; + const auto r = zfs.GET(url.c_str()); + const TestContext ctx{ {"url", url} }; + EXPECT_EQ(r->status, 400) << ctx; + EXPECT_EQ(r->body, expectedConfusionOfTonguesErrorHtml(url)) << ctx; + } + + { + // XML mode + const std::string url = "/ROOT/search?" + q + "&format=xml"; + const auto r = zfs.GET(url.c_str()); + const TestContext ctx{ {"url", url} }; + EXPECT_EQ(r->status, 400) << ctx; + EXPECT_EQ(r->body, expectedConfusionOfTonguesErrorXml(url)) << ctx; + } + } +}