diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 3e0639650..f28c3df03 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -692,8 +692,9 @@ InternalServer::search_catalog(const RequestContext& request, } catch (...) {} opdsDumper.setTitle("Search result for " + query); std::vector bookIdsToDump = mp_library->filter(filter); - auto totalResults = bookIdsToDump.size(); - bookIdsToDump.erase(bookIdsToDump.begin(), bookIdsToDump.begin()+startIndex); + const auto totalResults = bookIdsToDump.size(); + const auto s = std::min(startIndex, totalResults); + bookIdsToDump.erase(bookIdsToDump.begin(), bookIdsToDump.begin()+s); if (count>0 && bookIdsToDump.size() > count) { bookIdsToDump.resize(count); } diff --git a/test/server.cpp b/test/server.cpp index 759c7e5b3..c9556c76e 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -846,3 +846,54 @@ TEST_F(LibraryServerTest, catalog_search_by_category) "\n" ); } + +TEST_F(LibraryServerTest, catalog_search_results_pagination) +{ + { + const auto r = zfs1_->GET("/catalog/search?count=1"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(maskVariableOPDSFeedData(r->body), + OPDS_FEED_TAG + " 12345678-90ab-cdef-1234-567890abcdef\n" + " Search result for <Empty query>\n" + " YYYY-MM-DDThh:mm:ssZ\n" + " 3\n" + " 0\n" + " 1\n" + CATALOG_LINK_TAGS + CHARLES_RAY_CATALOG_ENTRY + "\n" + ); + } + { + const auto r = zfs1_->GET("/catalog/search?start=1&count=1"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(maskVariableOPDSFeedData(r->body), + OPDS_FEED_TAG + " 12345678-90ab-cdef-1234-567890abcdef\n" + " Search result for <Empty query>\n" + " YYYY-MM-DDThh:mm:ssZ\n" + " 3\n" + " 1\n" + " 1\n" + CATALOG_LINK_TAGS + RAY_CHARLES_CATALOG_ENTRY + "\n" + ); + } + { + const auto r = zfs1_->GET("/catalog/search?start=100&count=10"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(maskVariableOPDSFeedData(r->body), + OPDS_FEED_TAG + " 12345678-90ab-cdef-1234-567890abcdef\n" + " Search result for <Empty query>\n" + " YYYY-MM-DDThh:mm:ssZ\n" + " 3\n" + " 100\n" + " 0\n" + CATALOG_LINK_TAGS + "\n" + ); + } +}