Skip to content

Commit 07e1e8e

Browse files
committed
Moved some of the tests around; added class that will represent HTTP URLs.
1 parent 6b51a82 commit 07e1e8e

15 files changed

+774
-1
lines changed

http/src/network/http/v2/url.hpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef __TTP_NETWORK_HTTP_V2_URL_INC__
7+
#define __HTTP_NETWORK_HTTP_V2_URL_INC__
8+
9+
#include <network/uri.hpp>
10+
11+
namespace network {
12+
namespace http {
13+
namespace v2 {
14+
class invalid_scheme : public std::runtime_error {
15+
16+
public:
17+
18+
virtual ~invalid_scheme() noexcept {}
19+
virtual const char *what() const noexcept {
20+
return "invalid_scheme";
21+
}
22+
23+
};
24+
25+
class url {
26+
27+
public:
28+
29+
typedef uri::string_type string_type;
30+
typedef uri::iterator const_iterator;
31+
typedef uri::const_iterator iterator;
32+
typedef uri::value_type value_type;
33+
typedef uri::string_view string_view;
34+
35+
public:
36+
37+
url() {
38+
39+
}
40+
41+
url(const url &other)
42+
: uri_(other.uri_) {
43+
44+
}
45+
46+
url(url &&other)
47+
: uri_(other.uri_) {
48+
49+
}
50+
51+
url &operator = (url other) {
52+
other.swap(*this);
53+
return *this;
54+
}
55+
56+
void swap(url &other) noexcept {
57+
uri_.swap(other.uri_);
58+
}
59+
60+
const_iterator begin() const {
61+
return uri_.begin();
62+
}
63+
64+
const_iterator end() const {
65+
return uri_.end();
66+
}
67+
68+
bool empty() const noexcept {
69+
return uri_.empty();
70+
}
71+
72+
bool is_absolute() const noexcept {
73+
return uri_.is_absolute();
74+
}
75+
76+
private:
77+
78+
uri uri_;
79+
80+
};
81+
} // namespace v2
82+
} // namespace http
83+
} // namespace network
84+
85+
86+
#endif // __HTTP_NETWORK_HTTP_V2_URL_INC__

http/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ include_directories(
1212
${GTEST_INCLUDE_DIRS}
1313
${CPP-NETLIB_SOURCE_DIR})
1414

15-
add_subdirectory(client/v2)
15+
add_subdirectory(v2)
1616

1717
if (OPENSSL_FOUND)
1818
include_directories( ${OPENSSL_INCLUDE_DIR} )

