2017-05-06 20:49:46 +00:00
|
|
|
/* Copyright 2017, Michele Santullo
|
|
|
|
* This file is part of "tawashi".
|
|
|
|
*
|
|
|
|
* "tawashi" is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* "tawashi" is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with "tawashi". If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "catch.hpp"
|
2017-05-15 08:18:11 +00:00
|
|
|
#include "cgi_env.hpp"
|
2017-05-06 20:49:46 +00:00
|
|
|
#include "index_response.hpp"
|
|
|
|
#include "ini_file.hpp"
|
|
|
|
#include "settings_bag.hpp"
|
|
|
|
#include "safe_stack_object.hpp"
|
|
|
|
#include <sstream>
|
|
|
|
#include <utility>
|
|
|
|
#include <string>
|
2017-05-22 19:08:28 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2017-05-06 20:49:46 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const char g_mustache_html[] =
|
|
|
|
R"(<body>
|
|
|
|
<form action="{{base_uri}}/paste.cgi" method="post" accept-charset="UTF-8">
|
|
|
|
|
|
|
|
<div id="content">
|
|
|
|
<textarea type="text" id="PasteTextBox" name="pastie" placeholder=">Text..." autofocus required autocomplete="off" name="text"></textarea>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
)";
|
|
|
|
|
|
|
|
const char g_expected_response[] =
|
|
|
|
R"(Content-type: text/html
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<form action="http://127.0.0.1/paste.cgi" method="post" accept-charset="UTF-8">
|
|
|
|
|
|
|
|
<div id="content">
|
|
|
|
<textarea type="text" id="PasteTextBox" name="pastie" placeholder=">Text..." autofocus required autocomplete="off" name="text"></textarea>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
)";
|
|
|
|
} //unnamed namespace
|
|
|
|
|
|
|
|
namespace tawashi {
|
|
|
|
class IndexResponseCustomMustache : public IndexResponse {
|
|
|
|
public:
|
|
|
|
IndexResponseCustomMustache (
|
|
|
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
|
|
|
std::ostream* parStreamOut,
|
|
|
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
|
|
|
) :
|
2017-05-12 21:18:03 +00:00
|
|
|
IndexResponse(parSettings, parStreamOut, parCgiEnv)
|
2017-05-06 20:49:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string on_mustache_retrieve() override {
|
|
|
|
return g_mustache_html;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} //namespace tawashi
|
|
|
|
|
|
|
|
TEST_CASE ("Index response", "[index][response]") {
|
|
|
|
using curry::SafeStackObject;
|
|
|
|
|
2017-05-22 19:08:28 +00:00
|
|
|
auto statuslog = spdlog::stdout_logger_st("statuslog");
|
|
|
|
|
2017-05-15 08:18:11 +00:00
|
|
|
const char* const env_raw[] = {
|
|
|
|
"AUTH_TYPE=",
|
|
|
|
"CONTENT_TYPE=",
|
2017-05-19 09:01:22 +00:00
|
|
|
"PATH_INFO=/",
|
2017-05-15 08:18:11 +00:00
|
|
|
"PATH_TRANSLATED=",
|
|
|
|
"QUERY_STRING=index.cgi"
|
|
|
|
"REMOTE_ADDR=",
|
|
|
|
"REMOTE_HOST=",
|
|
|
|
"REMOTE_IDENT=",
|
|
|
|
"REMOTE_USER=",
|
|
|
|
"REQUEST_METHOD=GET"
|
|
|
|
"SCRIPT_NAME=",
|
|
|
|
"SERVER_NAME=test_server"
|
|
|
|
"SERVER_SOFTWARE=",
|
|
|
|
"CONTENT_LENGTH=",
|
|
|
|
"SERVER_PORT=80",
|
2017-05-19 09:01:22 +00:00
|
|
|
"HTTPS=",
|
2017-05-24 21:25:14 +00:00
|
|
|
"REQUEST_METHOD=GET",
|
2017-05-15 08:18:11 +00:00
|
|
|
nullptr
|
|
|
|
};
|
2017-05-19 09:01:22 +00:00
|
|
|
SafeStackObject<tawashi::cgi::Env> fake_env(env_raw, "/");
|
2017-05-06 20:49:46 +00:00
|
|
|
|
|
|
|
std::string tawashi_settings(
|
|
|
|
"[tawashi]\n"
|
2017-05-19 09:01:22 +00:00
|
|
|
" host_name = 127.0.0.1\n"
|
2017-05-06 20:49:46 +00:00
|
|
|
" website_root = /home/michele/dev/code/cpp/tawashi/html\n"
|
|
|
|
" logging_level = debug\n"
|
|
|
|
" langmap_dir = /usr/share/source-highlight\n"
|
2017-05-19 09:01:22 +00:00
|
|
|
" host_path = /\n"
|
|
|
|
" host_port =\n"
|
2017-05-06 20:49:46 +00:00
|
|
|
);
|
|
|
|
SafeStackObject<tawashi::IniFile> ini(std::move(tawashi_settings));
|
|
|
|
SafeStackObject<tawashi::SettingsBag> settings(ini);
|
|
|
|
|
|
|
|
std::stringstream response_stream;
|
|
|
|
|
2017-05-12 21:18:03 +00:00
|
|
|
tawashi::IndexResponseCustomMustache response(settings, &response_stream, fake_env);
|
2017-05-06 20:49:46 +00:00
|
|
|
response.send();
|
|
|
|
|
|
|
|
response_stream.seekg(0);
|
|
|
|
CHECK(response_stream.str() == g_expected_response);
|
|
|
|
}
|