Preparing to test archive dependent 404 responses

This commit is contained in:
Veloman Yunkan 2022-01-23 21:00:19 +04:00 committed by Matthieu Gautier
parent ae2d9b234f
commit 92f9ee9280
1 changed files with 44 additions and 18 deletions

View File

@ -322,9 +322,23 @@ TEST_F(ServerTest, 404)
EXPECT_EQ(404, zfs1_->GET(url)->status) << "url: " << url; EXPECT_EQ(404, zfs1_->GET(url)->status) << "url: " << url;
} }
std::string makeExpected404Response(const std::string& body) struct TestContentIn404HtmlResponse
{ {
const std::string preBody = R"PREBODY(<!DOCTYPE html> TestContentIn404HtmlResponse(const std::string& url,
const std::string& expectedBody)
: url(url)
, expectedBody(expectedBody)
{}
std::string url, expectedBody;
std::string expectedResponse() const;
};
std::string TestContentIn404HtmlResponse::expectedResponse() const
{
const std::string frag[] = {
R"FRAG(<!DOCTYPE html>
<html> <html>
<head> <head>
<meta content="text/html;charset=UTF-8" http-equiv="content-type" /> <meta content="text/html;charset=UTF-8" http-equiv="content-type" />
@ -341,33 +355,47 @@ std::string makeExpected404Response(const std::string& body)
<div class="kiwix_centered"> <div class="kiwix_centered">
<div class="kiwix_searchform"> <div class="kiwix_searchform">
<form class="kiwixsearch" method="GET" action="/ROOT/search" id="kiwixsearchform"> <form class="kiwixsearch" method="GET" action="/ROOT/search" id="kiwixsearchform">
//EOLWHITESPACEMARKER )FRAG",
R"FRAG(
<label for="kiwixsearchbox">&#x1f50d;</label> <label for="kiwixsearchbox">&#x1f50d;</label>
<input autocomplete="off" class="ui-autocomplete-input" id="kiwixsearchbox" name="pattern" type="text" title="Search ''" aria-label="Search ''"> )FRAG",
</form>
R"FRAG( <input autocomplete="off" class="ui-autocomplete-input" id="kiwixsearchbox" name="pattern" type="text" title="Search ''" aria-label="Search ''">
)FRAG",
R"FRAG( </form>
</div> </div>
<input type="checkbox" id="kiwix_button_show_toggle"> <input type="checkbox" id="kiwix_button_show_toggle">
<label for="kiwix_button_show_toggle"><img src="/ROOT/skin/caret.png" alt=""></label> <label for="kiwix_button_show_toggle"><img src="/ROOT/skin/caret.png" alt=""></label>
<div class="kiwix_button_cont"> <div class="kiwix_button_cont">
<a id="kiwix_serve_taskbar_library_button" title="Go to welcome page" aria-label="Go to welcome page" href="/ROOT/"><button>&#x1f3e0;</button></a> <a id="kiwix_serve_taskbar_library_button" title="Go to welcome page" aria-label="Go to welcome page" href="/ROOT/"><button>&#x1f3e0;</button></a>
//EOLWHITESPACEMARKER )FRAG",
R"FRAG(
</div> </div>
</div> </div>
</span> </span>
</span> </span>
)PREBODY"; )FRAG",
const std::string postBody = R"POSTBODY( </body> R"FRAG( </body>
</html> </html>
)POSTBODY"; )FRAG"
};
return removeEOLWhitespaceMarkers(preBody + body + postBody); return frag[0]
+ frag[1]
+ frag[2]
+ frag[3]
+ frag[4]
+ removeEOLWhitespaceMarkers(expectedBody)
+ frag[5];
} }
TEST_F(ServerTest, 404WithBodyTesting) TEST_F(ServerTest, 404WithBodyTesting)
{ {
typedef std::pair<std::string, std::string> UrlAndExpectedBody; const std::vector<TestContentIn404HtmlResponse> testData{
const std::vector<UrlAndExpectedBody> testData{
{ /* url */ "/ROOT/random?content=non-existent-book", { /* url */ "/ROOT/random?content=non-existent-book",
/* expected body */ R"( /* expected body */ R"(
<h1>Not Found</h1> <h1>Not Found</h1>
@ -442,13 +470,11 @@ TEST_F(ServerTest, 404WithBodyTesting)
)" } )" }
}; };
for ( const auto& urlAndExpectedBody : testData ) { for ( const auto& t : testData ) {
const std::string url = urlAndExpectedBody.first; const TestContext ctx{ {"url", t.url} };
const std::string expectedBody = urlAndExpectedBody.second; const auto r = zfs1_->GET(t.url.c_str());
const TestContext ctx{ {"url", url} };
const auto r = zfs1_->GET(url.c_str());
EXPECT_EQ(r->status, 404) << ctx; EXPECT_EQ(r->status, 404) << ctx;
EXPECT_EQ(r->body, makeExpected404Response(expectedBody)) << ctx; EXPECT_EQ(r->body, t.expectedResponse()) << ctx;
} }
} }