From 1139f2cb4ccfc9bbbbaaf18aa23939bcbb16acc7 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 22 Jun 2022 17:03:15 +0400 Subject: [PATCH] Testing of static front-end resource customization One important missing test is that the content of the customized resource is read from storage every time rather than once. Testing that requirement would involve creating temporary files which is a little more work. --- test/data/customized_resources.txt | 5 +++ test/data/helloworld.txt | 1 + test/data/welcome.html | 1 + test/meson.build | 5 ++- test/server.cpp | 62 ++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 test/data/customized_resources.txt create mode 100644 test/data/helloworld.txt create mode 100644 test/data/welcome.html diff --git a/test/data/customized_resources.txt b/test/data/customized_resources.txt new file mode 100644 index 000000000..5c5f2b72e --- /dev/null +++ b/test/data/customized_resources.txt @@ -0,0 +1,5 @@ +/non-existent-item text/plain ./test/helloworld.txt +/ text/html ./test/welcome.html +/skin/index.css application/json ./test/helloworld.txt +/zimfile/A/Ray_Charles ray/charles ./test/welcome.html +/search text/html ./test/helloworld.txt diff --git a/test/data/helloworld.txt b/test/data/helloworld.txt new file mode 100644 index 000000000..cd0875583 --- /dev/null +++ b/test/data/helloworld.txt @@ -0,0 +1 @@ +Hello world! diff --git a/test/data/welcome.html b/test/data/welcome.html new file mode 100644 index 000000000..7612b5763 --- /dev/null +++ b/test/data/welcome.html @@ -0,0 +1 @@ +Welcome diff --git a/test/meson.build b/test/meson.build index 0783691f8..3137163a7 100644 --- a/test/meson.build +++ b/test/meson.build @@ -36,7 +36,10 @@ if gtest_dep.found() and not meson.is_cross_build() 'zimfile&other.zim', 'corner_cases.zim', 'poor.zim', - 'library.xml' + 'library.xml', + 'customized_resources.txt', + 'helloworld.txt', + 'welcome.html', ] foreach file : data_files # configure_file(input : 'data/' + file, diff --git a/test/server.cpp b/test/server.cpp index 88ace8067..9d7c38293 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -287,6 +287,68 @@ TEST_F(ServerTest, 404) } } +struct CustomizedServerTest : ServerTest +{ + void SetUp() + { + setenv("KIWIX_SERVE_CUSTOMIZED_RESOURCES", "./test/customized_resources.txt", 1); + ServerTest::SetUp(); + } +}; + +typedef std::vector StringCollection; + +std::string getHeaderValue(const Headers& headers, const std::string& name) +{ + const auto er = headers.equal_range(name); + const auto n = std::distance(er.first, er.second); + if (n == 0) + throw std::runtime_error("Missing header: " + name); + if (n > 1) + throw std::runtime_error("Multiple occurrences of header: " + name); + return er.first->second; +} + +TEST_F(CustomizedServerTest, NewResourcesCanBeAdded) +{ + // ServerTest.404 verifies that "/ROOT/non-existent-item" doesn't exist + const auto r = zfs1_->GET("/ROOT/non-existent-item"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(getHeaderValue(r->headers, "Content-Type"), "text/plain"); + EXPECT_EQ(r->body, "Hello world!\n"); +} + +TEST_F(CustomizedServerTest, ContentOfAnyServableUrlCanBeOverriden) +{ + { + const auto r = zfs1_->GET("/ROOT/"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(getHeaderValue(r->headers, "Content-Type"), "text/html"); + EXPECT_EQ(r->body, "Welcome\n"); + } + + { + const auto r = zfs1_->GET("/ROOT/skin/index.css"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(getHeaderValue(r->headers, "Content-Type"), "application/json"); + EXPECT_EQ(r->body, "Hello world!\n"); + } + + { + const auto r = zfs1_->GET("/ROOT/zimfile/A/Ray_Charles"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(getHeaderValue(r->headers, "Content-Type"), "ray/charles"); + EXPECT_EQ(r->body, "Welcome\n"); + } + + { + const auto r = zfs1_->GET("/ROOT/search?pattern=la+femme"); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(getHeaderValue(r->headers, "Content-Type"), "text/html"); + EXPECT_EQ(r->body, "Hello world!\n"); + } +} + namespace TestingOfHtmlResponses {