Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unittest for suggestions #709

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 92 additions & 1 deletion test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ Headers invariantHeaders(Headers headers)
return headers;
}

// Output generated via mustache templates sometimes contains end-of-line
// whitespace. This complicates representing the expected output of a unit-test
// as C++ raw strings in editors that are configured to delete EOL whitespace.
// A workaround is to put special markers (//EOLWHITESPACEMARKER) at the end
// of such lines in the expected output string and remove them at runtime.
// This is exactly what this function is for.
Comment on lines +48 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is silly but I don't know how to do otherwise.

std::string removeEOLWhitespaceMarkers(const std::string& s)
{
const std::regex pattern("//EOLWHITESPACEMARKER");
return std::regex_replace(s, pattern, "");
}


class ZimFileServer
{
Expand Down Expand Up @@ -444,7 +456,7 @@ TEST_F(ServerTest, ETagOfUncompressibleContentIsNotAffectedByAcceptEncoding)
// NOTE: The "Date" header (which should belong to that list as required
// NOTE: by RFC 7232) is not included (since the result of this function
// NOTE: will be used to check the equality of headers from the 200 and 304
// NOTe: responses).
// NOTE: responses).
Headers special304Headers(const httplib::Response& r)
{
Headers result;
Expand Down Expand Up @@ -625,6 +637,85 @@ TEST_F(ServerTest, RangeHeaderIsCaseInsensitive)
}
}

TEST_F(ServerTest, suggestions)
{
typedef std::pair<std::string, std::string> UrlAndExpectedResponse;
const std::vector<UrlAndExpectedResponse> testData{
{ /* url: */ "/ROOT/suggest?content=zimfile&term=thing",
R"EXPECTEDRESPONSE([
{
"value" : "Doing His Thing",
"label" : "Doing His &lt;b&gt;Thing&lt;/b&gt;",
"kind" : "path"
, "path" : "A/Doing_His_Thing"
},
{
"value" : "We Didn&apos;t See a Thing",
"label" : "We Didn&apos;t See a &lt;b&gt;Thing&lt;/b&gt;",
"kind" : "path"
, "path" : "A/We_Didn&apos;t_See_a_Thing"
},
{
"value" : "thing ",
"label" : "containing &apos;thing&apos;...",
"kind" : "pattern"
//EOLWHITESPACEMARKER
}
]
)EXPECTEDRESPONSE"
},
{ /* url: */ "/ROOT/suggest?content=zimfile&term=movie",
R"EXPECTEDRESPONSE([
{
"value" : "Ray (movie)",
"label" : "Ray (&lt;b&gt;movie&lt;/b&gt;)",
"kind" : "path"
, "path" : "A/Ray_(movie)"
},
{
"value" : "movie ",
"label" : "containing &apos;movie&apos;...",
"kind" : "pattern"
//EOLWHITESPACEMARKER
}
]
)EXPECTEDRESPONSE"
},
{ /* url: */ "/ROOT/suggest?content=zimfile&term=abracadabra",
R"EXPECTEDRESPONSE([
{
"value" : "abracadabra ",
"label" : "containing &apos;abracadabra&apos;...",
"kind" : "pattern"
//EOLWHITESPACEMARKER
}
]
)EXPECTEDRESPONSE"
},
{ // Test handling of & (%26 when url-encoded) in the search string
/* url: */ "/ROOT/suggest?content=zimfile&term=A%26B",
R"EXPECTEDRESPONSE([
{
"value" : "A&amp;B ",
"label" : "containing &apos;A&amp;B&apos;...",
"kind" : "pattern"
//EOLWHITESPACEMARKER
}
]
)EXPECTEDRESPONSE"
},
};

for ( const auto& urlAndExpectedResponse : testData ) {
const std::string url = urlAndExpectedResponse.first;
const std::string expectedResponse = urlAndExpectedResponse.second;
const TestContext ctx{ {"url", url} };
const auto r = zfs1_->GET(url.c_str());
EXPECT_EQ(r->status, 200) << ctx;
EXPECT_EQ(r->body, removeEOLWhitespaceMarkers(expectedResponse)) << ctx;
}
}

////////////////////////////////////////////////////////////////////////////////
// Testing of the library-related functionality of the server
////////////////////////////////////////////////////////////////////////////////
Expand Down