diff --git a/test/server.cpp b/test/server.cpp index 600b171ae..bff14f654 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -322,26 +322,63 @@ TEST_F(ServerTest, 404) EXPECT_EQ(404, zfs1_->GET(url)->status) << "url: " << url; } -class TestContentIn404HtmlResponse +namespace TestingOfHtmlResponses +{ + +struct ExpectedResponseData +{ + const std::string bookName, bookTitle, expectedBody; +}; + +enum ExpectedResponseDataType +{ + book_name, + book_title, + expected_body +}; + +// Operator overloading is used as a means of defining a mini-DSL for +// defining test data in a concise way (see usage in +// TEST_F(ServerTest, 404WithBodyTesting)) +ExpectedResponseData operator==(ExpectedResponseDataType t, std::string s) +{ + switch (t) + { + case book_name: return ExpectedResponseData{s, "", ""}; + case book_title: return ExpectedResponseData{"", s, ""}; + case expected_body: return ExpectedResponseData{"", "", s}; + default: assert(false); return ExpectedResponseData{}; + } +} + +std::string selectNonEmpty(const std::string& a, const std::string& b) +{ + if ( a.empty() ) return b; + + assert(b.empty()); + return a; +} + +ExpectedResponseData operator&&(const ExpectedResponseData& a, + const ExpectedResponseData& b) +{ + return ExpectedResponseData{ + selectNonEmpty(a.bookName, b.bookName), + selectNonEmpty(a.bookTitle, b.bookTitle), + selectNonEmpty(a.expectedBody, b.expectedBody) + }; +} + +class TestContentIn404HtmlResponse : public ExpectedResponseData { public: TestContentIn404HtmlResponse(const std::string& url, - const std::string& expectedBody) - : url(url) - , expectedBody(expectedBody) + const ExpectedResponseData& erd) + : ExpectedResponseData(erd) + , url(url) {} - TestContentIn404HtmlResponse(const std::string& url, - const std::string& bookName, - const std::string& bookTitle, - const std::string& expectedBody) - : url(url) - , bookName(bookName) - , bookTitle(bookTitle) - , expectedBody(expectedBody) - {} - - const std::string url, bookName, bookTitle, expectedBody; + const std::string url; std::string expectedResponse() const; @@ -445,11 +482,14 @@ std::string TestContentIn404HtmlResponse::taskbarLinks() const + R"(">)"; } +} // namespace TestingOfHtmlResponses + TEST_F(ServerTest, 404WithBodyTesting) { + using namespace TestingOfHtmlResponses; const std::vector testData{ { /* url */ "/ROOT/random?content=non-existent-book", - /* expected body */ R"( + expected_body==R"(

Not Found

//EOLWHITESPACEMARKER

@@ -458,7 +498,7 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/suggest?content=no-such-book&term=whatever", - /* expected body */ R"( + expected_body==R"(

Not Found

//EOLWHITESPACEMARKER

@@ -467,7 +507,7 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/catalog/", - /* expected body */ R"( + expected_body==R"(

Not Found

The requested URL "/ROOT/catalog/" was not found on this server. @@ -478,7 +518,7 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/catalog/invalid_endpoint", - /* expected body */ R"( + expected_body==R"(

Not Found

The requested URL "/ROOT/catalog/invalid_endpoint" was not found on this server. @@ -489,7 +529,7 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/invalid-book/whatever", - /* expected body */ R"( + expected_body==R"(

Not Found

The requested URL "/ROOT/invalid-book/whatever" was not found on this server. @@ -500,9 +540,9 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/zimfile/invalid-article", - /* book name */ "zimfile", - /* book title */ "Ray Charles", - /* expected body */ R"( + book_name=="zimfile" && + book_title=="Ray Charles" && + expected_body==R"(

Not Found

The requested URL "/ROOT/zimfile/invalid-article" was not found on this server. @@ -513,7 +553,7 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ R"(/ROOT/">)", - /* expected body */ R"( + expected_body==R"(

Not Found

The requested URL "/ROOT/"><svg onload=alert(1)>" was not found on this server. @@ -524,9 +564,9 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ R"(/ROOT/zimfile/">)", - /* book name */ "zimfile", - /* book title */ "Ray Charles", - /* expected body */ R"( + book_name=="zimfile" && + book_title=="Ray Charles" && + expected_body==R"(

Not Found

The requested URL "/ROOT/zimfile/"><svg onload=alert(1)>" was not found on this server. @@ -537,7 +577,7 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/raw/no-such-book/meta/Title", - /* expected body */ R"( + expected_body==R"(

Not Found

The requested URL "/ROOT/raw/no-such-book/meta/Title" was not found on this server. @@ -548,7 +588,7 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/raw/zimfile/XYZ", - /* expected body */ R"( + expected_body==R"(

Not Found

The requested URL "/ROOT/raw/zimfile/XYZ" was not found on this server. @@ -559,9 +599,9 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/raw/zimfile/meta/invalid-metadata", - /* book name */ "zimfile", - /* book title */ "Ray Charles", - /* expected body */ R"( + book_name=="zimfile" && + book_title=="Ray Charles" && + expected_body==R"(

Not Found

The requested URL "/ROOT/raw/zimfile/meta/invalid-metadata" was not found on this server. @@ -572,9 +612,9 @@ TEST_F(ServerTest, 404WithBodyTesting) )" }, { /* url */ "/ROOT/raw/zimfile/content/invalid-article", - /* book name */ "zimfile", - /* book title */ "Ray Charles", - /* expected body */ R"( + book_name=="zimfile" && + book_title=="Ray Charles" && + expected_body==R"(

Not Found

The requested URL "/ROOT/raw/zimfile/content/invalid-article" was not found on this server.