From b9323f17bbef027d0bcf0d0fd54b4b1e91efdc47 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 5 Dec 2023 16:34:09 +0400 Subject: [PATCH] Introduced testing of HTTP response utils --- src/server/request_context.h | 2 +- src/server/response.h | 5 ++++ test/meson.build | 3 ++- test/response.cpp | 46 ++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 test/response.cpp diff --git a/src/server/request_context.h b/src/server/request_context.h index d5ab7b515..80d67ea6f 100644 --- a/src/server/request_context.h +++ b/src/server/request_context.h @@ -29,7 +29,7 @@ #include #include "byte_range.h" -#include "tools/stringTools.h" +#include "../tools/stringTools.h" extern "C" { #include "microhttpd_wrapper.h" diff --git a/src/server/response.h b/src/server/response.h index b1636fa9c..643f67f88 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -101,6 +101,9 @@ class ContentResponse : public Response { kainjow::mustache::data data, const std::string& mimetype); + const std::string& getContent() const { return m_content; } + const std::string& getMimeType() const { return m_mimeType; } + private: MHD_Response* create_mhd_response(const RequestContext& request); @@ -135,6 +138,8 @@ public: // functions protected: // functions std::string getMessage(const std::string& msgId) const; + +public: virtual std::unique_ptr generateResponseObject() const; public: //data diff --git a/test/meson.build b/test/meson.build index 5ed11246c..78446f473 100644 --- a/test/meson.build +++ b/test/meson.build @@ -14,7 +14,8 @@ tests = [ 'opds_catalog', 'server_helper', 'lrucache', - 'i18n' + 'i18n', + 'response' ] if build_machine.system() != 'windows' diff --git a/test/response.cpp b/test/response.cpp new file mode 100644 index 000000000..5d5efe649 --- /dev/null +++ b/test/response.cpp @@ -0,0 +1,46 @@ +#include "../src/server/response.h" +#include "gtest/gtest.h" + +#include "../src/server/request_context.h" + +namespace +{ + +using namespace kiwix; + +RequestContext makeHttpGetRequest(const std::string& url) +{ + return RequestContext(nullptr, "", url, "GET", "1.1"); +} + +std::string getResponseContent(const ContentResponseBlueprint& crb) +{ + return crb.generateResponseObject()->getContent(); +} + +} // unnamed namespace + + + +TEST(HTTPErrorResponse, shouldBeInEnglishByDefault) { + const RequestContext req = makeHttpGetRequest("/asdf"); + HTTPErrorResponse errResp(req, MHD_HTTP_NOT_FOUND, + "404-page-title", + "404-page-heading", + "/css/error.css"); + + EXPECT_EQ(getResponseContent(errResp), +R"( + + + + Content not found + + + +

Not Found

+ + + +)"); +}