From a34dc725f90224e0c1a774264d60d763e973d625 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 18 Jan 2022 21:50:36 +0400 Subject: [PATCH 1/7] ServerTest.RandomOnNonExistentBook This test was introduced with the purpose of testing the error message in the 404 page returned by /random for a non-existent book. The actual expected output currently present in this new unit-test is too much for that purpose and may become a maintenance burden if more tests of that kind are added. --- test/server.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/test/server.cpp b/test/server.cpp index fe584ad83..77681f5fd 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -322,6 +322,53 @@ TEST_F(ServerTest, 404) EXPECT_EQ(404, zfs1_->GET(url)->status) << "url: " << url; } +TEST_F(ServerTest, RandomOnNonExistentBook) +{ + const auto r = zfs1_->GET("/ROOT/random?content=non-existent-book"); + const char expectedResponse[] = R"EXPECTEDRESPONSE( + + + + Content not found + + + + + + + + + +
+
+
+ //EOLWHITESPACEMARKER + + +
+
+ + +
+ + //EOLWHITESPACEMARKER +
+
+
+
+ +

Not Found

+ //EOLWHITESPACEMARKER +

+ No such book: non-existent-book +

+ + +)EXPECTEDRESPONSE"; + + EXPECT_EQ(r->body, removeEOLWhitespaceMarkers(expectedResponse)); +} + TEST_F(ServerTest, RandomPageRedirectsToAnExistingArticle) { auto g = zfs1_->GET("/ROOT/random?content=zimfile"); From 5f4256b900a8246ba911b0c806a54b2836325769 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 20 Jan 2022 20:41:23 +0400 Subject: [PATCH 2/7] Enter helper function makeExpected404Response() --- test/server.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/test/server.cpp b/test/server.cpp index 77681f5fd..8d9a2054b 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -322,10 +322,9 @@ TEST_F(ServerTest, 404) EXPECT_EQ(404, zfs1_->GET(url)->status) << "url: " << url; } -TEST_F(ServerTest, RandomOnNonExistentBook) +std::string makeExpected404Response(const std::string& body) { - const auto r = zfs1_->GET("/ROOT/random?content=non-existent-book"); - const char expectedResponse[] = R"EXPECTEDRESPONSE( + const std::string preBody = R"PREBODY( @@ -356,17 +355,27 @@ TEST_F(ServerTest, RandomOnNonExistentBook) +)PREBODY"; + const std::string postBody = R"POSTBODY( + +)POSTBODY"; + + return removeEOLWhitespaceMarkers(preBody + body + postBody); +} + +TEST_F(ServerTest, RandomOnNonExistentBook) +{ + const auto r = zfs1_->GET("/ROOT/random?content=non-existent-book"); + const std::string expectedResponse = makeExpected404Response(R"(

Not Found

//EOLWHITESPACEMARKER

No such book: non-existent-book

- - -)EXPECTEDRESPONSE"; +)"); - EXPECT_EQ(r->body, removeEOLWhitespaceMarkers(expectedResponse)); + EXPECT_EQ(r->body, expectedResponse); } TEST_F(ServerTest, RandomPageRedirectsToAnExistingArticle) From 0ba452aece277a1d06035f583f101b9d181ae23c Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 20 Jan 2022 20:44:38 +0400 Subject: [PATCH 3/7] New unit-test ServerTest.404WithBodyTesting The `ServerTest.RandomOnNonExistentBook` unit test was replaced with a more general one testing multiple 404 scenarios where the content of the body is checked too. --- test/server.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/test/server.cpp b/test/server.cpp index 8d9a2054b..525eb69c9 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -364,18 +364,59 @@ std::string makeExpected404Response(const std::string& body) return removeEOLWhitespaceMarkers(preBody + body + postBody); } -TEST_F(ServerTest, RandomOnNonExistentBook) +TEST_F(ServerTest, 404WithBodyTesting) { - const auto r = zfs1_->GET("/ROOT/random?content=non-existent-book"); - const std::string expectedResponse = makeExpected404Response(R"( + typedef std::pair UrlAndExpectedBody; + const std::vector testData{ + { /* url */ "/ROOT/random?content=non-existent-book", + /* expected body */ R"(

Not Found

//EOLWHITESPACEMARKER

No such book: non-existent-book

-)"); +)" }, - EXPECT_EQ(r->body, expectedResponse); + { /* url */ "/ROOT/suggest?content=no-such-book&term=whatever", + /* expected body */ R"( +

Not Found

+ //EOLWHITESPACEMARKER +

+ No such book: no-such-book +

+)" }, + + { /* url */ "/ROOT/raw/no-such-book/meta/Title", + /* expected body */ R"( +

Not Found

+

+ The requested URL "/ROOT/raw/no-such-book/meta/Title" was not found on this server. +

+

+ No such book: no-such-book +

+)" }, + + { /* url */ "/ROOT/raw/zimfile/XYZ", + /* expected body */ R"( +

Not Found

+

+ The requested URL "/ROOT/raw/zimfile/XYZ" was not found on this server. +

+

+ XYZ is not a valid request for raw content. +

+)" } + }; + + for ( const auto& urlAndExpectedBody : testData ) { + const std::string url = urlAndExpectedBody.first; + const std::string expectedBody = urlAndExpectedBody.second; + const TestContext ctx{ {"url", url} }; + const auto r = zfs1_->GET(url.c_str()); + EXPECT_EQ(r->status, 404) << ctx; + EXPECT_EQ(r->body, makeExpected404Response(expectedBody)) << ctx; + } } TEST_F(ServerTest, RandomPageRedirectsToAnExistingArticle) From ae2d9b234fe9299210a147fd49547cc3d2a28cba Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 21 Jan 2022 23:08:32 +0400 Subject: [PATCH 4/7] More test points in ServerTest.404WithBodyTesting --- test/server.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/server.cpp b/test/server.cpp index 525eb69c9..9e2a3a80f 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -386,6 +386,39 @@ TEST_F(ServerTest, 404WithBodyTesting)

)" }, + { /* url */ "/ROOT/catalog/", + /* expected body */ R"( +

Not Found

+

+ The requested URL "/ROOT/catalog/" was not found on this server. +

+

+ //EOLWHITESPACEMARKER +

+)" }, + + { /* url */ "/ROOT/catalog/invalid_endpoint", + /* expected body */ R"( +

Not Found

+

+ The requested URL "/ROOT/catalog/invalid_endpoint" was not found on this server. +

+

+ //EOLWHITESPACEMARKER +

+)" }, + + { /* url */ "/ROOT/invalid-book/whatever", + /* expected body */ R"( +

Not Found

+

+ The requested URL "/ROOT/invalid-book/whatever" was not found on this server. +

+

+ Make a full text search for whatever +

+)" }, + { /* url */ "/ROOT/raw/no-such-book/meta/Title", /* expected body */ R"(

Not Found

From 92f9ee9280d8fb81aacab3b6637b7fc6234b73ed Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 23 Jan 2022 21:00:19 +0400 Subject: [PATCH 5/7] Preparing to test archive dependent 404 responses --- test/server.cpp | 62 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/test/server.cpp b/test/server.cpp index 9e2a3a80f..16b4e6aa1 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -322,9 +322,23 @@ TEST_F(ServerTest, 404) EXPECT_EQ(404, zfs1_->GET(url)->status) << "url: " << url; } -std::string makeExpected404Response(const std::string& body) +struct TestContentIn404HtmlResponse { - const std::string preBody = R"PREBODY( + 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( @@ -341,33 +355,47 @@ std::string makeExpected404Response(const std::string& body)
- //EOLWHITESPACEMARKER + )FRAG", + + R"FRAG( - -
+)FRAG", + + R"FRAG( +)FRAG", + + R"FRAG(
- //EOLWHITESPACEMARKER + )FRAG", + + R"FRAG(
-)PREBODY"; +)FRAG", - const std::string postBody = R"POSTBODY( + R"FRAG( -)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) { - typedef std::pair UrlAndExpectedBody; - const std::vector testData{ + const std::vector testData{ { /* url */ "/ROOT/random?content=non-existent-book", /* expected body */ R"(

Not Found

@@ -442,13 +470,11 @@ TEST_F(ServerTest, 404WithBodyTesting) )" } }; - for ( const auto& urlAndExpectedBody : testData ) { - const std::string url = urlAndExpectedBody.first; - const std::string expectedBody = urlAndExpectedBody.second; - const TestContext ctx{ {"url", url} }; - const auto r = zfs1_->GET(url.c_str()); + for ( const auto& t : testData ) { + const TestContext ctx{ {"url", t.url} }; + const auto r = zfs1_->GET(t.url.c_str()); EXPECT_EQ(r->status, 404) << ctx; - EXPECT_EQ(r->body, makeExpected404Response(expectedBody)) << ctx; + EXPECT_EQ(r->body, t.expectedResponse()) << ctx; } } From 7a6562395a140dea199d3d4db2317930ccf6f9c1 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 11 Feb 2022 18:21:03 +0400 Subject: [PATCH 6/7] Testing of /ROOT/zimfile/invalid-article --- test/server.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/test/server.cpp b/test/server.cpp index 16b4e6aa1..8b0179934 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -322,17 +322,33 @@ TEST_F(ServerTest, 404) EXPECT_EQ(404, zfs1_->GET(url)->status) << "url: " << url; } -struct TestContentIn404HtmlResponse +class TestContentIn404HtmlResponse { +public: TestContentIn404HtmlResponse(const std::string& url, const std::string& expectedBody) : url(url) , expectedBody(expectedBody) {} - std::string url, expectedBody; + 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; std::string expectedResponse() const; + +private: + std::string hiddenBookNameInput() const; + std::string searchPatternInput() const; + std::string taskbarLinks() const; }; std::string TestContentIn404HtmlResponse::expectedResponse() const @@ -361,9 +377,6 @@ std::string TestContentIn404HtmlResponse::expectedResponse() const )FRAG", - R"FRAG( -)FRAG", - R"FRAG( @@ -385,12 +398,51 @@ std::string TestContentIn404HtmlResponse::expectedResponse() const }; return frag[0] + + hiddenBookNameInput() + frag[1] + + searchPatternInput() + frag[2] + + taskbarLinks() + frag[3] - + frag[4] + removeEOLWhitespaceMarkers(expectedBody) - + frag[5]; + + frag[4]; +} + +std::string TestContentIn404HtmlResponse::hiddenBookNameInput() const +{ + return bookName.empty() + ? "" + : R"()"; +} + +std::string TestContentIn404HtmlResponse::searchPatternInput() const +{ + return R"( +)"; +} + +std::string TestContentIn404HtmlResponse::taskbarLinks() const +{ + if ( bookName.empty() ) + return ""; + + return R"( + )"; } TEST_F(ServerTest, 404WithBodyTesting) @@ -447,6 +499,19 @@ TEST_F(ServerTest, 404WithBodyTesting)

)" }, + { /* url */ "/ROOT/zimfile/invalid-article", + /* 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. +

+

+ Make a full text search for invalid-article +

+)" }, + { /* url */ "/ROOT/raw/no-such-book/meta/Title", /* expected body */ R"(

Not Found

From 34d069e61ff09732819a9e83b716497b38a1171c Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 23 Jan 2022 22:29:48 +0400 Subject: [PATCH 7/7] Two more 404 error tests for the /raw endpoint --- test/server.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/server.cpp b/test/server.cpp index 8b0179934..d5e05a652 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -532,7 +532,33 @@ TEST_F(ServerTest, 404WithBodyTesting)

XYZ is not a valid request for raw content.

-)" } +)" }, + + { /* url */ "/ROOT/raw/zimfile/meta/invalid-metadata", + /* 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. +

+

+ Cannot find meta entry invalid-metadata +

+)" }, + + { /* url */ "/ROOT/raw/zimfile/content/invalid-article", + /* 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. +

+

+ Cannot find content entry invalid-article +

+)" }, }; for ( const auto& t : testData ) {