157 lines
4.2 KiB
C++
157 lines
4.2 KiB
C++
/* CgiApplication-test.cpp
|
|
* Copyright © 2023 Saul D. Beniquez
|
|
* License: Mozilla Public License v. 2.0
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
|
* v.2.0. If a copy of the MPL was not distributed with this file, You can
|
|
* obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "CgiApplication.hpp"
|
|
#include "Exception.hpp"
|
|
#include "IRouteHandler.hpp"
|
|
|
|
#include "tests-common.hpp"
|
|
#include <catch2/matchers/catch_matchers_string.hpp>
|
|
|
|
BEGIN_TEST_SUITE("CgiApplication")
|
|
{
|
|
using namespace mdml;
|
|
struct SimulatedLaunch {
|
|
static const char* argv[];
|
|
static const char* env[];
|
|
};
|
|
inline const char* SimulatedLaunch::argv[] = { "param1",
|
|
"param2",
|
|
"param3" };
|
|
inline const char* SimulatedLaunch::env[] = {
|
|
"PATH=/usr/bin",
|
|
"VAR2=TWO",
|
|
"REQUEST_URI=markdown?msg=hello-world",
|
|
nullptr
|
|
};
|
|
|
|
// Define a test oute handler for the "/markdown" route
|
|
class ExampleRouteHandler : public mdml::IRouteHandler {
|
|
public:
|
|
auto process(
|
|
const std::string& name, const std::string& request_uri
|
|
) -> mdml::Result<std::string> override
|
|
{
|
|
return mdml::Result(
|
|
mdml::NoError, std::string("processed")
|
|
);
|
|
}
|
|
~ExampleRouteHandler() override {}
|
|
};
|
|
|
|
struct TestFixture {
|
|
const char* envp[4] = { "PATH=/usr/bin",
|
|
"VAR2=TWO",
|
|
"REQUEST_URI=markdown?msg=hello-world",
|
|
nullptr };
|
|
TestFixture() : cgi_app(1, SimulatedLaunch::argv, envp) {}
|
|
|
|
CgiApplication cgi_app;
|
|
};
|
|
|
|
TEST("CgiApplication Constructor Test")
|
|
{
|
|
|
|
CgiApplication test(
|
|
1, SimulatedLaunch::argv, SimulatedLaunch::env
|
|
);
|
|
};
|
|
|
|
TEST("CgiApplication RegisterRoutes Test")
|
|
{
|
|
|
|
CgiApplication test_app(
|
|
1, SimulatedLaunch::argv, SimulatedLaunch::env
|
|
);
|
|
|
|
mdml::CgiApplication::RouteDictionary routes;
|
|
routes.emplace("test", new ExampleRouteHandler());
|
|
routes.emplace("test2", new ExampleRouteHandler());
|
|
routes.emplace("fasdfdshjk", new ExampleRouteHandler());
|
|
|
|
test_app.Routes = std::move(routes);
|
|
|
|
REQUIRE(routes.size() < 1);
|
|
|
|
REQUIRE(test_app.Routes.size() == 3);
|
|
REQUIRE(test_app.Routes.at("test") != nullptr);
|
|
};
|
|
|
|
TEST("CgiApplication::processRequest no REQUEST_URI variable")
|
|
{
|
|
const char* no_request_env[] = { "PATH=/usr/bin",
|
|
"VAR2=TWO",
|
|
nullptr };
|
|
CgiApplication test_app(
|
|
1, SimulatedLaunch::argv, no_request_env
|
|
);
|
|
REQUIRE_THROWS_AS(
|
|
[&]() { test_app.processRequest(); }(), IOCore::Exception
|
|
);
|
|
}
|
|
|
|
FIXTURE_TEST("CgiApplication processRequest with valid route")
|
|
{
|
|
|
|
// Add the test route handler to the Routes dictionary
|
|
auto handler = std::make_shared<ExampleRouteHandler>();
|
|
cgi_app.Routes["markdown"] = handler;
|
|
|
|
auto result = cgi_app.processRequest();
|
|
|
|
// Add assertions to check the result
|
|
CHECK(result.getErrorData() == "processed");
|
|
REQUIRE(result.isError() == false);
|
|
}
|
|
|
|
FIXTURE_TEST("CgiApplication processRequest with unknown route")
|
|
{
|
|
|
|
// Remove the "markdown" route from the Routes
|
|
// dictionary
|
|
for (auto& [key, value] : cgi_app.Routes) {
|
|
if (value.get() != nullptr) {
|
|
value.reset();
|
|
}
|
|
cgi_app.Routes.erase(key);
|
|
}
|
|
|
|
auto result = cgi_app.processRequest();
|
|
|
|
// Add assertions to check the result
|
|
CHECK(result.getErrorData() != "Success");
|
|
REQUIRE(result.isError() == true);
|
|
}
|
|
|
|
FIXTURE_TEST("CgiApplication processRequest Route throws exception")
|
|
{
|
|
// Define a test oute handler for the "/markdown" route
|
|
class BadRouteHandler : public IRouteHandler {
|
|
public:
|
|
auto process(
|
|
const std::string& name,
|
|
const std::string& request_uri
|
|
) -> Result<std::string> override
|
|
{
|
|
throw std::runtime_error("Error");
|
|
return { mdml::Error,
|
|
"This line never executes" };
|
|
}
|
|
~BadRouteHandler() override = default;
|
|
};
|
|
auto handler = std::make_shared<BadRouteHandler>();
|
|
cgi_app.Routes["markdown"] = handler;
|
|
|
|
REQUIRE_THROWS_AS(cgi_app.processRequest(), IOCore::Exception);
|
|
// REQUIRE(cgi_app.processRequest().isError());
|
|
}
|
|
}
|
|
// clang-format off
|
|
// vim: set foldmethod=marker foldmarker=@{,@} textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :
|