mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-02-09 09:23:56 +00:00
Add unit test for IndexResponse.
This commit is contained in:
parent
f5f19350ea
commit
df384f5128
7 changed files with 312 additions and 21 deletions
|
@ -8,6 +8,8 @@ add_executable(${PROJECT_NAME}
|
|||
)
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PRIVATE tawashi_implem
|
||||
#hack - add duckhandy to the project instead of picking from inside redis
|
||||
PRIVATE duckhandy
|
||||
)
|
||||
|
||||
set_target_properties(
|
||||
|
@ -17,8 +19,6 @@ set_target_properties(
|
|||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PRIVATE ${TAWASHI_GEN_INCLUDE_DIR}
|
||||
#hack - add duckhandy to the project instead of picking from inside redis
|
||||
PRIVATE ${TAWASHI_SOURCE_ROOT}/lib/incredis/lib/duckhandy/include
|
||||
)
|
||||
install(TARGETS ${PROJECT_NAME}
|
||||
LIBRARY DESTINATION lib
|
||||
|
|
|
@ -23,3 +23,10 @@
|
|||
#define VERSION_MAJOR @PROJECT_VERSION_MAJOR@
|
||||
#define VERSION_MINOR @PROJECT_VERSION_MINOR@
|
||||
#define VERSION_PATCH @PROJECT_VERSION_PATCH@
|
||||
#cmakedefine BUILD_TESTING
|
||||
|
||||
#if defined(BUILD_TESTING)
|
||||
# define virtual_testing virtual
|
||||
#else
|
||||
# define virtual_testing
|
||||
#endif
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "duckhandy/compatibility.h"
|
||||
#include "escapist.hpp"
|
||||
#include "kakoune/safe_ptr.hh"
|
||||
#include "tawashiConfig.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
|
@ -42,27 +43,27 @@ namespace tawashi {
|
|||
typedef boost::container::flat_map<std::string, std::string> GetMapType;
|
||||
|
||||
Env();
|
||||
~Env() noexcept;
|
||||
virtual_testing ~Env() noexcept;
|
||||
|
||||
const std::string& auth_type() const;
|
||||
std::size_t content_length() const;
|
||||
const std::string& content_type() const;
|
||||
boost::optional<VersionInfo> gateway_interface() const a_pure;
|
||||
const std::string& path_info() const;
|
||||
const std::string& path_translated() const;
|
||||
const std::string& query_string() const;
|
||||
const std::string& remote_addr() const;
|
||||
const std::string& remote_host() const;
|
||||
const std::string& remote_ident() const;
|
||||
const std::string& remote_user() const;
|
||||
const std::string& request_method() const;
|
||||
const std::string& script_name() const;
|
||||
const std::string& server_name() const;
|
||||
uint16_t server_port() const a_pure;
|
||||
boost::optional<VersionInfo> server_protocol() const a_pure;
|
||||
const std::string& server_software() const;
|
||||
virtual_testing const std::string& auth_type() const;
|
||||
virtual_testing std::size_t content_length() const;
|
||||
virtual_testing const std::string& content_type() const;
|
||||
virtual_testing boost::optional<VersionInfo> gateway_interface() const a_pure;
|
||||
virtual_testing const std::string& path_info() const;
|
||||
virtual_testing const std::string& path_translated() const;
|
||||
virtual_testing const std::string& query_string() const;
|
||||
virtual_testing const std::string& remote_addr() const;
|
||||
virtual_testing const std::string& remote_host() const;
|
||||
virtual_testing const std::string& remote_ident() const;
|
||||
virtual_testing const std::string& remote_user() const;
|
||||
virtual_testing const std::string& request_method() const;
|
||||
virtual_testing const std::string& script_name() const;
|
||||
virtual_testing const std::string& server_name() const;
|
||||
virtual_testing uint16_t server_port() const a_pure;
|
||||
virtual_testing boost::optional<VersionInfo> server_protocol() const a_pure;
|
||||
virtual_testing const std::string& server_software() const;
|
||||
|
||||
GetMapType query_string_split() const a_pure;
|
||||
virtual_testing GetMapType query_string_split() const a_pure;
|
||||
|
||||
std::ostream& print_all (std::ostream& parStream, const char* parNewline) const;
|
||||
|
||||
|
|
|
@ -5,15 +5,19 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||
|
||||
add_executable(${PROJECT_NAME}
|
||||
check.cpp
|
||||
fake_cgi_env.cpp
|
||||
test_ini_file.cpp
|
||||
test_settings_bag.cpp
|
||||
test_index_response.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PRIVATE ${TAWASHI_SOURCE_ROOT}/lib/Catch/single_include
|
||||
PRIVATE ${TAWASHI_GEN_INCLUDE_DIR}
|
||||
)
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PRIVATE tawashi_implem
|
||||
PRIVATE duckhandy
|
||||
)
|
||||
|
||||
add_test(
|
||||
|
|
96
test/unit/fake_cgi_env.cpp
Normal file
96
test/unit/fake_cgi_env.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
/* 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 "fake_cgi_env.hpp"
|
||||
#include "duckhandy/lexical_cast.hpp"
|
||||
#include <cassert>
|
||||
|
||||
namespace tawashi {
|
||||
namespace cgi {
|
||||
FakeEnv::FakeEnv (std::string&& parVariablesIni) :
|
||||
m_variables(std::move(parVariablesIni)),
|
||||
m_auth_type(m_variables.parsed().at("fake_env").at("auth_type")),
|
||||
m_content_type(m_variables.parsed().at("fake_env").at("content_type")),
|
||||
m_path_info(m_variables.parsed().at("fake_env").at("path_info")),
|
||||
m_path_translated(m_variables.parsed().at("fake_env").at("path_translated")),
|
||||
m_query_string(m_variables.parsed().at("fake_env").at("query_string")),
|
||||
m_remote_addr(m_variables.parsed().at("fake_env").at("remote_addr")),
|
||||
m_remote_host(m_variables.parsed().at("fake_env").at("remote_host")),
|
||||
m_remote_ident(m_variables.parsed().at("fake_env").at("remote_ident")),
|
||||
m_remote_user(m_variables.parsed().at("fake_env").at("remote_user")),
|
||||
m_request_method(m_variables.parsed().at("fake_env").at("request_method")),
|
||||
m_script_name(m_variables.parsed().at("fake_env").at("script_name")),
|
||||
m_server_name(m_variables.parsed().at("fake_env").at("server_name")),
|
||||
m_server_software(m_variables.parsed().at("fake_env").at("server_software")),
|
||||
m_content_length(dhandy::lexical_cast<std::size_t>(m_variables.parsed().at("fake_env").at("content_length"))),
|
||||
m_server_port(dhandy::lexical_cast<uint16_t>(m_variables.parsed().at("fake_env").at("server_port")))
|
||||
{
|
||||
}
|
||||
|
||||
const std::string& FakeEnv::auth_type() const {
|
||||
return m_auth_type;
|
||||
}
|
||||
std::size_t FakeEnv::content_length() const {
|
||||
std::size_t m_content_length;
|
||||
}
|
||||
const std::string& FakeEnv::content_type() const {
|
||||
return m_content_type;
|
||||
}
|
||||
auto FakeEnv::gateway_interface() const -> boost::optional<VersionInfo> {
|
||||
}
|
||||
const std::string& FakeEnv::path_info() const {
|
||||
return m_path_info;
|
||||
}
|
||||
const std::string& FakeEnv::path_translated() const {
|
||||
return m_path_translated;
|
||||
}
|
||||
const std::string& FakeEnv::query_string() const {
|
||||
return m_query_string;
|
||||
}
|
||||
const std::string& FakeEnv::remote_addr() const {
|
||||
return m_remote_addr;
|
||||
}
|
||||
const std::string& FakeEnv::remote_host() const {
|
||||
return m_remote_host;
|
||||
}
|
||||
const std::string& FakeEnv::remote_ident() const {
|
||||
return m_remote_ident;
|
||||
}
|
||||
const std::string& FakeEnv::remote_user() const {
|
||||
return m_remote_user;
|
||||
}
|
||||
const std::string& FakeEnv::request_method() const {
|
||||
return m_request_method;
|
||||
}
|
||||
const std::string& FakeEnv::script_name() const {
|
||||
return m_script_name;
|
||||
}
|
||||
const std::string& FakeEnv::server_name() const {
|
||||
return m_server_name;
|
||||
}
|
||||
uint16_t FakeEnv::server_port() const {
|
||||
uint16_t m_server_port;
|
||||
}
|
||||
auto FakeEnv::server_protocol() const -> boost::optional<VersionInfo> {
|
||||
}
|
||||
const std::string& FakeEnv::server_software() const {
|
||||
return m_server_software;
|
||||
}
|
||||
auto FakeEnv::query_string_split() const -> GetMapType {
|
||||
}
|
||||
} //namespace cgi
|
||||
} //namespace tawashi
|
69
test/unit/fake_cgi_env.hpp
Normal file
69
test/unit/fake_cgi_env.hpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
/* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cgi_env.hpp"
|
||||
#include "ini_file.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace tawashi {
|
||||
namespace cgi {
|
||||
class FakeEnv : public Env {
|
||||
public:
|
||||
explicit FakeEnv (std::string&& parVariablesIni);
|
||||
|
||||
virtual const std::string& auth_type() const override;
|
||||
virtual std::size_t content_length() const override;
|
||||
virtual const std::string& content_type() const override;
|
||||
virtual boost::optional<VersionInfo> gateway_interface() const override a_pure;
|
||||
virtual const std::string& path_info() const override;
|
||||
virtual const std::string& path_translated() const override;
|
||||
virtual const std::string& query_string() const override;
|
||||
virtual const std::string& remote_addr() const override;
|
||||
virtual const std::string& remote_host() const override;
|
||||
virtual const std::string& remote_ident() const override;
|
||||
virtual const std::string& remote_user() const override;
|
||||
virtual const std::string& request_method() const override;
|
||||
virtual const std::string& script_name() const override;
|
||||
virtual const std::string& server_name() const override;
|
||||
virtual uint16_t server_port() const override a_pure;
|
||||
virtual boost::optional<VersionInfo> server_protocol() const override a_pure;
|
||||
virtual const std::string& server_software() const override;
|
||||
|
||||
virtual GetMapType query_string_split() const override a_pure;
|
||||
|
||||
private:
|
||||
IniFile m_variables;
|
||||
std::string m_auth_type;
|
||||
std::string m_content_type;
|
||||
std::string m_path_info;
|
||||
std::string m_path_translated;
|
||||
std::string m_query_string;
|
||||
std::string m_remote_addr;
|
||||
std::string m_remote_host;
|
||||
std::string m_remote_ident;
|
||||
std::string m_remote_user;
|
||||
std::string m_request_method;
|
||||
std::string m_script_name;
|
||||
std::string m_server_name;
|
||||
std::string m_server_software;
|
||||
std::size_t m_content_length;
|
||||
uint16_t m_server_port;
|
||||
};
|
||||
} //namespace cgi
|
||||
} //namespace tawashi
|
114
test/unit/test_index_response.cpp
Normal file
114
test/unit/test_index_response.cpp
Normal file
|
@ -0,0 +1,114 @@
|
|||
/* 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"
|
||||
#include "fake_cgi_env.hpp"
|
||||
#include "index_response.hpp"
|
||||
#include "ini_file.hpp"
|
||||
#include "settings_bag.hpp"
|
||||
#include "safe_stack_object.hpp"
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
||||
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
|
||||
) :
|
||||
IndexResponse(parSettings, parStreamOut, parCgiEnv)
|
||||
{
|
||||
}
|
||||
|
||||
virtual std::string on_mustache_retrieve() override {
|
||||
return g_mustache_html;
|
||||
}
|
||||
};
|
||||
} //namespace tawashi
|
||||
|
||||
TEST_CASE ("Index response", "[index][response]") {
|
||||
using curry::SafeStackObject;
|
||||
|
||||
std::string env_ini(
|
||||
"[fake_env]\n"
|
||||
" auth_type = \n"
|
||||
" content_type = \n"
|
||||
" path_info = \n"
|
||||
" path_translated = \n"
|
||||
" query_string = index.cgi\n"
|
||||
" remote_addr = \n"
|
||||
" remote_host = \n"
|
||||
" remote_ident = \n"
|
||||
" remote_user = \n"
|
||||
" request_method = GET\n"
|
||||
" script_name = \n"
|
||||
" server_name = test_server\n"
|
||||
" server_software = \n"
|
||||
" content_length = 0\n"
|
||||
" server_port = 80\n"
|
||||
);
|
||||
SafeStackObject<tawashi::cgi::FakeEnv> fake_env(std::move(env_ini));
|
||||
|
||||
std::string tawashi_settings(
|
||||
"[tawashi]\n"
|
||||
" base_uri = http://127.0.0.1\n"
|
||||
" website_root = /home/michele/dev/code/cpp/tawashi/html\n"
|
||||
" logging_level = debug\n"
|
||||
" langmap_dir = /usr/share/source-highlight\n"
|
||||
);
|
||||
SafeStackObject<tawashi::IniFile> ini(std::move(tawashi_settings));
|
||||
SafeStackObject<tawashi::SettingsBag> settings(ini);
|
||||
|
||||
std::stringstream response_stream;
|
||||
|
||||
tawashi::IndexResponseCustomMustache response(settings, &response_stream, fake_env);
|
||||
response.send();
|
||||
|
||||
response_stream.seekg(0);
|
||||
CHECK(response_stream.str() == g_expected_response);
|
||||
}
|
Loading…
Add table
Reference in a new issue