diff --git a/src/tawashi/CMakeLists.txt b/src/tawashi/CMakeLists.txt index 288db4f..2816030 100644 --- a/src/tawashi/CMakeLists.txt +++ b/src/tawashi/CMakeLists.txt @@ -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 diff --git a/src/tawashiConfig.h.in b/src/tawashiConfig.h.in index 43f9e5a..cb2557d 100644 --- a/src/tawashiConfig.h.in +++ b/src/tawashiConfig.h.in @@ -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 diff --git a/src/tawashi_implem/cgi_env.hpp b/src/tawashi_implem/cgi_env.hpp index 10f441d..171f9dc 100644 --- a/src/tawashi_implem/cgi_env.hpp +++ b/src/tawashi_implem/cgi_env.hpp @@ -21,6 +21,7 @@ #include "duckhandy/compatibility.h" #include "escapist.hpp" #include "kakoune/safe_ptr.hh" +#include "tawashiConfig.h" #include #include #include @@ -42,27 +43,27 @@ namespace tawashi { typedef boost::container::flat_map 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 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 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 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 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; diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index e5bba26..3a9768c 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -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( diff --git a/test/unit/fake_cgi_env.cpp b/test/unit/fake_cgi_env.cpp new file mode 100644 index 0000000..a6aa695 --- /dev/null +++ b/test/unit/fake_cgi_env.cpp @@ -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 . + */ + +#include "fake_cgi_env.hpp" +#include "duckhandy/lexical_cast.hpp" +#include + +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(m_variables.parsed().at("fake_env").at("content_length"))), + m_server_port(dhandy::lexical_cast(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 { + } + 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 { + } + const std::string& FakeEnv::server_software() const { + return m_server_software; + } + auto FakeEnv::query_string_split() const -> GetMapType { + } + } //namespace cgi +} //namespace tawashi diff --git a/test/unit/fake_cgi_env.hpp b/test/unit/fake_cgi_env.hpp new file mode 100644 index 0000000..9c17858 --- /dev/null +++ b/test/unit/fake_cgi_env.hpp @@ -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 . + */ + +#pragma once + +#include "cgi_env.hpp" +#include "ini_file.hpp" +#include + +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 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 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 diff --git a/test/unit/test_index_response.cpp b/test/unit/test_index_response.cpp new file mode 100644 index 0000000..1e4bc95 --- /dev/null +++ b/test/unit/test_index_response.cpp @@ -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 . + */ + +#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 +#include +#include + +namespace { + const char g_mustache_html[] = +R"( +
+ +
+ +
+ +
+ +)"; + + const char g_expected_response[] = +R"(Content-type: text/html + + +
+ +
+ +
+ +
+ +)"; +} //unnamed namespace + +namespace tawashi { + class IndexResponseCustomMustache : public IndexResponse { + public: + IndexResponseCustomMustache ( + const Kakoune::SafePtr& parSettings, + std::ostream* parStreamOut, + const Kakoune::SafePtr& 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 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 ini(std::move(tawashi_settings)); + SafeStackObject 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); +}