diff --git a/CMakeLists.txt b/CMakeLists.txt index 2dc02acde..1febc176b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,11 +23,7 @@ else() set(Boost_USE_STATIC_LIBS ON) endif() set(Boost_USE_MULTITHREADED ON) -if(CPP-NETLIB_BUILD_TESTS) - set(Boost_COMPONENTS unit_test_framework system regex date_time filesystem program_options ) -else() - set(Boost_COMPONENTS system regex date_time filesystem program_options ) -endif() +set(Boost_COMPONENTS system regex date_time filesystem program_options ) find_package( Boost 1.51 REQUIRED ${Boost_COMPONENTS} ) find_package( OpenSSL ) find_package( Threads ) @@ -47,7 +43,7 @@ if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) INCLUDE(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG(-std=c++11 HAVE_STD11) if (HAVE_STD11) - set(CMAKE_CXX_FLAGS -std=c++11) + set(CMAKE_CXX_FLAGS -std=c++11 -Wall) else() message(FATAL_ERROR "No advanced standard C++ support (-std=c++11 not defined).") endif() @@ -55,7 +51,7 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES Clang) INCLUDE(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG(-std=c++11 HAVE_STD11) if (HAVE_STD11) - set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++") + set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall") set(CMAKE_CXX_LINK_FLAGS "-std=c++11 -stdlib=libc++") else() message(FATAL_ERROR "No C++11 support for Clang version. Please upgrade Clang to a version supporting C++11.") diff --git a/concurrency/test/CMakeLists.txt b/concurrency/test/CMakeLists.txt index 73cf52cad..91576940a 100644 --- a/concurrency/test/CMakeLists.txt +++ b/concurrency/test/CMakeLists.txt @@ -3,15 +3,9 @@ # (See accompanying file LICENSE_1_0.txt or copy at # http://www.boost.org/LICENSE_1_0.txt) -include_directories(${CPP-NETLIB_SOURCE_DIR}/concurrency/src) - -if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) - set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11") -elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang) - CHECK_CXX_COMPILER_FLAG(-std=c++11 HAVE_STD11) - set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++") - set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++") -endif() +include_directories(${CPP-NETLIB_SOURCE_DIR}/concurrency/src + ${GTEST_INCLUDE_DIRS} +) set_source_files_properties(thread_pool_test.cpp PROPERTIES COMPILE_FLAGS "-Wall") @@ -19,6 +13,7 @@ add_executable(cpp-netlib-thread_pool_test thread_pool_test.cpp) target_link_libraries(cpp-netlib-thread_pool_test cppnetlib-concurrency ${Boost_LIBRARIES} + ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) set_target_properties(cpp-netlib-thread_pool_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests) diff --git a/concurrency/test/thread_pool_test.cpp b/concurrency/test/thread_pool_test.cpp index 008370a64..e26706555 100644 --- a/concurrency/test/thread_pool_test.cpp +++ b/concurrency/test/thread_pool_test.cpp @@ -1,17 +1,12 @@ -// Copyright 2010 Dean Michael Berris. +// Copyright 2010, 2012 Dean Michael Berris // Copyright 2012 Google, Inc. // Copyright (c) Glyn Matthews 2012. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE thread pool test -#include -#include +#include #include #include @@ -25,9 +20,9 @@ using network::concurrency::thread_pool; // syntactically. // -BOOST_AUTO_TEST_CASE( default_constructor ) { +TEST(concurrency_test, default_constructor) { thread_pool pool; - BOOST_CHECK_EQUAL(pool.thread_count(), std::size_t(1)); + ASSERT_EQ(pool.thread_count(), std::size_t(1)); } struct foo { @@ -42,13 +37,13 @@ struct foo { int val_; }; -BOOST_AUTO_TEST_CASE( post_work ) { +TEST(concurrency_test, post_work) { foo instance; { thread_pool pool; - BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 1))); - BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 2))); + ASSERT_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 1))); + ASSERT_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 2))); // require that pool is destroyed here, RAII baby } - BOOST_CHECK_EQUAL(instance.val(), 3); + ASSERT_EQ(instance.val(), 3); } diff --git a/http/src/network/protocol/http/request/request_base.ipp b/http/src/network/protocol/http/request/request_base.ipp index c9c67604e..82b27951c 100644 --- a/http/src/network/protocol/http/request/request_base.ipp +++ b/http/src/network/protocol/http/request/request_base.ipp @@ -8,6 +8,7 @@ #define NETWORK_RPTOCOL_HTTP_REQUEST_BASE_IPP_20111102 #include +#include #include #include diff --git a/http/test/CMakeLists.txt b/http/test/CMakeLists.txt index c0fa2920b..5573b2004 100644 --- a/http/test/CMakeLists.txt +++ b/http/test/CMakeLists.txt @@ -8,11 +8,12 @@ include_directories( ${CPP-NETLIB_SOURCE_DIR}/message/src ${CPP-NETLIB_SOURCE_DIR}/logging/src ${CPP-NETLIB_SOURCE_DIR}/http/src + ${GTEST_INCLUDE_DIRS} ${CPP-NETLIB_SOURCE_DIR}) if (OPENSSL_FOUND) include_directories( ${OPENSSL_INCLUDE_DIR} ) - add_definitions(-DBOOST_NETWORK_ENABLE_HTTPS) + add_definitions(-DNETWORK_ENABLE_HTTPS) endif() if( NOT CPP-NETLIB_DISABLE_LOGGING ) @@ -23,13 +24,13 @@ endif() # if not then it will be empty set( CPP-NETLIB_LOGGING_LIB cppnetlib-logging ) -if (Boost_FOUND) +if (CPP-NETLIB_BUILD_TESTS) # These are the internal (simple) tests. - set ( MESSAGE_TESTS + set (MESSAGE_TESTS request_base_test request_test - request_linearize_test response_test + response_incremental_parser_test ) foreach ( test ${MESSAGE_TESTS} ) if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) @@ -47,6 +48,7 @@ if (Boost_FOUND) ) target_link_libraries(cpp-netlib-http-${test} ${Boost_LIBRARIES} + ${GTEST_BOTH_LIBRARIES} ${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} cppnetlib-message @@ -62,69 +64,62 @@ if (Boost_FOUND) ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test}) endforeach(test) - set ( TESTS - client_constructor_test - client_get_test - client_get_different_port_test - client_get_timeout_test - client_get_streaming_test - ) - foreach ( test ${TESTS} ) - if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) - set_source_files_properties(${test}.cpp - PROPERTIES COMPILE_FLAGS "-Wall") - endif() - add_executable(cpp-netlib-http-${test} ${test}.cpp) - target_link_libraries(cpp-netlib-http-${test} - ${Boost_LIBRARIES} - ${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} - cppnetlib-constants - cppnetlib-uri - cppnetlib-message - cppnetlib-message-wrappers - cppnetlib-message-directives - ${CPP-NETLIB_LOGGING_LIB} - cppnetlib-http-message - cppnetlib-http-message-wrappers - cppnetlib-http-client - cppnetlib-http-client-connections) - if (OPENSSL_FOUND) - target_link_libraries(cpp-netlib-http-${test} ${OPENSSL_LIBRARIES}) - endif() - set_target_properties(cpp-netlib-http-${test} - PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests) - add_test(cpp-netlib-http-${test} - ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test}) - endforeach (test) + if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) + set_source_files_properties(client_test.cpp + PROPERTIES COMPILE_FLAGS "-Wall") + endif() + add_executable(cpp-netlib-http-client_test client_test.cpp) + target_link_libraries(cpp-netlib-http-client_test + ${Boost_LIBRARIES} + ${GTEST_BOTH_LIBRARIES} + ${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + cppnetlib-constants + cppnetlib-uri + cppnetlib-message + cppnetlib-message-wrappers + cppnetlib-message-directives + ${CPP-NETLIB_LOGGING_LIB} + cppnetlib-http-message + cppnetlib-http-message-wrappers + cppnetlib-http-client + cppnetlib-http-client-connections) + if (OPENSSL_FOUND) + target_link_libraries(cpp-netlib-http-client_test ${OPENSSL_LIBRARIES}) + endif() + set_target_properties(cpp-netlib-http-client_test + PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests) + add_test(cpp-netlib-http-client_test + ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test}) - #set ( SERVER_API_TESTS - # server_constructor_test - # server_async_run_stop_concurrency - # ) - #foreach ( test ${SERVER_API_TESTS} ) - # if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) - # set_source_files_properties(${test}.cpp - # PROPERTIES COMPILE_FLAGS "-Wall") - # endif() - # add_executable(cpp-netlib-http-${test} ${test}.cpp) - # target_link_libraries(cpp-netlib-http-${test} - # ${Boost_LIBRARIES} - # ${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES} - # ${CMAKE_THREAD_LIBS_INIT} - # cppnetlib-constants - # cppnetlib-uri - # cppnetlib-message - # cppnetlib-message-wrappers - # cppnetlib-http-message - # cppnetlib-http-server - # cppnetlib-http-server-parsers - # cppnetlib-utils-thread_pool - # ) - # set_target_properties(cpp-netlib-http-${test} - # PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests) - # add_test(cpp-netlib-http-${test} - # ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test}) - #endforeach (test) +# set ( SERVER_API_TESTS +# server_constructor_test +# server_async_run_stop_concurrency +# ) +# foreach ( test ${SERVER_API_TESTS} ) +# if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) +# set_source_files_properties(${test}.cpp +# PROPERTIES COMPILE_FLAGS "-Wall") +# endif() +# add_executable(cpp-netlib-http-${test} ${test}.cpp) +# target_link_libraries(cpp-netlib-http-${test} +# ${Boost_LIBRARIES} +# ${GTEST_BOTH_LIBRARIES} +# ${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES} +# ${CMAKE_THREAD_LIBS_INIT} +# cppnetlib-constants +# cppnetlib-uri +# cppnetlib-message +# cppnetlib-message-wrappers +# cppnetlib-http-message +# cppnetlib-http-server +# cppnetlib-http-server-parsers +# cppnetlib-utils-thread_pool +# ) +# set_target_properties(cpp-netlib-http-${test} +# PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests) +# add_test(cpp-netlib-http-${test} +# ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test}) +# endforeach (test) endif() diff --git a/http/test/client_constructor_test.cpp b/http/test/client_constructor_test.cpp deleted file mode 100644 index e1f789cee..000000000 --- a/http/test/client_constructor_test.cpp +++ /dev/null @@ -1,32 +0,0 @@ - -// Copyright 2010 Dean Michael Berris. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP 1.0 Client Constructor Test -#include -#include - -namespace http = network::http; - -BOOST_AUTO_TEST_CASE(http_client_constructor_test) { - // Here's the simplest way to construct a client. - http::client instance; - - // The next way we're supporting is actually to construct an options object - // that allows you to set options. This class replaces the Boost.Parameter - // based approach to a much simpler model that scales better. - http::client_options options; - boost::asio::io_service io_service; - options.io_service(&io_service) - .follow_redirects() - .cache_resolved() - .add_openssl_certificate_path("/dev/zero") - .add_openssl_verify_path("/dev/null"); - http::client instance2(options); -} diff --git a/http/test/client_get_different_port_test.cpp b/http/test/client_get_different_port_test.cpp deleted file mode 100644 index 3d929e356..000000000 --- a/http/test/client_get_different_port_test.cpp +++ /dev/null @@ -1,26 +0,0 @@ - -// Copyright 2010 Dean Michael Berris. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP Client Get Different Port Test -#include -#include - -namespace http = network::http; -namespace net = network; - -BOOST_AUTO_TEST_CASE(http_get_test_different_port) { - http::request request_("http://www.boost.org:80/"); - http::client client_; - http::response response_ = client_.get(request_); - net::headers_wrapper::container_type const &headers_ = headers(response_); - BOOST_CHECK( !headers_.empty() ); - BOOST_CHECK( body(response_).size() > 0 ); -} - diff --git a/http/test/client_get_streaming_test.cpp b/http/test/client_get_streaming_test.cpp deleted file mode 100644 index 92c2a0668..000000000 --- a/http/test/client_get_streaming_test.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2011 Dean Michael Berris <dberris@google.com>. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP 1.1 Get Streaming Test -#include -#include -#include - -namespace net = network; -namespace http = network::http; - -struct body_handler { - - explicit body_handler(std::string & body) - : body(body) {} - - NETWORK_HTTP_BODY_CALLBACK(operator(), range, error) { - body.append(boost::begin(range), boost::end(range)); - } - - std::string & body; - -}; - - -BOOST_AUTO_TEST_CASE(http_client_get_streaming_test) { - http::client::request request("http://www.boost.org"); - http::client::response response; - std::string body_string; - std::string dummy_body; - body_handler handler_instance(body_string); - { - http::client client_; - BOOST_CHECK_NO_THROW( response = client_.get(request, handler_instance) ); - net::headers_wrapper::container_type const & headers_ = headers(response); - BOOST_CHECK ( !boost::empty(headers_) ); - BOOST_CHECK_EQUAL ( body(response).size(), 0u ); - std::string version_, status_message_; - boost::uint16_t status_; - version_ = version(response); - status_ = status(response); - status_message_ = status_message(response); - BOOST_CHECK_EQUAL ( version_.substr(0, 7), std::string("HTTP/1.") ); - BOOST_CHECK_EQUAL ( status_, 200u ); - BOOST_CHECK_EQUAL ( status_message_, std::string("OK") ); - dummy_body = body(response); - } - BOOST_CHECK ( dummy_body == std::string() ); -} - diff --git a/http/test/client_get_test.cpp b/http/test/client_get_test.cpp deleted file mode 100644 index 6bd1dac36..000000000 --- a/http/test/client_get_test.cpp +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2010 Dean Michael Berris. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP 1.0 Get Test -#include -#include - -namespace net = network; -namespace http = network::http; - -BOOST_AUTO_TEST_CASE(http_client_get_test) { - http::client::request request("http://www.google.com/"); - request << net::header("Connection", "close"); - http::client client_; - http::client::response response; - BOOST_REQUIRE_NO_THROW ( response = client_.get(request) ); - std::multimap headers_ = net::headers(response); - BOOST_CHECK ( !boost::empty(headers_) ); - BOOST_REQUIRE_NO_THROW ( BOOST_CHECK ( body(response).size() != 0 ) ); - std::string version_, status_message_; - response.get_version(version_); - uint16_t status_; - response.get_status(status_); - response.get_status_message(status_message_); - BOOST_CHECK_EQUAL ( version_.substr(0,7), "HTTP/1."); - BOOST_CHECK ( status_ == 302u || status_ == 200u ); - BOOST_CHECK ( status_message_ == std::string("Found") || status_message_ == std::string("OK") ); -} - -#ifdef NETWORK_ENABLE_HTTPS - -BOOST_AUTO_TEST_CASE(https_client_get_test) { - http::client::request request("https://www.google.com"); - request << net::header("Connection", "close"); - http::client client_; - http::client::response response; - BOOST_REQUIRE_NO_THROW ( response = client_.get(request) ); - std::multimap headers_ = net::headers(response); - BOOST_CHECK ( !boost::empty(headers_) ); - BOOST_REQUIRE_NO_THROW ( BOOST_CHECK ( body(response).size() != 0 ) ); - std::string version_, status_message_; - response.get_version(version_); - uint16_t status_; - response.get_status(status_); - response.get_status_message(status_message_); - BOOST_CHECK_EQUAL ( version_.substr(0,7), "HTTP/1."); - BOOST_CHECK ( status_ == 302u || status_ == 200u ); - BOOST_CHECK ( status_message_ == std::string("Found") || status_message_ == std::string("OK") ); -} - -#endif diff --git a/http/test/client_get_timeout_test.cpp b/http/test/client_get_timeout_test.cpp deleted file mode 100644 index d8cfb768d..000000000 --- a/http/test/client_get_timeout_test.cpp +++ /dev/null @@ -1,26 +0,0 @@ - -// Copyright 2010 Dean Michael Berris. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP Client Get Timeout Test -#include -#include - -namespace http = network::http; - -BOOST_AUTO_TEST_CASE(http_get_test_timeout_1_0) { - http::client::request request("http://localhost:12121/"); - http::client::response response_; - http::client client_; - boost::uint16_t port_ = port(request); - std::string temp; - BOOST_CHECK_EQUAL ( 12121, port_ ); - BOOST_CHECK_THROW ( response_ = client_.get(request); temp = body(response_); , std::exception ); -} - diff --git a/http/test/client_include_inlined.cpp b/http/test/client_include_inlined.cpp deleted file mode 100644 index f7f1d4def..000000000 --- a/http/test/client_include_inlined.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2011 Dean Michael Berris <dberris@google.com>. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#define NETWORK_NO_LIB -#include - -int main(int argc, char * argv[]) { - using namespace boost; - using namespace boost::network; - http::client c; - http::client::request req("http://www.boost.org/"); - try { - http::client::response res = c.get(req); - } catch (...) { - // ignore the error, we just want to make sure - // the interface works inlined. - } - return 0; -} - diff --git a/http/test/client_localhost_normal_test.cpp b/http/test/client_localhost_normal_test.cpp deleted file mode 100644 index d332b430a..000000000 --- a/http/test/client_localhost_normal_test.cpp +++ /dev/null @@ -1,355 +0,0 @@ -// -// Copyright Divye Kapoor 2008. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// Changes by Kim Grasman 2008 -// Changes by Dean Michael Berris 2008, 2010 - -#define BOOST_TEST_MODULE http 1.0 localhost tests - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "http_test_server.hpp" - -using std::cout; -using std::endl; - -namespace { - const std::string base_url = "http://localhost:8000"; - const std::string cgi_url = base_url + "/cgi-bin/requestinfo.py"; - - struct running_server_fixture - { - // NOTE: Can't use BOOST_REQUIRE_MESSAGE here, as Boost.Test data structures - // are not fully set up when the global fixture runs. - running_server_fixture() { - if( !server.start() ) - cout << "Failed to start HTTP server for test!" << endl; - } - - ~running_server_fixture() { - if( !server.stop() ) - cout << "Failed to stop HTTP server for test!" << endl; - } - - http_test_server server; - }; - - std::size_t readfile(std::ifstream& file, std::vector& buffer) { - using std::ios; - - std::istreambuf_iterator src(file); - std::istreambuf_iterator eof; - std::copy(src, eof, std::back_inserter(buffer)); - - return buffer.size(); - } - - std::map parse_headers(std::string const& body) { - std::map headers; - - std::istringstream stream(body); - while (stream.good()) - { - std::string line; - std::getline(stream, line); - if (!stream.eof()) - { - std::size_t colon = line.find(':'); - if (colon != std::string::npos) - { - std::string header = line.substr(0, colon); - std::string value = line.substr(colon + 2); - headers[header] = value; - } - } - } - - return headers; - } - - std::string get_content_length(std::string const& content) { - return boost::lexical_cast(content.length()); - } - -} - -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - // Uncomment the below if you're running Python pre-2.6. There was a bug - // in the Python HTTP server for earlier versions that causes this test - // case to fail. - //BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(text_query_preserves_crlf, 2); -#endif - -BOOST_GLOBAL_FIXTURE( running_server_fixture ); - -BOOST_AUTO_TEST_CASE(body_test) { - // Tests presence of body in http responses - using namespace boost::network; - http::client::request request_(base_url); - http::client client_; - http::client::response response_; - BOOST_REQUIRE_NO_THROW( response_ = client_.get(request_) ); - BOOST_CHECK(body(response_).size() != 0); -} - -BOOST_AUTO_TEST_CASE(text_content_type_test) { - // Tests correct parsing of the content-type header sent by the server - using namespace boost::network; - http::client::request request_(base_url); - http::client client_; - http::client::response response_; - BOOST_REQUIRE_NO_THROW( response_ = client_.get(request_) ); - BOOST_REQUIRE(headers(response_).count("Content-type") != 0); - headers_range::type range = headers(response_)["Content-type"]; - BOOST_CHECK(boost::begin(range)->first == "Content-type"); - BOOST_CHECK(boost::begin(range)->second == "text/html"); -} - -BOOST_AUTO_TEST_CASE(binary_content_type_test) { - // Tests correct parsing of content-type for binary files such as .zip files - using namespace boost::network; - http::client::request request_(base_url + "/boost.jpg"); - http::client client_; - http::client::response response_; - BOOST_REQUIRE_NO_THROW( response_ = client_.get(request_) ); - BOOST_REQUIRE(headers(response_).count("Content-type") != 0); - headers_range::type range = headers(response_)["Content-type"]; - BOOST_CHECK(boost::begin(range)->first == "Content-type"); - BOOST_CHECK(boost::begin(range)->second == "image/jpeg"); -} - -BOOST_AUTO_TEST_CASE(content_length_header_test) { - // Uses the test.xml file to ensure that the file was received at the correct length for a text encoding - using namespace boost::network; - http::client::request request_(base_url + "/test.xml"); - http::client client_; - http::client::response response_; - BOOST_REQUIRE_NO_THROW( response_ = client_.get(request_) ); - BOOST_REQUIRE(headers(response_).count("Content-Length") != 0); - headers_range::type range = headers(response_)["Content-Length"]; - BOOST_CHECK_EQUAL(boost::begin(range)->first, "Content-Length"); - BOOST_CHECK_EQUAL(boost::begin(range)->second, "113"); - BOOST_CHECK(body(response_).size() != 0); -} - -BOOST_AUTO_TEST_CASE(text_query_preserves_crlf) { - // Tests proper transfer of a text file - using namespace boost::network; - http::client::request request_(base_url + "/test.xml"); - http::client client_; - http::client::response response_; - BOOST_REQUIRE_NO_THROW( response_ = client_.get(request_) ); - - http::client::response::string_type body_ = body(response_); - BOOST_CHECK(body(response_).size() != 0); - - using std::ios; - - std::ifstream file("libs/network/test/server/test.xml", ios::in | ios::binary); - if( ! file ) { - file.clear(); - file.open("server/test.xml", ios::in | ios::binary); - } - - BOOST_REQUIRE_MESSAGE( file, "Could not open local test.xml"); - - std::vector memblock; - std::size_t size = readfile(file, memblock); - - BOOST_CHECK(size != 0); - BOOST_CHECK_EQUAL(body_.size(), size); - - if (body(response_).size() == size) { - std::pair::iterator, std::string::const_iterator> diff_pos = std::mismatch(memblock.begin(), memblock.end(), body_.begin()); - BOOST_CHECK_EQUAL(boost::numeric_cast(diff_pos.first - memblock.begin()), size); - } -} - -BOOST_AUTO_TEST_CASE(binary_file_query) { - // Tests proper transfer of a binary image - using namespace boost::network; - http::client::request request_(base_url + "/boost.jpg"); - http::client client_; - http::client::response response_; - BOOST_REQUIRE_NO_THROW(response_ = client_.get(request_)); - - http::client::response::string_type body_ = body(response_); - BOOST_CHECK(body_.size() != 0); - - using std::ios; - - std::ifstream file("libs/network/test/server/boost.jpg", ios::in | ios::binary); - if( ! file ) { - file.clear(); - file.open("server/boost.jpg", ios::in | ios::binary); - } - - BOOST_REQUIRE_MESSAGE( file, "Could not open boost.jpg locally"); - - std::vector memblock; - std::size_t size = readfile(file, memblock); - - BOOST_CHECK(size != 0); - BOOST_CHECK_EQUAL(body_.size(), size); - - std::pair::iterator, std::string::const_iterator> diff_pos = std::mismatch(memblock.begin(), memblock.end(), body_.begin()); - BOOST_CHECK_EQUAL(boost::numeric_cast(diff_pos.first - memblock.begin()), size); -} - -BOOST_AUTO_TEST_CASE(cgi_query) { - // Get a dynamic request with no Content-Length header - // Ensure that we have a body - using namespace boost::network; - - http::client::request req(cgi_url + "?query=1"); - http::client c; - http::client::response r; - BOOST_REQUIRE_NO_THROW(r = c.get(req)); - BOOST_CHECK(body(r).size() != 0); - BOOST_CHECK(boost::empty(headers(r)["Content-Length"])); -} - -BOOST_AUTO_TEST_CASE(cgi_multi_line_headers) { - using namespace boost::network; - - http::client::request req(base_url + "/cgi-bin/multiline-header.py?query=1"); - http::client c; - http::client::response r; - BOOST_REQUIRE_NO_THROW(r = c.get(req)); - BOOST_CHECK(body(r).size() != 0); - BOOST_CHECK(boost::empty(headers(r)["Content-Type"])); - headers_range::type range=headers(r)["X-CppNetlib-Test"]; - BOOST_REQUIRE(boost::begin(range) != boost::end(range)); - BOOST_REQUIRE(distance(range) == 2); - BOOST_CHECK_EQUAL(boost::begin(range)->second, std::string("multi-line-header")); - BOOST_CHECK_EQUAL((++boost::begin(range))->second, std::string("that-should-concatenate")); -} - -BOOST_AUTO_TEST_CASE(file_not_found) { - // Request for a non existing file. - // Ensure that we have a body even in the presence of an error response - using namespace boost::network; - - http::client::request req(base_url + "/file_not_found"); - http::client c; - http::client::response r = c.get(req); - - BOOST_CHECK(body(r).size() != 0); -} - -BOOST_AUTO_TEST_CASE(head_test) { - using namespace boost::network; - http::client::request request_(base_url + "/test.xml"); - http::client client_; - http::client::response response_; - BOOST_REQUIRE_NO_THROW( response_ = client_.head(request_) ); - BOOST_REQUIRE(headers(response_).count("Content-Length") != 0); - headers_range::type range = headers(response_)["Content-Length"]; - BOOST_CHECK_EQUAL(boost::begin(range)->first, "Content-Length"); - BOOST_CHECK_EQUAL(boost::begin(range)->second, "113"); - BOOST_CHECK(body(response_).size() == 0); -} - -BOOST_AUTO_TEST_CASE(post_with_explicit_headers) { - // This test checks that the headers echoed through echo_headers.py - // are in fact the same as what are sent through the POST request - using namespace boost::network; - - const std::string postdata = "empty"; - const std::string content_length = get_content_length(postdata); - const std::string content_type = "application/x-www-form-urlencoded"; - - http::client::request req(base_url + "/cgi-bin/echo_headers.py"); - req << header("Content-Length", content_length); - req << header("Content-Type", content_type); - req << body(postdata); - - http::client c; - http::client::response r; - BOOST_REQUIRE_NO_THROW(r = c.post(req)); - - std::map headers = parse_headers(body(r)); - BOOST_CHECK_EQUAL(headers["content-length"], content_length); - BOOST_CHECK_EQUAL(headers["content-type"], content_type); -} - -BOOST_AUTO_TEST_CASE(post_with_implicit_headers) { - // This test checks that post(request, body) derives Content-Length - // and Content-Type - using namespace boost::network; - - const std::string postdata = "empty"; - - http::client::request req(base_url + "/cgi-bin/echo_headers.py"); - - http::client c; - http::client::response r; - BOOST_REQUIRE_NO_THROW(r = c.post(req, postdata)); - - std::map headers = parse_headers(body(r)); - BOOST_CHECK_EQUAL(headers["content-length"], get_content_length(postdata)); - BOOST_CHECK_EQUAL(headers["content-type"], "x-application/octet-stream"); -} - -BOOST_AUTO_TEST_CASE(post_with_explicit_content_type) { - // This test checks that post(request, content_type, body) derives Content-Length, - // and keeps Content-Type - using namespace boost::network; - - const std::string postdata = "empty"; - const std::string content_type = "application/x-my-content-type"; - - http::client::request req(base_url + "/cgi-bin/echo_headers.py"); - - http::client c; - http::client::response r; - BOOST_REQUIRE_NO_THROW(r = c.post(req, content_type, postdata)); - - std::map headers = parse_headers(body(r)); - BOOST_CHECK_EQUAL(headers["content-length"], get_content_length(postdata)); - BOOST_CHECK_EQUAL(headers["content-type"], content_type); -} - -BOOST_AUTO_TEST_CASE(post_body_default_content_type) { - // This test checks that post(request, body) gets the post data - // through to the server - using namespace boost::network; - - const std::string postdata = "firstname=bill&lastname=badger"; - - http::client::request req(base_url + "/cgi-bin/echo_body.py"); - - http::client c; - http::client::response r; - BOOST_REQUIRE_NO_THROW(r = c.post(req, postdata)); - http::client::response::string_type body_ = body(r); - BOOST_CHECK_EQUAL(postdata, body_); -} - -BOOST_AUTO_TEST_CASE(post_with_custom_headers) { - // This test checks that custom headers pass through to the server - // when posting - using namespace boost::network; - - http::client::request req(base_url + "/cgi-bin/echo_headers.py"); - req << header("X-Cpp-Netlib", "rocks!"); - - http::client c; - http::client::response r; - BOOST_REQUIRE_NO_THROW(r = c.post(req, std::string())); - - std::map headers = parse_headers(body(r)); - BOOST_CHECK_EQUAL(headers["x-cpp-netlib"], "rocks!"); -} diff --git a/http/test/client_localhost_ssl_test.cpp b/http/test/client_localhost_ssl_test.cpp deleted file mode 100644 index e1e1f173f..000000000 --- a/http/test/client_localhost_ssl_test.cpp +++ /dev/null @@ -1,322 +0,0 @@ -// -// Copyright Divye Kapoor 2008. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Changes by Kim Grasman 2008 -// Changes by Dean Michael Berris 2008, 2009 - -#define BOOST_TEST_MODULE https 1.0 localhost tests - -#include -#include -#include -#include -#include -#include -#include -#include - -#define HTTPS_SERVER_TEST -#include "http_test_server.hpp" - -using std::cout; -using std::endl; - -namespace { - const std::string base_url = "https://localhost:8443"; - const std::string cgi_url = base_url + "/cgi-bin/requestinfo.py"; - - struct running_server_fixture - { - // NOTE: Can't use BOOST_REQUIRE_MESSAGE here, as Boost.Test data structures - // are not fully set up when the global fixture runs. - running_server_fixture() { - if( !server.start() ) - cout << "Failed to start HTTP server for test!" << endl; - } - - ~running_server_fixture() { - if( !server.stop() ) - cout << "Failed to stop HTTP server for test!" << endl; - } - - http_test_server server; - }; - - std::size_t readfile(std::ifstream& file, std::vector& buffer) { - using std::ios; - - std::istreambuf_iterator src(file); - std::istreambuf_iterator eof; - std::copy(src, eof, std::back_inserter(buffer)); - - return buffer.size(); - } - -} - -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - // Uncomment the below if you're running Python pre-2.6. There was a bug - // in the Python HTTP server for earlier versions that causes this test - // case to fail. - //BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(text_query_preserves_crlf, 2); -#endif - -BOOST_GLOBAL_FIXTURE( running_server_fixture ); - -BOOST_AUTO_TEST_CASE(body_test) { - // Tests presence of body in http responses - using namespace boost::network; - http::client::request request_(base_url); - http::client client_; - http::client::response response_ = client_.get(request_); - BOOST_CHECK(body(response_).size() != 0); -} - -BOOST_AUTO_TEST_CASE(text_content_type_test) { - // Tests correct parsing of the content-type header sent by the server - using namespace boost::network; - http::client::request request_(base_url); - http::client client_; - http::client::response response_ = client_.get(request_); - BOOST_REQUIRE(headers(response_).count("Content-type") != 0); - headers_range::type range = headers(response_)["Content-type"]; - BOOST_CHECK(boost::begin(range)->first == "Content-type"); - BOOST_CHECK(boost::begin(range)->second == "text/html"); -} - -BOOST_AUTO_TEST_CASE(binary_content_type_test) { - // Tests correct parsing of content-type for binary files such as .zip files - using namespace boost::network; - http::client::request request_(base_url + "/boost.jpg"); - http::client client_; - http::client::response response_ = client_.get(request_); - BOOST_REQUIRE(headers(response_).count("Content-type") != 0); - headers_range::type range = headers(response_)["Content-type"]; - BOOST_CHECK(boost::begin(range)->first == "Content-type"); - BOOST_CHECK(boost::begin(range)->second == "image/jpeg"); -} - -BOOST_AUTO_TEST_CASE(content_length_header_test) { - // Uses the test.xml file to ensure that the file was received at the correct length for a text encoding - using namespace boost::network; - http::client::request request_(base_url + "/test.xml"); - http::client client_; - http::client::response response_ = client_.get(request_); - BOOST_REQUIRE(headers(response_).count("Content-Length") != 0); - headers_range::type range = headers(response_)["Content-Length"]; - BOOST_CHECK_EQUAL(boost::begin(range)->first, "Content-Length"); - BOOST_CHECK_EQUAL(boost::begin(range)->second, "113"); - BOOST_CHECK(body(response_).size() != 0); -} - -BOOST_AUTO_TEST_CASE(text_query_preserves_crlf) { - // Tests proper transfer of a text file - using namespace boost::network; - http::client::request request_(base_url + "/test.xml"); - http::client client_; - http::client::response response_ = client_.get(request_); - - http::client::response::string_type body_ = body(response_); - BOOST_CHECK(body_.size() != 0); - - using std::ios; - - std::ifstream file("libs/network/test/server/test.xml", ios::in | ios::binary); - if( ! file ) { - file.clear(); - file.open("server/test.xml", ios::in | ios::binary); - } - - BOOST_REQUIRE_MESSAGE( file, "Could not open local test.xml"); - - std::vector memblock; - std::size_t size = readfile(file, memblock); - - BOOST_CHECK(size != 0); - BOOST_CHECK_EQUAL(body(response_).size(), size); - - if (body(response_).size() == size) { - std::pair::iterator, std::string::const_iterator> diff_pos = std::mismatch(memblock.begin(), memblock.end(), body_.begin()); - BOOST_CHECK_EQUAL(boost::numeric_cast(diff_pos.first - memblock.begin()), size); - } -} - -BOOST_AUTO_TEST_CASE(binary_file_query) { - // Tests proper transfer of a binary image - using namespace boost::network; - http::client::request request_(base_url + "/boost.jpg"); - http::client client_; - http::client::response response_; - BOOST_CHECK_NO_THROW(response_ = client_.get(request_)); - - http::client::response::string_type body_ = body(response_); - BOOST_CHECK(body_.size() != 0); - - using std::ios; - - std::ifstream file("libs/network/test/server/boost.jpg", ios::in | ios::binary); - if( ! file ) { - file.clear(); - file.open("server/boost.jpg", ios::in | ios::binary); - } - - BOOST_REQUIRE_MESSAGE( file, "Could not open boost.jpg locally"); - - std::vector memblock; - std::size_t size = readfile(file, memblock); - - BOOST_CHECK(size != 0); - BOOST_CHECK_EQUAL(body(response_).size(), size); - - std::pair::iterator, std::string::const_iterator> diff_pos = std::mismatch(memblock.begin(), memblock.end(), body_.begin()); - BOOST_CHECK_EQUAL(boost::numeric_cast(diff_pos.first - memblock.begin()), size); -} - -//BOOST_AUTO_TEST_CASE(cgi_query) { -// // Get a dynamic request with no Content-Length header -// // Ensure that we have a body -// using namespace boost::network; -// -// http::client::request req(cgi_url + "?query=1"); -// http::client c; -// http::client::response r; -// BOOST_REQUIRE_NO_THROW(r = c.get(req)); -// BOOST_CHECK(body(r).size() != 0); -// BOOST_CHECK(headers(r)["Content-Type"].begin() != headers(r)["Content-Type"].end()); -//} -// -//BOOST_AUTO_TEST_CASE(cgi_multi_line_headers) { -// using namespace boost::network; -// -// http::client::request req(base_url + "/cgi-bin/multiline-header.py?query=1"); -// http::client c; -// http::client::response r; -// BOOST_REQUIRE_NO_THROW(r = c.get(req)); -// BOOST_CHECK(body(r).size() != 0); -// BOOST_CHECK(headers(r)["Content-Type"].begin() != headers(r)["Content-Type"].end()); -// headers_range::type range=headers(r)["X-CppNetlib-Test"]; -// BOOST_REQUIRE(boost::begin(range) != boost::end(range)); -// BOOST_REQUIRE(distance(range) == 2); -// BOOST_CHECK_EQUAL(boost::begin(range)->second, std::string("multi-line-header")); -// BOOST_CHECK_EQUAL((++boost::begin(range))->second, std::string("that-should-concatenate")); -//} - -BOOST_AUTO_TEST_CASE(file_not_found) { - // Request for a non existing file. - // Ensure that we have a body even in the presence of an error response - using namespace boost::network; - - http::client::request req(base_url + "/file_not_found"); - http::client c; - http::client::response r = c.get(req); - - BOOST_CHECK(body(r).size() != 0); -} - -BOOST_AUTO_TEST_CASE(head_test) { - using namespace boost::network; - http::client::request request_(base_url + "/test.xml"); - http::client client_; - http::client::response response_ = client_.head(request_); - BOOST_REQUIRE(headers(response_).count("Content-Length") != 0); - headers_range::type range = headers(response_)["Content-Length"]; - BOOST_CHECK_EQUAL(boost::begin(range)->first, "Content-Length"); - BOOST_CHECK_EQUAL(boost::begin(range)->second, "113"); - BOOST_CHECK(body(response_).size() == 0); -} - -//BOOST_AUTO_TEST_CASE(post_with_explicit_headers) { -// // This test checks that the headers echoed through echo_headers.py -// // are in fact the same as what are sent through the POST request -// using namespace boost::network; -// -// const std::string postdata = "empty"; -// const std::string content_length = get_content_length(postdata); -// const std::string content_type = "application/x-www-form-urlencoded"; -// -// http::client::request req(base_url + "/cgi-bin/echo_headers.py"); -// req << header("Content-Length", content_length); -// req << header("Content-Type", content_type); -// req << body(postdata); -// -// http::client c; -// http::client::response r; -// BOOST_REQUIRE_NO_THROW(r = c.post(req)); -// -// std::map headers = parse_headers(body(r)); -// BOOST_CHECK_EQUAL(headers["content-length"], content_length); -// BOOST_CHECK_EQUAL(headers["content-type"], content_type); -//} -// -//BOOST_AUTO_TEST_CASE(post_with_implicit_headers) { -// // This test checks that post(request, body) derives Content-Length -// // and Content-Type -// using namespace boost::network; -// -// const std::string postdata = "empty"; -// -// http::client::request req(base_url + "/cgi-bin/echo_headers.py"); -// -// http::client c; -// http::client::response r; -// BOOST_REQUIRE_NO_THROW(r = c.post(req, postdata)); -// -// std::map headers = parse_headers(body(r)); -// BOOST_CHECK_EQUAL(headers["content-length"], get_content_length(postdata)); -// BOOST_CHECK_EQUAL(headers["content-type"], "x-application/octet-stream"); -//} -// -//BOOST_AUTO_TEST_CASE(post_with_explicit_content_type) { -// // This test checks that post(request, content_type, body) derives Content-Length, -// // and keeps Content-Type -// using namespace boost::network; -// -// const std::string postdata = "empty"; -// const std::string content_type = "application/x-my-content-type"; -// -// http::client::request req(base_url + "/cgi-bin/echo_headers.py"); -// -// http::client c; -// http::client::response r; -// BOOST_REQUIRE_NO_THROW(r = c.post(req, content_type, postdata)); -// -// std::map headers = parse_headers(body(r)); -// BOOST_CHECK_EQUAL(headers["content-length"], get_content_length(postdata)); -// BOOST_CHECK_EQUAL(headers["content-type"], content_type); -//} -// -//BOOST_AUTO_TEST_CASE(post_body_default_content_type) { -// // This test checks that post(request, body) gets the post data -// // through to the server -// using namespace boost::network; -// -// const std::string postdata = "firstname=bill&lastname=badger"; -// -// http::client::request req(base_url + "/cgi-bin/echo_body.py"); -// -// http::client c; -// http::client::response r; -// BOOST_REQUIRE_NO_THROW(r = c.post(req, postdata)); -// -// BOOST_CHECK_EQUAL(postdata, body(r)); -//} -// -//BOOST_AUTO_TEST_CASE(post_with_custom_headers) { -// // This test checks that custom headers pass through to the server -// // when posting -// using namespace boost::network; -// -// http::client::request req(base_url + "/cgi-bin/echo_headers.py"); -// req << header("X-Cpp-Netlib", "rocks!"); -// -// http::client c; -// http::client::response r; -// BOOST_REQUIRE_NO_THROW(r = c.post(req, std::string())); -// -// std::map headers = parse_headers(body(r)); -// BOOST_CHECK_EQUAL(headers["x-cpp-netlib"], "rocks!"); -//} diff --git a/http/test/client_test.cpp b/http/test/client_test.cpp new file mode 100644 index 000000000..1fdd178c4 --- /dev/null +++ b/http/test/client_test.cpp @@ -0,0 +1,125 @@ +// Copyright 2012 Dean Michael Berris +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +namespace http = network::http; +namespace net = network; + +TEST(client_test, constructor) { + // Here's the simplest way to construct a client. + http::client instance; + + // The next way we're supporting is actually to construct an options object + // that allows you to set options. This class replaces the Boost.Parameter + // based approach to a much simpler model that scales better. + http::client_options options; + boost::asio::io_service io_service; + options.io_service(&io_service) + .follow_redirects() + .cache_resolved() + .add_openssl_certificate_path("/dev/zero") + .add_openssl_verify_path("/dev/null"); + http::client instance2(options); +} + +TEST(client_test, get_different_port) { + http::request request_("http://www.boost.org:80/"); + http::client client_; + http::response response_ = client_.get(request_); + net::headers_wrapper::container_type const &headers_ = headers(response_); + ASSERT_TRUE( !headers_.empty() ); + ASSERT_GT( body(response_).size() , 0 ); +} + +struct body_handler { + explicit body_handler(std::string & body) + : body(body) {} + + NETWORK_HTTP_BODY_CALLBACK(operator(), range, error) { + body.append(boost::begin(range), boost::end(range)); + } + + std::string & body; +}; + +TEST(client_test, get_streaming_test) { + http::client::request request("http://www.boost.org"); + http::client::response response; + std::string body_string; + std::string dummy_body; + body_handler handler_instance(body_string); + { + http::client client_; + ASSERT_NO_THROW( response = client_.get(request, handler_instance) ); + net::headers_wrapper::container_type const & headers_ = headers(response); + ASSERT_TRUE ( !boost::empty(headers_) ); + ASSERT_EQ ( body(response).size(), 0u ); + std::string version_, status_message_; + boost::uint16_t status_; + version_ = version(response); + status_ = status(response); + status_message_ = status_message(response); + ASSERT_EQ ( version_.substr(0, 7), std::string("HTTP/1.") ); + ASSERT_EQ ( status_, 200u ); + ASSERT_EQ ( status_message_, std::string("OK") ); + dummy_body = body(response); + } + ASSERT_TRUE ( dummy_body == std::string() ); +} + +TEST(client_test, http_get_test) { + http::client::request request("http://www.google.com/"); + request << net::header("Connection", "close"); + http::client client_; + http::client::response response; + ASSERT_NO_THROW ( response = client_.get(request) ); + std::multimap headers_ = net::headers(response); + ASSERT_TRUE ( !boost::empty(headers_) ); + ASSERT_NO_THROW ( ASSERT_TRUE ( body(response).size() != 0 ) ); + std::string version_, status_message_; + response.get_version(version_); + uint16_t status_; + response.get_status(status_); + response.get_status_message(status_message_); + ASSERT_EQ ( version_.substr(0,7), "HTTP/1."); + ASSERT_TRUE ( status_ == 302u || status_ == 200u ); + ASSERT_TRUE ( status_message_ == std::string("Found") || status_message_ == std::string("OK") ); +} + +#ifdef NETWORK_ENABLE_HTTPS + +TEST(client_test, https_get_test) { + http::client::request request("https://www.google.com"); + request << net::header("Connection", "close"); + http::client client_; + http::client::response response; + ASSERT_NO_THROW ( response = client_.get(request) ); + std::multimap headers_ = net::headers(response); + ASSERT_TRUE ( !boost::empty(headers_) ); + ASSERT_NO_THROW ( ASSERT_TRUE ( body(response).size() != 0 ) ); + std::string version_, status_message_; + response.get_version(version_); + uint16_t status_; + response.get_status(status_); + response.get_status_message(status_message_); + ASSERT_EQ ( version_.substr(0,7), "HTTP/1."); + ASSERT_TRUE ( status_ == 302u || status_ == 200u ); + ASSERT_TRUE ( status_message_ == std::string("Found") || status_message_ == std::string("OK") ); +} + +#endif + +TEST(client_test, http_get_test_timeout_1_0) { + http::client::request request("http://localhost:12121/"); + http::client::response response_; + http::client client_; + boost::uint16_t port_ = port(request); + std::string temp; + ASSERT_EQ ( 12121, port_ ); + ASSERT_THROW ( response_ = client_.get(request); temp = body(response_); , std::exception ); +} + diff --git a/http/test/request_base_test.cpp b/http/test/request_base_test.cpp index a44a39c5f..e931493d4 100644 --- a/http/test/request_base_test.cpp +++ b/http/test/request_base_test.cpp @@ -4,12 +4,8 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP Request Storage Base Test +#include #include -#include namespace http = network::http; @@ -39,7 +35,7 @@ struct request_test : http::request_storage_base { } }; -BOOST_AUTO_TEST_CASE(request_storage_flow) { +TEST(request_test, request_storage_flow) { // Use a few byte chunks just to make it manageable. request_test simple(64); static char data[] = @@ -47,15 +43,15 @@ BOOST_AUTO_TEST_CASE(request_storage_flow) { simple.append(data, sizeof(data)); std::string output; size_t bytes_read = simple.read(output, 0, sizeof(data)); - BOOST_CHECK_EQUAL(bytes_read, sizeof(data)); + ASSERT_EQ(bytes_read, sizeof(data)); std::string flattened; simple.flatten(flattened); - BOOST_CHECK_EQUAL(flattened, std::string(output, sizeof(data))); - BOOST_CHECK_EQUAL(std::string(data, sizeof(data)), std::string(output, sizeof(data))); + ASSERT_EQ(flattened, std::string(output, sizeof(data))); + ASSERT_EQ(std::string(data, sizeof(data)), std::string(output, sizeof(data))); simple.clear(); } -BOOST_AUTO_TEST_CASE(request_storage_copy) { +TEST(request_test, request_storage_copy) { // Use a few byt chunks just to make it manageable. request_test original(64); static char quick_brown[] = "The quick brown fox jumps over the lazy dog."; @@ -63,13 +59,13 @@ BOOST_AUTO_TEST_CASE(request_storage_copy) { std::string output; request_test copy(original); size_t bytes_read = copy.read(output, 0, sizeof(quick_brown)); - BOOST_CHECK_EQUAL(bytes_read, sizeof(quick_brown)); + ASSERT_EQ(bytes_read, sizeof(quick_brown)); std::string flattened; copy.flatten(flattened); - BOOST_CHECK_EQUAL(flattened, std::string(output, sizeof(quick_brown))); - BOOST_CHECK_EQUAL(std::string(quick_brown, sizeof(quick_brown)), std::string(output, sizeof(quick_brown))); + ASSERT_EQ(flattened, std::string(output, sizeof(quick_brown))); + ASSERT_EQ(std::string(quick_brown, sizeof(quick_brown)), std::string(output, sizeof(quick_brown))); copy.clear(); flattened.clear(); original.flatten(flattened); - BOOST_CHECK_EQUAL(flattened, std::string(quick_brown, sizeof(quick_brown))); + ASSERT_EQ(flattened, std::string(quick_brown, sizeof(quick_brown))); } diff --git a/http/test/request_incremental_parser_test.cpp b/http/test/request_incremental_parser_test.cpp index 59dccf2e9..e0067ad0a 100644 --- a/http/test/request_incremental_parser_test.cpp +++ b/http/test/request_incremental_parser_test.cpp @@ -4,9 +4,7 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#define BOOST_TEST_MODULE HTTP Incremental Request Parser Test -#include -#include +#include #include #include #include @@ -30,11 +28,11 @@ namespace logic = boost::logic; namespace fusion = boost::fusion; using namespace boost::network::http; -BOOST_AUTO_TEST_CASE(incremental_parser_constructor) { +TEST(request_test, incremental_parser_constructor) { request_parser p; // default constructible } -BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_method) { +TEST(request_test, incremental_parser_parse_http_method) { request_parser p; logic::tribool parsed_ok = false; typedef request_parser request_parser_type; @@ -45,8 +43,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_method) { fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::method_done , valid_http_method); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(!boost::empty(result_range)); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(!boost::empty(result_range)); std::string parsed(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; @@ -55,12 +53,12 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_method) { fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::method_done , invalid_http_method); - BOOST_CHECK_EQUAL(parsed_ok, false); + ASSERT_EQ(parsed_ok, false); parsed.assign(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; } -BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_uri) { +TEST(request_test, incremental_parser_parse_http_uri) { request_parser p; logic::tribool parsed_ok = false; typedef request_parser request_parser_type; @@ -70,8 +68,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_uri) { std::string valid_http_request = "GET / HTTP/1.1\r\n"; fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::uri_done, valid_http_request); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(!boost::empty(result_range)); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(!boost::empty(result_range)); std::string parsed(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; @@ -79,12 +77,12 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_uri) { p.reset(); fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::uri_done, invalid_http_request); - BOOST_CHECK_EQUAL(parsed_ok, false); + ASSERT_EQ(parsed_ok, false); parsed.assign(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; } -BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { +TEST(request_test, incremental_parser_parse_http_version) { request_parser p; logic::tribool parsed_ok = false; typedef request_parser request_parser_type; @@ -94,8 +92,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { std::string valid_http_request = "GET / HTTP/1.1\r\n"; fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::version_done, valid_http_request); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(!boost::empty(result_range)); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(!boost::empty(result_range)); std::string parsed(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; @@ -103,12 +101,12 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { p.reset(); fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::version_done, invalid_http_request); - BOOST_CHECK_EQUAL(parsed_ok, false); + ASSERT_EQ(parsed_ok, false); parsed.assign(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; } -BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_headers) { +TEST(request_test, incremental_parser_parse_http_headers) { request_parser p; logic::tribool parsed_ok = false; typedef request_parser request_parser_type; @@ -118,8 +116,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_headers) { std::string valid_http_request = "GET / HTTP/1.1\r\nHost: cpp-netlib.org\r\n\r\n"; fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::headers_done, valid_http_request); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(!boost::empty(result_range)); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(!boost::empty(result_range)); std::string parsed(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; @@ -127,8 +125,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_headers) { p.reset(); fusion::tie(parsed_ok, result_range) = p.parse_until( request_parser_type::headers_done, valid_http_request); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(!boost::empty(result_range)); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(!boost::empty(result_range)); parsed.assign(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " << std::endl; } diff --git a/http/test/request_linearize_test.cpp b/http/test/request_linearize_test.cpp deleted file mode 100644 index 336f4c1d1..000000000 --- a/http/test/request_linearize_test.cpp +++ /dev/null @@ -1,25 +0,0 @@ - -// Copyright 2010 Dean Michael Berris. -// Copyright 2012 Google, Inc. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP Request Linearize Test -#include -#include -#include -#include - -namespace http = network::http; -namespace net = network; - -BOOST_AUTO_TEST_CASE(linearize_request) { - http::request request("http://www.boost.org"); - linearize(request, "GET", 1, 0, std::ostream_iterator(std::cout)); - linearize(request, "GET", 1, 1, std::ostream_iterator(std::cout)); -} - diff --git a/http/test/request_test.cpp b/http/test/request_test.cpp index 12fae9b5f..c8648d0e9 100644 --- a/http/test/request_test.cpp +++ b/http/test/request_test.cpp @@ -4,31 +4,28 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP Request Test +#include #include #include #include -#include +#include #include namespace http = network::http; namespace net = network; -BOOST_AUTO_TEST_CASE(request_construction) { +TEST(message_test, request_construction) { http::request request; http::request other(request); } -BOOST_AUTO_TEST_CASE(request_value_semantics) { +TEST(message_test, request_value_semantics) { // First let's default construct a request. http::request original; // Next let's copy the request. http::request copy(original); // Next let's compare the requests. - BOOST_CHECK(original == copy); + ASSERT_TRUE(original == copy); // Next let's assign the original to another request. http::request assigned; assigned = original; @@ -39,50 +36,50 @@ BOOST_AUTO_TEST_CASE(request_value_semantics) { assigned.set_destination("http://www.google.com/"); assigned.append_header("Connection", "close"); assigned.set_body("Hello, world!"); - BOOST_CHECK(original != assigned); + ASSERT_TRUE(original != assigned); // Next we swap the assigned and copy. std::swap(assigned, copy); - BOOST_CHECK(copy != assigned); - BOOST_CHECK(copy != original); - BOOST_CHECK(original == assigned); + ASSERT_TRUE(copy != assigned); + ASSERT_TRUE(copy != original); + ASSERT_TRUE(original == assigned); } -//BOOST_AUTO_TEST_CASE(request_uri_test) { -// http::request request; -// request.set_uri("http://www.google.com/"); -// http::request other(request); -// std::string original, copied; -// request.get_uri(original); -// other.get_uri(copied); -// BOOST_CHECK_EQUAL(std::string("http://www.google.com/"), original); -// BOOST_CHECK_EQUAL(original, copied); -// -// // Now we test the bare uri instance with accessing using the request -// // convenience wrapper. -// network::uri uri_; -// request.get_uri(uri_); -// std::string host_ = http::host(request); -// BOOST_CHECK(network::valid(uri_)); -// BOOST_CHECK_EQUAL(std::string("www.google.com"), host_); -// BOOST_CHECK_EQUAL(uri_.host(), host_); -// BOOST_CHECK_EQUAL(std::string("www.google.com"), uri_.host()); -//} +TEST(message_test, request_uri) { + http::request request; + request.set_uri("http://www.google.com/"); + http::request other(request); + std::string original, copied; + request.get_uri(original); + other.get_uri(copied); + ASSERT_EQ(std::string("http://www.google.com/"), original); + ASSERT_EQ(original, copied); + + // Now we test the bare uri instance with accessing using the request + // convenience wrapper. + network::uri uri_; + request.get_uri(uri_); + std::string host_ = http::host(request); + ASSERT_EQ(std::string("www.google.com"), host_); + std::string gotten_host(*uri_.host()); + ASSERT_EQ(gotten_host, host_); + ASSERT_EQ(std::string("www.google.com"), gotten_host); +} -BOOST_AUTO_TEST_CASE(request_url_constructor_test) { +TEST(message_test, request_url_constructor) { http::request request("http://www.google.com/"); http::request other; other.set_uri("http://www.google.com/"); network::uri original, other_uri; request.get_uri(original); other.get_uri(other_uri); - BOOST_CHECK_EQUAL(original, other_uri); + ASSERT_EQ(original, other_uri); // Now test the directives.. network::uri directive_original = http::uri(request); - BOOST_CHECK_EQUAL(original, directive_original); + ASSERT_EQ(original, directive_original); } -BOOST_AUTO_TEST_CASE(request_basics_test) { +TEST(message_test, request_basics) { http::request request; request.set_uri("http://www.google.com/"); request.set_source("127.0.0.1"); @@ -99,9 +96,16 @@ BOOST_AUTO_TEST_CASE(request_basics_test) { request.get_destination(destination_); request.get_body(body_); - BOOST_CHECK_EQUAL(uri_.string(), std::string("http://www.google.com/")); - BOOST_CHECK_EQUAL(source_, std::string("127.0.0.1")); - BOOST_CHECK_EQUAL(destination_, std::string("destination!")); - BOOST_CHECK_EQUAL(body_, std::string("The quick brown fox jumps over the lazy dog!")); - BOOST_CHECK(!boost::empty(headers_)); + ASSERT_EQ(uri_.string(), std::string("http://www.google.com/")); + ASSERT_EQ(source_, std::string("127.0.0.1")); + ASSERT_EQ(destination_, std::string("destination!")); + ASSERT_EQ(body_, std::string("The quick brown fox jumps over the lazy dog!")); + ASSERT_TRUE(!boost::empty(headers_)); +} + +TEST(message_test, linearize_request) { + http::request request("http://www.boost.org"); + // TODO: Actually specify the expected output. + linearize(request, "GET", 1, 0, std::ostream_iterator(std::cout)); + linearize(request, "GET", 2, 1, std::ostream_iterator(std::cout)); } diff --git a/http/test/response_incremental_parser_test.cpp b/http/test/response_incremental_parser_test.cpp index dd3ab1e7b..9b097d8b9 100644 --- a/http/test/response_incremental_parser_test.cpp +++ b/http/test/response_incremental_parser_test.cpp @@ -1,13 +1,11 @@ -// Copyright Dean Michael Berris 2010. +// Copyright Dean Michael Berris 2010. // Copyright 2012 Google, Inc. // Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) -#define BOOST_TEST_MODULE HTTP Incremental Parser Test -#include -#include +#include #include #include #include @@ -49,13 +47,12 @@ * Author: Dean Michael Berris */ -namespace tags = boost::network::tags; namespace logic = boost::logic; namespace fusion = boost::fusion; -using namespace boost::network::http; +using namespace network::http; -BOOST_AUTO_TEST_CASE(incremental_parser_constructor) { - response_parser p; // default constructible +TEST(response_test, incremental_parser_constructor) { + response_parser p; // default constructible } /** In this test we want to be able to parse incrementally a @@ -64,10 +61,10 @@ BOOST_AUTO_TEST_CASE(incremental_parser_constructor) { * we want it to parse until it either finds the HTTP version * or there is an error encountered. */ -BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { - response_parser p; // default constructible +TEST(response_test, incremental_parser_parse_http_version) { + response_parser p; // default constructible logic::tribool parsed_ok = false; - typedef response_parser response_parser_type; + typedef response_parser response_parser_type; typedef boost::iterator_range range_type; range_type result_range; @@ -75,8 +72,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_version_done, valid_http_version); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(!boost::empty(result_range)); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(!boost::empty(result_range)); std::string parsed(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; p.reset(); @@ -84,8 +81,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_version_done, valid_http_version); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(!boost::empty(result_range)); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(!boost::empty(result_range)); parsed.assign(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; @@ -95,7 +92,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_version_done, invalid_http_version); - BOOST_CHECK_EQUAL(parsed_ok, false); + ASSERT_EQ(parsed_ok, false); parsed.assign(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; @@ -105,7 +102,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_version_done, valid_http_version); - BOOST_CHECK_EQUAL(parsed_ok, true); + ASSERT_EQ(parsed_ok, true); parsed.assign(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; } @@ -115,8 +112,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { * the parser doesn't do any conversions from string to integer * and outsource that part to the user of the parser. */ -BOOST_AUTO_TEST_CASE(incremental_parser_parse_status) { - typedef response_parser response_parser_type; +TEST(response_test, incremental_parser_parse_status) { + typedef response_parser response_parser_type; typedef boost::iterator_range range_type; // We want to create a parser that has been initialized to a specific // state. In this case we assume that the parser has already parsed @@ -129,7 +126,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_status) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_status_done, valid_status); - BOOST_CHECK_EQUAL(parsed_ok, true); + ASSERT_EQ(parsed_ok, true); std::string parsed = std::string(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; @@ -138,7 +135,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_status) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_status_done, invalid_status); - BOOST_CHECK_EQUAL(parsed_ok, false); + ASSERT_EQ(parsed_ok, false); parsed = std::string(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; } @@ -146,8 +143,8 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_status) { /** In this test then we get the rest of the first line of the HTTP * Response, and treat it as the status message. */ -BOOST_AUTO_TEST_CASE(incremental_parser_parse_status_message) { - typedef response_parser response_parser_type; +TEST(response_test, incremental_parser_parse_status_message) { + typedef response_parser response_parser_type; typedef boost::iterator_range range_type; response_parser_type p(response_parser_type::http_status_done); @@ -157,7 +154,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_status_message) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_status_message_done, valid_status_message); - BOOST_CHECK_EQUAL(parsed_ok, true); + ASSERT_EQ(parsed_ok, true); std::string parsed = std::string(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; @@ -166,7 +163,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_status_message) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_status_message_done, valid_status_message); - BOOST_CHECK_EQUAL(parsed_ok, true); + ASSERT_EQ(parsed_ok, true); parsed = std::string(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; @@ -175,15 +172,15 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_status_message) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_status_message_done, valid_status_message); - BOOST_CHECK_EQUAL(parsed_ok, true); + ASSERT_EQ(parsed_ok, true); parsed = std::string(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; } /** This test specifices how one-line-per-header parsing happens incrementally. */ -BOOST_AUTO_TEST_CASE(incremental_parser_parse_header_lines) { - typedef response_parser response_parser_type; +TEST(response_test, incremental_parser_parse_header_lines) { + typedef response_parser response_parser_type; typedef boost::iterator_range range_type; response_parser_type p(response_parser_type::http_status_message_done); @@ -193,7 +190,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_header_lines) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_header_line_done, valid_headers); - BOOST_CHECK_EQUAL(parsed_ok, true); + ASSERT_EQ(parsed_ok, true); std::string parsed1 = std::string(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed1 << " state=" << p.state() << std::endl; p.reset(response_parser_type::http_status_message_done); @@ -202,7 +199,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_header_lines) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_header_line_done, valid_headers); - BOOST_CHECK_EQUAL(parsed_ok, true); + ASSERT_EQ(parsed_ok, true); std::string parsed2 = std::string(boost::begin(result_range), boost::end(result_range)); std::cout << "PARSED: " << parsed2 << " state=" << p.state() << std::endl; valid_headers.assign(boost::end(result_range), end); @@ -210,7 +207,7 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_header_lines) { fusion::tie(parsed_ok, result_range) = p.parse_until( response_parser_type::http_headers_done, valid_headers); - BOOST_CHECK_EQUAL(parsed_ok, true); - BOOST_CHECK(parsed1 != parsed2); + ASSERT_EQ(parsed_ok, true); + ASSERT_TRUE(parsed1 != parsed2); } diff --git a/http/test/response_test.cpp b/http/test/response_test.cpp index 4e93db284..2052821c9 100644 --- a/http/test/response_test.cpp +++ b/http/test/response_test.cpp @@ -4,33 +4,29 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE HTTP Client Response Test +#include #include -#include namespace http = network::http; -BOOST_AUTO_TEST_CASE(response_constructor_test) { +TEST(response_test, response_constructor) { http::response created; } -BOOST_AUTO_TEST_CASE(response_value_semantics_test) { +TEST(response_test, response_value_semantics) { http::response original; http::response copy(original); http::response assigned; assigned = original; - BOOST_CHECK(original == assigned); + ASSERT_TRUE(original == assigned); assigned.set_source("http://www.google.com/"); - BOOST_CHECK(original != assigned); + ASSERT_TRUE(original != assigned); std::swap(assigned, copy); - BOOST_CHECK(assigned == original); - BOOST_CHECK(copy != original); - BOOST_CHECK(assigned != copy); + ASSERT_TRUE(assigned == original); + ASSERT_TRUE(copy != original); + ASSERT_TRUE(assigned != copy); original = copy; - BOOST_CHECK(original == copy); + ASSERT_TRUE(original == copy); } struct multimap_inserter { @@ -43,7 +39,7 @@ struct multimap_inserter { std::multimap & multimap_; }; -BOOST_AUTO_TEST_CASE(response_setters_and_getters_test) { +TEST(response_test, response_setters_and_getters) { http::response response; response.set_source("http://www.google.com/"); response.set_destination("127.0.0.1"); @@ -65,10 +61,10 @@ BOOST_AUTO_TEST_CASE(response_setters_and_getters_test) { response.get_version(version); response.get_headers(multimap_inserter(headers)); response.get_status(status); - BOOST_CHECK_EQUAL(source, std::string("http://www.google.com/")); - BOOST_CHECK_EQUAL(destination, std::string("127.0.0.1")); - BOOST_CHECK_EQUAL(body, std::string("Hello, World!")); - BOOST_CHECK_EQUAL(status, 200u); - BOOST_CHECK_EQUAL(version, std::string("HTTP/1.1")); - BOOST_CHECK(expected_headers == headers); + ASSERT_EQ(source, std::string("http://www.google.com/")); + ASSERT_EQ(destination, std::string("127.0.0.1")); + ASSERT_EQ(body, std::string("Hello, World!")); + ASSERT_EQ(status, 200u); + ASSERT_EQ(version, std::string("HTTP/1.1")); + ASSERT_TRUE(expected_headers == headers); } diff --git a/logging/test/CMakeLists.txt b/logging/test/CMakeLists.txt index 34e70348c..25a65ad6c 100644 --- a/logging/test/CMakeLists.txt +++ b/logging/test/CMakeLists.txt @@ -4,11 +4,12 @@ # (See accompanying file LICENSE_1_0.txt or copy at # http://www.boost.org/LICENSE_1_0.txt) +include_directories(${GTEST_INCLUDE_DIRS}) include_directories(${CPP-NETLIB_SOURCE_DIR}/uri/src) include_directories(${CPP-NETLIB_SOURCE_DIR}/message/src) include_directories(${CPP-NETLIB_SOURCE_DIR}/logging/src) -if (Boost_FOUND) +if (CPP-NETLIB_BUILD_TESTS) set( TESTS logging_log_record @@ -22,10 +23,10 @@ if (Boost_FOUND) add_executable(cpp-netlib-${test} ${test}.cpp) add_dependencies(cpp-netlib-${test} cppnetlib-logging) target_link_libraries(cpp-netlib-${test} - ${Boost_LIBRARIES} cppnetlib-logging) + ${Boost_LIBRARIES} ${GTEST_BOTH_LIBRARIES} cppnetlib-logging) set_target_properties(cpp-netlib-${test} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests) add_test(cpp-netlib-${test} ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-${test}) endforeach (test) -endif() +endif (CPP-NETLIB_BUILD_TESTS) diff --git a/logging/test/logging_custom_handler.cpp b/logging/test/logging_custom_handler.cpp index 989955439..f9621d53c 100644 --- a/logging/test/logging_custom_handler.cpp +++ b/logging/test/logging_custom_handler.cpp @@ -6,15 +6,13 @@ #include #include -#define BOOST_TEST_MODULE logging log_record -#include -#include +#include #include using namespace network::logging; -BOOST_AUTO_TEST_CASE(custom_log_handler_output) { +TEST(logging_custom_handler, custom_log_handler_output) { std::stringstream log_output; auto custom_log_handler = [&]( const log_record& log ) @@ -32,6 +30,6 @@ BOOST_AUTO_TEST_CASE(custom_log_handler_output) { const auto result_output = log_output.str(); - BOOST_CHECK( !result_output.empty() ); - BOOST_CHECK( result_output == "[CPPNETLIB] " + message ); + ASSERT_TRUE( !result_output.empty() ); + ASSERT_TRUE( result_output == "[CPPNETLIB] " + message ); } diff --git a/logging/test/logging_log_record.cpp b/logging/test/logging_log_record.cpp index 212c8f055..276205a9f 100644 --- a/logging/test/logging_log_record.cpp +++ b/logging/test/logging_log_record.cpp @@ -5,9 +5,7 @@ #include -#define BOOST_TEST_MODULE logging log_record -#include -#include +#include #include #define NETWORK_ENABLE_LOGGING @@ -15,47 +13,47 @@ using namespace network::logging; -BOOST_AUTO_TEST_CASE(default_constructor) { +TEST(logging_log_record, default_constructor) { log_record record; - BOOST_CHECK( record.message() == "" ); - BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME ); - BOOST_CHECK( record.line() == 0 ); + ASSERT_TRUE( record.message() == "" ); + ASSERT_TRUE( record.filename() == log_record::UNKNOWN_FILE_NAME ); + ASSERT_TRUE( record.line() == 0 ); } -BOOST_AUTO_TEST_CASE(cstring_constructor) { +TEST(logging_log_record, cstring_constructor) { const auto message = "This is a test."; log_record record( message ); - BOOST_CHECK( record.message() == message ); - BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME ); - BOOST_CHECK( record.line() == 0 ); + ASSERT_TRUE( record.message() == message ); + ASSERT_TRUE( record.filename() == log_record::UNKNOWN_FILE_NAME ); + ASSERT_TRUE( record.line() == 0 ); } -BOOST_AUTO_TEST_CASE(string_constructor) { +TEST(logging_log_record, string_constructor) { const std::string message("This is a test."); log_record record( message ); - BOOST_CHECK( record.message() == message ); - BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME ); - BOOST_CHECK( record.line() == 0 ); + ASSERT_TRUE( record.message() == message ); + ASSERT_TRUE( record.filename() == log_record::UNKNOWN_FILE_NAME ); + ASSERT_TRUE( record.line() == 0 ); } -BOOST_AUTO_TEST_CASE(int_constructor) { +TEST(logging_log_record, int_constructor) { const auto num = 42; log_record record( num ); - BOOST_CHECK( record.message() == std::to_string( num ) ); - BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME ); - BOOST_CHECK( record.line() == 0 ); + ASSERT_TRUE( record.message() == std::to_string( num ) ); + ASSERT_TRUE( record.filename() == log_record::UNKNOWN_FILE_NAME ); + ASSERT_TRUE( record.line() == 0 ); } -BOOST_AUTO_TEST_CASE(info_constructor) { +TEST(logging_log_record, info_constructor) { const auto line_num = 42; const auto file_name = "somewhere.cpp"; log_record record( file_name, line_num ); - BOOST_CHECK( record.message() == "" ); - BOOST_CHECK( record.filename() == file_name ); - BOOST_CHECK( record.line() == line_num ); + ASSERT_TRUE( record.message() == "" ); + ASSERT_TRUE( record.filename() == file_name ); + ASSERT_TRUE( record.line() == line_num ); } -BOOST_AUTO_TEST_CASE(text_stream) { +TEST(logging_log_record, text_stream) { const auto line_num = 42; const auto file_name = "somewhere.cpp"; const auto message = "At line " + std::to_string(line_num) + " we check the code."; @@ -63,16 +61,16 @@ BOOST_AUTO_TEST_CASE(text_stream) { record << "At line " << line_num << " we check the code."; - BOOST_CHECK( record.message() == message ); - BOOST_CHECK( record.filename() == file_name ); - BOOST_CHECK( record.line() == line_num ); + ASSERT_TRUE( record.message() == message ); + ASSERT_TRUE( record.filename() == file_name ); + ASSERT_TRUE( record.line() == line_num ); } -BOOST_AUTO_TEST_CASE(raw_log) { +TEST(logging_log_record, raw_log) { log( "This is a raw log." ); } -BOOST_AUTO_TEST_CASE(macro_log) { +TEST(logging_log_record, macro_log) { NETWORK_MESSAGE( "This is a log through the macro." ); NETWORK_MESSAGE( "This is a log through the macro, with a stream! Num=" << 42 << " - OK!" ); } \ No newline at end of file diff --git a/message/test/CMakeLists.txt b/message/test/CMakeLists.txt index e5fd3dcbc..c90c336ae 100644 --- a/message/test/CMakeLists.txt +++ b/message/test/CMakeLists.txt @@ -3,24 +3,22 @@ # (See accompanying file LICENSE_1_0.txt or copy at # http://www.boost.org/LICENSE_1_0.txt) +include_directories(${GTEST_INCLUDE_DIRS}) include_directories(${CPP-NETLIB_SOURCE_DIR}/uri/src) include_directories(${CPP-NETLIB_SOURCE_DIR}/message/src) -if (Boost_FOUND) - set( - TESTS - message_test - message_transform_test - ) +if (CPP-NETLIB_BUILD_TESTS) + set(TESTS message_test message_transform_test) foreach (test ${TESTS}) if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU) set_source_files_properties(${test}.cpp PROPERTIES COMPILE_FLAGS "-Wall") endif() add_executable(cpp-netlib-${test} ${test}.cpp) - add_dependencies(cpp-netlib-${test} cppnetlib-uri cppnetlib-message cppnetlib-message-directives cppnetlib-message-wrappers) + add_dependencies(cpp-netlib-${test} cppnetlib-message cppnetlib-message-directives cppnetlib-message-wrappers) target_link_libraries(cpp-netlib-${test} - ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} cppnetlib-uri cppnetlib-message cppnetlib-message-directives cppnetlib-message-wrappers) + ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${GTEST_BOTH_LIBRARIES} + cppnetlib-message cppnetlib-message-directives cppnetlib-message-wrappers) if (OPENSSL_FOUND) target_link_libraries(cpp-netlib-${test} ${OPENSSL_LIBRARIES}) endif() @@ -29,4 +27,4 @@ if (Boost_FOUND) add_test(cpp-netlib-${test} ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-${test}) endforeach (test) -endif (Boost_FOUND) +endif (CPP-NETLIB_BUILD_TESTS) diff --git a/message/test/message_test.cpp b/message/test/message_test.cpp index 63b958c9c..e8fcd366d 100644 --- a/message/test/message_test.cpp +++ b/message/test/message_test.cpp @@ -1,83 +1,73 @@ -// Copyright Dean Michael Berris 2007. +// Copyright 2007, 2012 Dean Michael Berris // Copyright 2012 Google, Inc. // Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE message test -#include -#include +#include #include #include using namespace network; -/** - * Defines a set of template functions that can be used to test - * generic code. - */ - -BOOST_AUTO_TEST_CASE(copy_constructor_test) { +TEST(message_test, copy_constructor) { message instance; instance << header("name", "value"); message copy(instance); headers_wrapper::container_type const &headers_ = headers(copy); - BOOST_CHECK_EQUAL(headers_.count("name"), static_cast(1)); + ASSERT_EQ(headers_.count("name"), static_cast(1)); message::headers_range range = headers_.equal_range("name"); - BOOST_CHECK (!boost::empty(range)); + ASSERT_TRUE (!boost::empty(range)); } -BOOST_AUTO_TEST_CASE(swap_test) { +TEST(message_test, swap) { message instance; instance << header("name", "value"); message other; swap(instance, other); headers_wrapper::container_type const &instance_headers = headers(instance); headers_wrapper::container_type const &other_headers = headers(other); - BOOST_CHECK_EQUAL (instance_headers.count("name"), static_cast(0)); - BOOST_CHECK_EQUAL (other_headers.count("name"), static_cast(1)); + ASSERT_EQ (instance_headers.count("name"), static_cast(0)); + ASSERT_EQ (other_headers.count("name"), static_cast(1)); } -BOOST_AUTO_TEST_CASE(headers_directive_test) { +TEST(message_test, headers_directive) { message instance; instance << header("name", "value"); headers_wrapper::container_type const &instance_headers = headers(instance); - BOOST_CHECK_EQUAL ( instance_headers.count("name"), static_cast(1) ); + ASSERT_EQ ( instance_headers.count("name"), static_cast(1) ); message::headers_range range = instance_headers.equal_range("name"); - BOOST_CHECK (boost::begin(range) != boost::end(range)); + ASSERT_TRUE (boost::begin(range) != boost::end(range)); } -BOOST_AUTO_TEST_CASE(body_directive_test) { +TEST(message_test, body_directive) { message instance; instance << ::network::body("body"); std::string body_string = body(instance); - BOOST_CHECK ( body_string == "body" ); + ASSERT_TRUE ( body_string == "body" ); } -BOOST_AUTO_TEST_CASE(source_directive_test) { +TEST(message_test, source_directive) { message instance; instance << ::network::source("source"); std::string source_string = source(instance); - BOOST_CHECK ( source_string == "source" ); + ASSERT_TRUE ( source_string == "source" ); } -BOOST_AUTO_TEST_CASE(destination_directive_test) { +TEST(message_test, destination_directive) { message instance; instance << destination("destination"); std::string const & destination_ = destination(instance); - BOOST_CHECK ( destination_ == "destination" ); + ASSERT_TRUE ( destination_ == "destination" ); } -BOOST_AUTO_TEST_CASE(remove_header_directive_test) { +TEST(message_test, remove_header_directive) { message instance; instance << header("name", "value") << remove_header("name"); headers_wrapper::container_type const &instance_headers = headers(instance); message::headers_range range = instance_headers.equal_range("name"); - BOOST_CHECK ( boost::begin(range) == boost::end(range) ); + ASSERT_TRUE ( boost::begin(range) == boost::end(range) ); } diff --git a/message/test/message_transform_test.cpp b/message/test/message_transform_test.cpp index fc62c86e5..63a2a45b2 100644 --- a/message/test/message_transform_test.cpp +++ b/message/test/message_transform_test.cpp @@ -1,52 +1,47 @@ -// Copyright Dean Michael Berris 2007. +// Copyright 2007, 2012 Dean Michael Berris // Copyright 2012 Google, Inc. // Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) -#ifdef BUILD_SHARED_LIBS -# define BOOST_TEST_DYN_LINK -#endif -#define BOOST_TEST_MODULE message test -#include -#include +#include #include #include -BOOST_AUTO_TEST_CASE ( message_transform_toupper ) { +TEST(message_test, message_transform_toupper) { using namespace network; message msg; msg << source("me"); std::string const & source_orig = source(msg); - BOOST_CHECK_EQUAL ( source_orig, "me" ); + ASSERT_EQ ( source_orig, "me" ); msg << transform(to_upper_, source_); std::string const & source_upper = source(msg); - BOOST_CHECK_EQUAL ( source_upper, "ME" ); + ASSERT_EQ ( source_upper, "ME" ); msg << destination("you"); std::string const & destination_orig = destination(msg); - BOOST_CHECK_EQUAL ( destination_orig, "you"); + ASSERT_EQ ( destination_orig, "you"); msg << transform(to_upper_, destination_); std::string const & destination_upper = destination(msg); - BOOST_CHECK_EQUAL ( destination_upper, "YOU"); + ASSERT_EQ ( destination_upper, "YOU"); } -BOOST_AUTO_TEST_CASE ( message_transform_tolower ) { +TEST(message_test, message_transform_tolower) { using namespace network; message msg; msg << source("ME"); std::string const & source_orig = source(msg); - BOOST_CHECK_EQUAL ( source_orig, "ME" ); + ASSERT_EQ ( source_orig, "ME" ); msg << transform(to_lower_, source_); std::string const & source_lower = source(msg); - BOOST_CHECK_EQUAL ( source_lower, "me" ); + ASSERT_EQ ( source_lower, "me" ); msg << destination("YOU"); std::string const & destination_orig = destination(msg); - BOOST_CHECK_EQUAL ( destination_orig, "YOU" ); + ASSERT_EQ ( destination_orig, "YOU" ); msg << transform(to_lower_, destination_); std::string const & destination_lower = destination(msg); - BOOST_CHECK_EQUAL ( destination_lower, "you" ); + ASSERT_EQ ( destination_lower, "you" ); }