Correctly handle invalid book.

If user request for a non existent book, we must return a 400 page.
(This is done by throwing a `std::invalid_argument` and let the catch
handle it)
This commit is contained in:
Matthieu Gautier 2022-03-09 15:38:45 +01:00
parent 7407f30790
commit 1962262f94
2 changed files with 23 additions and 18 deletions

View File

@ -558,12 +558,7 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
bookId = mp_nameMapper->getIdForName(searchInfo.bookName);
archive = mp_library->getArchiveById(bookId);
} catch (const std::out_of_range&) {
auto data = get_default_data();
data.set("pattern", encodeDiples(searchInfo.pattern));
data.set("root", m_root);
auto response = ContentResponse::build(*this, RESOURCE::templates::no_search_result_html, data, "text/html; charset=utf-8");
response->set_code(MHD_HTTP_NOT_FOUND);
return withTaskbarInfo(searchInfo.bookName, archive.get(), std::move(response));
throw std::invalid_argument("The requested book doesn't exist.");
}
}

View File

@ -290,6 +290,8 @@ TEST_F(ServerTest, UncompressibleContentIsNotCompressed)
const char* urls400[] = {
"/ROOT/search",
"/ROOT/search?content=zimfile",
"/ROOT/search?content=non-existing-book&pattern=asdfqwerty",
"/ROOT/search?content=non-existing-book&pattern=asd<qwerty",
"/ROOT/search?pattern"
};
@ -315,7 +317,6 @@ const char* urls404[] = {
"/ROOT/meta?content=non-existent-book&name=title",
"/ROOT/random",
"/ROOT/random?content=non-existent-book",
"/ROOT/search?content=non-existing-book&pattern=asdfqwerty",
"/ROOT/suggest",
"/ROOT/suggest?content=non-existent-book&term=abcd",
"/ROOT/catch/external",
@ -682,17 +683,6 @@ TEST_F(ServerTest, 404WithBodyTesting)
Cannot find content entry invalid-article
</p>
)" },
{ /* url */ "/ROOT/search?content=non-existent-book&pattern=asdfqwerty",
expected_page_title=="Fulltext search unavailable" &&
expected_css_url=="/ROOT/skin/search_results.css" &&
expected_body==R"(
<div class="header">Not found</div>
<p>
There is no article with the title <b> "asdfqwerty"</b>
and the fulltext search engine is not available for this content.
</p>
)" },
};
for ( const auto& t : testData ) {
@ -726,6 +716,26 @@ TEST_F(ServerTest, 400WithBodyTesting)
<p>
No query provided.
</p>
)" },
{ /* url */ "/ROOT/search?content=non-existing-book&pattern=asdfqwerty",
expected_body==R"(
<h1>Invalid request</h1>
<p>
The requested URL "/ROOT/search?content=non-existing-book&pattern=asdfqwerty" is not a valid request.
</p>
<p>
The requested book doesn't exist.
</p>
)" },
{ /* url */ "/ROOT/search?content=non-existing-book&pattern=a\"<script foo>",
expected_body==R"(
<h1>Invalid request</h1>
<p>
The requested URL "/ROOT/search?content=non-existing-book&pattern=a"&lt;script foo&gt;" is not a valid request.
</p>
<p>
The requested book doesn't exist.
</p>
)" },
// There is a flaw in our way to handle query string, we cannot differenciate
// between `pattern` and `pattern=`