From 1962262f94d4ad31db95e9dd0057e941cea95bdd Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 9 Mar 2022 15:38:45 +0100 Subject: [PATCH] Correctly handle invalid book. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- src/server/internalServer.cpp | 7 +------ test/server.cpp | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 14aaf3a68..282d622f7 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -558,12 +558,7 @@ std::unique_ptr 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."); } } diff --git a/test/server.cpp b/test/server.cpp index 73ad0f83d..97e112b8b 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -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 )" }, - - { /* 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"( -
Not found
-

- There is no article with the title "asdfqwerty" - and the fulltext search engine is not available for this content. -

-)" }, }; for ( const auto& t : testData ) { @@ -726,6 +716,26 @@ TEST_F(ServerTest, 400WithBodyTesting)

No query provided.

+)" }, + { /* url */ "/ROOT/search?content=non-existing-book&pattern=asdfqwerty", + expected_body==R"( +

Invalid request

+

+ The requested URL "/ROOT/search?content=non-existing-book&pattern=asdfqwerty" is not a valid request. +

+

+ The requested book doesn't exist. +

+)" }, + { /* url */ "/ROOT/search?content=non-existing-book&pattern=a\"