From 06d7a2320fd81de0a57eb6fa0c8b00952c5cfc1e Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sat, 4 Jun 2022 21:18:59 +0400 Subject: [PATCH] test/server_search.cpp covers XML search too --- test/server_search.cpp | 149 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/test/server_search.cpp b/test/server_search.cpp index 6749ad323..16e4d7fd8 100644 --- a/test/server_search.cpp +++ b/test/server_search.cpp @@ -137,6 +137,25 @@ std::string makeSearchResultsHtml(const std::string& pattern, return html; } +std::string makeSearchResultsXml(const std::string& header, + const std::string& results) +{ + const char SEARCHRESULTS_XML_TEMPLATE[] = R"XML( + + + %HEADER%%RESULTS% + + +)XML"; + + std::string html = removeEOLWhitespaceMarkers(SEARCHRESULTS_XML_TEMPLATE); + html = replace(html, "%HEADER%", header); + html = replace(html, "%RESULTS%", results); + return html; +} + struct SearchResult { std::string link; @@ -155,6 +174,18 @@ struct SearchResult + "
from " + bookTitle + "
\n" + "
" + wordCount + " words
\n"; } + + std::string getXml() const + { + return std::string() + + " " + title + "\n" + + " " + replace(link, "'", "'") + "\n" + + " " + snippet + "\n" + + " \n" + + " " + bookTitle + "\n" + + " \n" + + " " + wordCount + ""; + } }; #define SEARCH_RESULT(LINK, TITLE, SNIPPET, BOOK_TITLE, WORDCOUNT) \ @@ -569,6 +600,27 @@ Snippets extractSearchResultSnippetsFromHtml(const std::string& html) return snippets; } +const char SNIPPET_REGEX_FOR_XML[] = "(?!Search result for)(.+)"; + +std::string maskSnippetsInXmlSearchResults(std::string s) +{ + return replace(s, SNIPPET_REGEX_FOR_XML, "SNIPPET TEXT WAS MASKED"); +} + +Snippets extractSearchResultSnippetsFromXml(const std::string& xml) +{ + Snippets snippets; + const std::regex snippetRegex(SNIPPET_REGEX_FOR_XML); + std::sregex_iterator snippetIt(xml.begin(), xml.end(), snippetRegex); + const std::sregex_iterator end; + for ( ; snippetIt != end; ++snippetIt) + { + const std::smatch snippetMatch = *snippetIt; + snippets.push_back(snippetMatch[1].str()); + } + return snippets; +} + bool isValidSnippet(const std::string& s) { return s.size() >= 250 @@ -650,19 +702,37 @@ struct TestData return url; } - std::string getPattern() const + std::string extractQueryValue(const std::string& key) const { - const std::string p = "pattern="; + const std::string p = key + "="; const size_t i = query.find(p); + if (i == std::string::npos) { + return ""; + } std::string r = query.substr(i + p.size()); return r.substr(0, r.find("&")); } + std::string getPattern() const + { + return extractQueryValue("pattern"); + } + + std::string getLang() const + { + return extractQueryValue("books.filter.lang"); + } + std::string url() const { return makeUrl(query, start, resultsPerPage); } + std::string xmlSearchUrl() const + { + return url() + "&format=xml"; + } + std::string expectedHtmlHeader() const { if ( totalResultCount == 0 ) { @@ -736,11 +806,70 @@ struct TestData ); } + std::string expectedXmlHeader() const + { + std::string header = R"(Search: PATTERN + URL + Search result for PATTERN + RESULTCOUNT + FIRSTRESULT + ITEMCOUNT +