From cb02dbd92a00df0a3a73394f04ee3d490a62ad33 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 21 Oct 2022 19:49:43 +0400 Subject: [PATCH] RequestContext preserves the exact query string Before this change RequestContext::get_query() returned a reordered query string (alphabetically sorted by the parameter names). This fix facilitiates testing of responses where the request URL appears in the response. --- src/server/request_context.cpp | 8 ++++++++ src/server/request_context.h | 5 ++--- test/library_server.cpp | 8 ++++---- test/server.cpp | 10 +++++----- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 0b9a1a539..5e191afa7 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -107,6 +107,14 @@ MHD_Result RequestContext::fill_argument(void *__this, enum MHD_ValueKind kind, { RequestContext *_this = static_cast(__this); _this->arguments[key].push_back(value == nullptr ? "" : value); + if ( ! _this->queryString.empty() ) { + _this->queryString += "&"; + } + _this->queryString += key; + if ( value ) { + _this->queryString += "="; + _this->queryString += value; + } return MHD_YES; } diff --git a/src/server/request_context.h b/src/server/request_context.h index f63f89810..de02d465f 100644 --- a/src/server/request_context.h +++ b/src/server/request_context.h @@ -92,9 +92,7 @@ class RequestContext { std::string get_url_part(int part) const; std::string get_full_url() const; - std::string get_query(bool mustEncode = false) const { - return get_query([](const std::string& key) {return true;}, mustEncode); - } + std::string get_query() const { return queryString; } template std::string get_query(F filter, bool mustEncode) const { @@ -132,6 +130,7 @@ class RequestContext { ByteRange byteRange_; std::map headers; std::map> arguments; + std::string queryString; private: // functions static MHD_Result fill_header(void *, enum MHD_ValueKind, const char*, const char*); diff --git a/test/library_server.cpp b/test/library_server.cpp index fda5916b4..4afcbc10b 100644 --- a/test/library_server.cpp +++ b/test/library_server.cpp @@ -359,7 +359,7 @@ TEST_F(LibraryServerTest, catalog_search_results_pagination) EXPECT_EQ(maskVariableOPDSFeedData(r->body), OPDS_FEED_TAG " 12345678-90ab-cdef-1234-567890abcdef\n" - " Filtered zims (count=1&start=1)\n" + " Filtered zims (start=1&count=1)\n" " YYYY-MM-DDThh:mm:ssZ\n" " 3\n" " 1\n" @@ -375,7 +375,7 @@ TEST_F(LibraryServerTest, catalog_search_results_pagination) EXPECT_EQ(maskVariableOPDSFeedData(r->body), OPDS_FEED_TAG " 12345678-90ab-cdef-1234-567890abcdef\n" - " Filtered zims (count=10&start=100)\n" + " Filtered zims (start=100&count=10)\n" " YYYY-MM-DDThh:mm:ssZ\n" " 3\n" " 100\n" @@ -638,8 +638,8 @@ TEST_F(LibraryServerTest, catalog_v2_entries_filtered_by_range) const auto r = zfs1_->GET("/ROOT/catalog/v2/entries?start=1&count=1"); EXPECT_EQ(r->status, 200); EXPECT_EQ(maskVariableOPDSFeedData(r->body), - CATALOG_V2_ENTRIES_PREAMBLE("?count=1&start=1") - " Filtered Entries (count=1&start=1)\n" + CATALOG_V2_ENTRIES_PREAMBLE("?start=1&count=1") + " Filtered Entries (start=1&count=1)\n" " YYYY-MM-DDThh:mm:ssZ\n" " 3\n" " 1\n" diff --git a/test/server.cpp b/test/server.cpp index c774d0f1a..76541d0b3 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -839,7 +839,7 @@ TEST_F(ServerTest, Http400HtmlError) expected_body==R"(

Invalid request

- The requested URL "/ROOT/search?books.filter.lang=eng&pattern=" is not a valid request. + The requested URL "/ROOT/search?books.filter.lang=eng&pattern" is not a valid request.

No query provided. @@ -896,21 +896,21 @@ TEST_F(ServerTest, HttpXmlError) /* HTTP status code */ 400, /* expected response XML */ R"( Invalid request -The requested URL "/ROOT/search?content=zimfile&format=xml" is not a valid request. +The requested URL "/ROOT/search?format=xml&content=zimfile" is not a valid request. No query provided. )" }, { /* url */ "/ROOT/search?format=xml&content=non-existing-book&pattern=asdfqwerty", /* HTTP status code */ 400, /* expected response XML */ R"( Invalid request -The requested URL "/ROOT/search?content=non-existing-book&format=xml&pattern=asdfqwerty" is not a valid request. +The requested URL "/ROOT/search?format=xml&content=non-existing-book&pattern=asdfqwerty" is not a valid request. No such book: non-existing-book )" }, { /* url */ "/ROOT/search?format=xml&content=non-existing-book&pattern=a\"