mirror of https://github.com/kiwix/libkiwix.git
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.
This commit is contained in:
parent
0086049d4f
commit
1139f2cb4c
|
@ -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
|
|
@ -0,0 +1 @@
|
||||||
|
Hello world!
|
|
@ -0,0 +1 @@
|
||||||
|
<html><head></head><body>Welcome</body></html>
|
|
@ -36,7 +36,10 @@ if gtest_dep.found() and not meson.is_cross_build()
|
||||||
'zimfile&other.zim',
|
'zimfile&other.zim',
|
||||||
'corner_cases.zim',
|
'corner_cases.zim',
|
||||||
'poor.zim',
|
'poor.zim',
|
||||||
'library.xml'
|
'library.xml',
|
||||||
|
'customized_resources.txt',
|
||||||
|
'helloworld.txt',
|
||||||
|
'welcome.html',
|
||||||
]
|
]
|
||||||
foreach file : data_files
|
foreach file : data_files
|
||||||
# configure_file(input : 'data/' + file,
|
# configure_file(input : 'data/' + file,
|
||||||
|
|
|
@ -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<std::string> 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, "<html><head></head><body>Welcome</body></html>\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, "<html><head></head><body>Welcome</body></html>\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
|
namespace TestingOfHtmlResponses
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue