Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate message, request, and response tests
  • Loading branch information
deanberris committed Jan 22, 2013
commit 4874357f15d17cdb83c782da4dff6858d6bed306
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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()
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.")
Expand Down
6 changes: 3 additions & 3 deletions http/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,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)
Expand Down
24 changes: 10 additions & 14 deletions http/test/request_base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gtest/gtest.h>
#include <network/protocol/http/request/request_base.hpp>
#include <boost/test/unit_test.hpp>

namespace http = network::http;

Expand Down Expand Up @@ -39,37 +35,37 @@ 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[] =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus vitae ante sed nunc dapibus convallis in at neque. Vestibulum sed congue nunc. Sed tempus lorem non dui ultrices porttitor porta ligula venenatis. Sed a orci gravida tellus condimentum laoreet. Vivamus pulvinar, tortor eu adipiscing tempus, dolor urna tincidunt enim, id pretium eros ante quis dui. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In hac habitasse platea dictumst. Maecenas mattis metus.";
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.";
original.append(quick_brown, sizeof(quick_brown));
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)));
}
40 changes: 19 additions & 21 deletions http/test/request_incremental_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <boost/config/warning_disable.hpp>
#include <boost/test/unit_test.hpp>
#include <gtest/gtest.h>
#include <network/protocol/http/server/request_parser.hpp>
#include <network/tags.hpp>
#include <boost/range.hpp>
Expand All @@ -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<tags::default_string> p; // default constructible
}

BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_method) {
TEST(request_test, incremental_parser_parse_http_method) {
request_parser<tags::default_string> p;
logic::tribool parsed_ok = false;
typedef request_parser<tags::default_string> request_parser_type;
Expand All @@ -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;

Expand All @@ -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<tags::default_string> p;
logic::tribool parsed_ok = false;
typedef request_parser<tags::default_string> request_parser_type;
Expand All @@ -70,21 +68,21 @@ 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;

std::string invalid_http_request = "GET /\t HTTP/1.1\r\n";
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<tags::default_string> p;
logic::tribool parsed_ok = false;
typedef request_parser<tags::default_string> request_parser_type;
Expand All @@ -94,21 +92,21 @@ 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;

std::string invalid_http_request = "GET / HTTP 1.1\r\n";
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<tags::default_string> p;
logic::tribool parsed_ok = false;
typedef request_parser<tags::default_string> request_parser_type;
Expand All @@ -118,17 +116,17 @@ 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;

valid_http_request = "GET / HTTP/1.1\r\nHost: cpp-netlib.org\r\nConnection: close\r\n\r\n";
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;
}
Expand Down
25 changes: 0 additions & 25 deletions http/test/request_linearize_test.cpp

This file was deleted.

86 changes: 45 additions & 41 deletions http/test/request_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gtest/gtest.h>
#include <network/protocol/http/request.hpp>
#include <network/protocol/http/message/wrappers.hpp>
#include <network/message/wrappers.hpp>
#include <boost/test/unit_test.hpp>
#include <network/protocol/http/algorithms/linearize.hpp>
#include <network/uri/uri_io.hpp>

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;
Expand All @@ -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");
Expand All @@ -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<char>(std::cout));
linearize(request, "GET", 2, 1, std::ostream_iterator<char>(std::cout));
}
Loading