From 6b868975b341722bc44947ad2de278f16d6434f9 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 23 Jan 2022 21:00:19 +0400 Subject: [PATCH] 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 27d5a86b3..bff1efcdf 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; } }