http/test/v2/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) 2013 by Glyn Matthews
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
set(CPP-NETLIB_CLIENT_TESTS
7+
url_test
8+
)
9+
10+
foreach(test ${CPP-NETLIB_CLIENT_TESTS})
11+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
12+
set_source_files_properties(${test}.cpp
13+
PROPERTIES COMPILE_FLAGS "-Wall")
14+
endif()
15+
16+
add_executable(cpp-netlib-http-v2-${test} ${test}.cpp)
17+
target_link_libraries(cpp-netlib-http-v2-${test}
18+
cppnetlib-uri
19+
${Boost_LIBRARIES}
20+
${GTEST_BOTH_LIBRARIES}
21+
${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES}
22+
${CMAKE_THREAD_LIBS_INIT}
23+
)
24+
set_target_properties(cpp-netlib-http-v2-${test}
25+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
26+
add_test(cpp-netlib-http-v2-${test}
27+
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-v2-${test})
28+
29+
endforeach(test)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2012 Dean Michael Berris <dberris@google.com>.
2+
// Copyright 2012 Google, Inc.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include <gtest/gtest.h>
8+
#include <network/protocol/http/request/request_base.hpp>
9+
10+
namespace http = network::http;
11+
12+
// In this test we make sure that the implementation of the default request
13+
// storage base actually doesn't have bugs and works as advertised. Although we
14+
// don't intend to expose this interface to users, we use the test as a sanity
15+
// check on the internals of the implementation.
16+
struct request_test : http::request_storage_base {
17+
typedef http::request_storage_base base_type;
18+
19+
// Expose the protected functions so that we can test them.
20+
using base_type::append;
21+
using base_type::read;
22+
using base_type::flatten;
23+
using base_type::clear;
24+
25+
explicit request_test(size_t chunk_size) : base_type(chunk_size) {}
26+
27+
request_test(request_test const& other) : base_type(other) {}
28+
29+
~request_test() {
30+
// do nothing here.
31+
}
32+
};
33+
34+
TEST(request_test, request_storage_flow) {
35+
// Use a few byte chunks just to make it manageable.
36+
request_test simple(64);
37+
static char data[] =
38+
"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.";
39+
simple.append(data, sizeof(data));
40+
std::string output;
41+
size_t bytes_read = simple.read(output, 0, sizeof(data));
42+
ASSERT_EQ(bytes_read, sizeof(data));
43+
std::string flattened;
44+
simple.flatten(flattened);
45+
ASSERT_EQ(flattened, output);
46+
ASSERT_EQ(std::string(data, sizeof(data)), output);
47+
simple.clear();
48+
}
49+
50+
TEST(request_test, request_storage_copy) {
51+
// Use a few byt chunks just to make it manageable.
52+
request_test original(64);
53+
static char quick_brown[] = "The quick brown fox jumps over the lazy dog.";
54+
original.append(quick_brown, sizeof(quick_brown));
55+
std::string output;
56+
request_test copy(original);
57+
size_t bytes_read = copy.read(output, 0, sizeof(quick_brown));
58+
ASSERT_EQ(bytes_read, sizeof(quick_brown));
59+
std::string flattened;
60+
copy.flatten(flattened);
61+
ASSERT_EQ(flattened, output);
62+
ASSERT_EQ(std::string(quick_brown, sizeof(quick_brown)), output);
63+
copy.clear();
64+
flattened.clear();
65+
original.flatten(flattened);
66+
ASSERT_EQ(flattened, std::string(quick_brown, sizeof(quick_brown)));
67+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Copyright 2010 Dean Michael Berris.
3+
// Copyright 2012 Google, Inc.
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE_1_0.txt or copy at
6+
// http://www.boost.org/LICENSE_1_0.txt)
7+
8+
#include <gtest/gtest.h>
9+
#include <network/protocol/http/server/request_parser.hpp>
10+
#include <network/tags.hpp>
11+
#include <boost/range.hpp>
12+
#include <boost/logic/tribool.hpp>
13+
#include <string>
14+
#include <iostream>
15+
16+
/** Synopsis
17+
*
18+
* Test for the HTTP Request Incremental Parser
19+
* --------------------------------------------
20+
*
21+
* In this test we fully intend to specify how an incremental HTTP request
22+
* parser should be used. This follows the HTTP Response Incremental Parser
23+
* example, and models the Incremental Parser Concept.
24+
*
25+
*/
26+
27+
namespace tags = boost::network::tags;
28+
namespace logic = boost::logic;
29+
namespace fusion = boost::fusion;
30+
using namespace boost::network::http;
31+
32+
TEST(request_test, incremental_parser_constructor) {
33+
request_parser<tags::default_string> p; // default constructible
34+
}
35+
36+
TEST(request_test, incremental_parser_parse_http_method) {
37+
request_parser<tags::default_string> p;
38+
logic::tribool parsed_ok = false;
39+
typedef request_parser<tags::default_string> request_parser_type;
40+
typedef boost::iterator_range<std::string::const_iterator> range_type;
41+
range_type result_range;
42+
43+
std::string valid_http_method = "GET ";
44+
fusion::tie(parsed_ok, result_range) =
45+
p.parse_until(request_parser_type::method_done, valid_http_method);
46+
ASSERT_EQ(parsed_ok, true);
47+
ASSERT_TRUE(!boost::empty(result_range));
48+
std::string parsed(boost::begin(result_range), boost::end(result_range));
49+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
50+
<< std::endl;
51+
52+
std::string invalid_http_method = "get ";
53+
p.reset();
54+
fusion::tie(parsed_ok, result_range) =
55+
p.parse_until(request_parser_type::method_done, invalid_http_method);
56+
ASSERT_EQ(parsed_ok, false);
57+
parsed.assign(boost::begin(result_range), boost::end(result_range));
58+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
59+
<< std::endl;
60+
}
61+
62+
TEST(request_test, incremental_parser_parse_http_uri) {
63+
request_parser<tags::default_string> p;
64+
logic::tribool parsed_ok = false;
65+
typedef request_parser<tags::default_string> request_parser_type;
66+
typedef boost::iterator_range<std::string::const_iterator> range_type;
67+
range_type result_range;
68+
69+
std::string valid_http_request = "GET / HTTP/1.1\r\n";
70+
fusion::tie(parsed_ok, result_range) =
71+
p.parse_until(request_parser_type::uri_done, valid_http_request);
72+
ASSERT_EQ(parsed_ok, true);
73+
ASSERT_TRUE(!boost::empty(result_range));
74+
std::string parsed(boost::begin(result_range), boost::end(result_range));
75+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
76+
<< std::endl;
77+
78+
std::string invalid_http_request = "GET /\t HTTP/1.1\r\n";
79+
p.reset();
80+
fusion::tie(parsed_ok, result_range) =
81+
p.parse_until(request_parser_type::uri_done, invalid_http_request);
82+
ASSERT_EQ(parsed_ok, false);
83+
parsed.assign(boost::begin(result_range), boost::end(result_range));
84+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
85+
<< std::endl;
86+
}
87+
88+
TEST(request_test, incremental_parser_parse_http_version) {
89+
request_parser<tags::default_string> p;
90+
logic::tribool parsed_ok = false;
91+
typedef request_parser<tags::default_string> request_parser_type;
92+
typedef boost::iterator_range<std::string::const_iterator> range_type;
93+
range_type result_range;
94+
95+
std::string valid_http_request = "GET / HTTP/1.1\r\n";
96+
fusion::tie(parsed_ok, result_range) =
97+
p.parse_until(request_parser_type::version_done, valid_http_request);
98+
ASSERT_EQ(parsed_ok, true);
99+
ASSERT_TRUE(!boost::empty(result_range));
100+
std::string parsed(boost::begin(result_range), boost::end(result_range));
101+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
102+
<< std::endl;
103+
104+
std::string invalid_http_request = "GET / HTTP 1.1\r\n";
105+
p.reset();
106+
fusion::tie(parsed_ok, result_range) =
107+
p.parse_until(request_parser_type::version_done, invalid_http_request);
108+
ASSERT_EQ(parsed_ok, false);
109+
parsed.assign(boost::begin(result_range), boost::end(result_range));
110+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
111+
<< std::endl;
112+
}
113+
114+
TEST(request_test, incremental_parser_parse_http_headers) {
115+
request_parser<tags::default_string> p;
116+
logic::tribool parsed_ok = false;
117+
typedef request_parser<tags::default_string> request_parser_type;
118+
typedef boost::iterator_range<std::string::const_iterator> range_type;
119+
range_type result_range;
120+
121+
std::string valid_http_request =
122+
"GET / HTTP/1.1\r\nHost: cpp-netlib.org\r\n\r\n";
123+
fusion::tie(parsed_ok, result_range) =
124+
p.parse_until(request_parser_type::headers_done, valid_http_request);
125+
ASSERT_EQ(parsed_ok, true);
126+
ASSERT_TRUE(!boost::empty(result_range));
127+
std::string parsed(boost::begin(result_range), boost::end(result_range));
128+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
129+
<< std::endl;
130+
131+
valid_http_request =
132+
"GET / HTTP/1.1\r\nHost: cpp-netlib.org\r\nConnection: close\r\n\r\n";
133+
p.reset();
134+
fusion::tie(parsed_ok, result_range) =
135+
p.parse_until(request_parser_type::headers_done, valid_http_request);
136+
ASSERT_EQ(parsed_ok, true);
137+
ASSERT_TRUE(!boost::empty(result_range));
138+
parsed.assign(boost::begin(result_range), boost::end(result_range));
139+
std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] "
140+
<< std::endl;
141+
}
142+

0 commit comments

Comments
 (0)