Skip to content

Commit 0c85833

Browse files
committed
Updated tests for client.
1 parent 74d99b8 commit 0c85833

File tree

8 files changed

+125
-74
lines changed

8 files changed

+125
-74
lines changed

http/src/http/v2/client/client.cpp

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
#include <future>
7+
#include <network/uri.hpp>
78
#include <network/config.hpp>
89
#include <network/http/v2/client/client.hpp>
910
#include <network/http/v2/method.hpp>
@@ -23,37 +24,32 @@ namespace network {
2324
//
2425
// }
2526
//
27+
// void connect(client::string_type host,
28+
// const boost::system::error_code &ec,
29+
// tcp::resolver::iterator endpoint_iterator);
30+
//
31+
// void write_request(const boost::system::error_code &ec);
32+
//
33+
// void read_response_status(const boost::system::error_code &ec,
34+
// std::size_t bytes_written);
35+
//
36+
// void read_response_headers(const boost::system::error_code &ec,
37+
// std::size_t bytes_read);
38+
//
2639
// client::string_type host_;
2740
// boost::asio::streambuf request_;
2841
// std::promise<response> response_promise_;
2942
// boost::asio::streambuf response_;
3043
//
3144
//};
3245

33-
struct resolve_helper {
34-
35-
};
36-
37-
struct connect_helper {
38-
39-
};
40-
41-
struct request_helper {
42-
43-
};
44-
45-
struct response_helper {
46-
47-
};
48-
4946
struct client::impl {
5047

5148
explicit impl(client_options options);
5249

5350
~impl() noexcept;
5451

55-
void connect(client::string_type host,
56-
const boost::system::error_code &ec,
52+
void connect(const boost::system::error_code &ec,
5753
tcp::resolver::iterator endpoint_iterator);
5854

5955
void write_request(const boost::system::error_code &ec);
@@ -68,10 +64,10 @@ namespace network {
6864

6965
client_options options_;
7066
boost::asio::io_service io_service_;
71-
std::unique_ptr<boost::asio::io_service::work> sentinel_;
72-
std::thread lifetime_thread_;
7367
async_resolver resolver_;
7468
normal_connection connection_;
69+
std::unique_ptr<boost::asio::io_service::work> sentinel_;
70+
std::thread lifetime_thread_;
7571

7672
boost::asio::streambuf request_;
7773
std::promise<response> response_promise_;
@@ -84,10 +80,10 @@ namespace network {
8480

8581
client::impl::impl(client_options options)
8682
: options_(options)
87-
, sentinel_(new boost::asio::io_service::work(io_service_))
88-
, lifetime_thread_([=] () { io_service_.run(); })
8983
, resolver_(io_service_, options.cache_resolved())
90-
, connection_(io_service_) {
84+
, connection_(io_service_)
85+
, sentinel_(new boost::asio::io_service::work(io_service_))
86+
, lifetime_thread_([=] () { io_service_.run(); }) {
9187

9288
}
9389

@@ -96,30 +92,32 @@ namespace network {
9692
lifetime_thread_.join();
9793
}
9894

99-
void client::impl::connect(client::string_type host,
100-
const boost::system::error_code &ec,
95+
void client::impl::connect(const boost::system::error_code &ec,
10196
tcp::resolver::iterator endpoint_iterator) {
102-
if (ec) {
103-
return;
104-
}
105-
106-
if (endpoint_iterator == tcp::resolver::iterator()) {
107-
return;
108-
}
109-
11097
tcp::endpoint endpoint(*endpoint_iterator);
111-
connection_.async_connect(endpoint, host,
112-
[=] (const boost::system::error_code &ec) {
113-
write_request(ec);
114-
});
98+
std::cout << "Resolved " << endpoint << std::endl;
99+
//connection_.async_connect(endpoint,
100+
// [=] (const boost::system::error_code &ec) {
101+
// if (ec) {
102+
// return;
103+
// }
104+
//
105+
// std::cout << "Oh." << std::endl;
106+
// //response_promise_.set_value(v2::response());
107+
// //write_request(ec);
108+
// });
115109
}
116110

117111
void client::impl::write_request(const boost::system::error_code &ec) {
118112
if (ec) {
119113
return;
120114
}
121115

122-
// request!
116+
connection_.async_write(request_,
117+
[=] (const boost::system::error_code &ec,
118+
std::size_t bytes_written) {
119+
read_response_status(ec, bytes_written);
120+
});
123121
}
124122

125123
void client::impl::read_response_status(const boost::system::error_code &ec,
@@ -128,7 +126,13 @@ namespace network {
128126
return;
129127
}
130128

131-
// response
129+
connection_.async_read_until(response_,
130+
"\r\n",
131+
[=] (const boost::system::error_code &ec,
132+
std::size_t bytes_read) {
133+
// fill headers
134+
read_response_headers(ec, bytes_read);
135+
});
132136
}
133137

134138
void client::impl::read_response_headers(const boost::system::error_code &ec,
@@ -137,8 +141,13 @@ namespace network {
137141
return;
138142
}
139143

140-
// response
141-
}
144+
connection_.async_read_until(response_,
145+
"\r\n\r\n",
146+
[=] (const boost::system::error_code &ec,
147+
std::size_t bytes_read) {
148+
// um...
149+
});
150+
}
142151

143152
std::future<response> client::impl::do_request(method met,
144153
request req,
@@ -154,13 +163,33 @@ namespace network {
154163
return response;
155164
}
156165

157-
//
158-
159-
auto endpoints = resolver_.async_resolve(req.host(), req.port(),
160-
[=](const boost::system::error_code &ec,
161-
tcp::resolver::iterator endpoint_iterator) {
162-
this->connect(ec, endpoint_iterator);
163-
});
166+
uri_builder builder;
167+
for (auto header : req.headers()) {
168+
// boost::iequals(header.first, "host")
169+
if ((header.first == "Host") || (header.first == "host")) {
170+
builder
171+
.authority(header.second)
172+
;
173+
break;
174+
}
175+
}
176+
auto auth = builder.uri();
177+
auto host = auth.host()?
178+
uri::string_type(std::begin(*auth.host()), std::end(*auth.host())) : uri::string_type();
179+
auto port = auth.port<std::uint16_t>()? *auth.port<std::uint16_t>() : 80;
180+
181+
resolver_.async_resolve(host, port,
182+
[=](const boost::system::error_code &ec,
183+
tcp::resolver::iterator endpoint_iterator) {
184+
if (ec) {
185+
if (endpoint_iterator == tcp::resolver::iterator()) {
186+
return;
187+
}
188+
return;
189+
}
190+
191+
connect(ec, endpoint_iterator);
192+
});
164193

165194
return response;
166195
}

http/src/network/http/v2/client/connection/connection.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ namespace network {
5555
/**
5656
* \brief Asynchronously creates a connection to an endpoint.
5757
* \param endpoint The endpoint to which to connect.
58-
* \param host The host name.
5958
* \param callback A callback handler.
6059
*/
61-
virtual void async_connect(boost::asio::ip::tcp::endpoint &endpoint,
62-
const std::string &host, connect_callback callback) = 0;
60+
virtual void async_connect(const boost::asio::ip::tcp::endpoint &endpoint,
61+
connect_callback callback) = 0;
6362

6463
/**
6564
* \brief Asynchronously writes data across the connection.

http/src/network/http/v2/client/connection/normal_connection.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ namespace network {
3232

3333
}
3434

35-
virtual void async_connect(boost::asio::ip::tcp::endpoint &endpoint,
36-
const std::string &host,
35+
virtual void async_connect(const boost::asio::ip::tcp::endpoint &endpoint,
3736
connect_callback callback) {
38-
socket_.reset(new boost::asio::ip::tcp::socket(io_service_));
37+
using boost::asio::ip::tcp;
38+
std::cout << "Oh 1." << std::endl;
39+
socket_.reset(new tcp::socket{io_service_});
40+
std::cout << "Oh 2." << std::endl;
3941
socket_->async_connect(endpoint, callback);
42+
std::cout << "Oh 3." << std::endl;
4043
}
4144

4245
virtual void async_write(boost::asio::streambuf &command_streambuf,

http/src/network/http/v2/client/connection/ssl_connection.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ namespace network {
3737

3838
}
3939

40-
virtual void async_connect(boost::asio::ip::tcp::endpoint &endpoint,
41-
const std::string &host,
40+
virtual void async_connect(const boost::asio::ip::tcp::endpoint &endpoint,
4241
connect_callback callback) {
4342
context_.reset(new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
4443
std::vector<std::string> const& certificate_paths =
@@ -54,7 +53,7 @@ namespace network {
5453
context_->add_verify_path(path);
5554
}
5655
context_->set_verify_mode(boost::asio::ssl::context::verify_peer);
57-
context_->set_verify_callback(boost::asio::ssl::rfc2818_verification(host));
56+
//context_->set_verify_callback(boost::asio::ssl::rfc2818_verification(host));
5857
}
5958
else {
6059
context_->set_default_verify_paths();

http/src/network/http/v2/client/response.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ namespace network {
102102
swap(headers_, other.headers_);
103103
}
104104

105+
/**
106+
* \brief Returns the HTTP version.
107+
* \returns The HTTP version.
108+
*/
109+
string_type version() const {
110+
return version_;
111+
}
112+
105113
/**
106114
* \brief Returns the HTTP response status.
107115
* \returns The status code.
@@ -135,14 +143,6 @@ namespace network {
135143
// append_body
136144
// get_body
137145

138-
/**
139-
* \brief Returns the HTTP version.
140-
* \returns The HTTP version.
141-
*/
142-
string_type version() const {
143-
return version_;
144-
}
145-
146146
private:
147147

148148
string_type version_;

http/test/v2/features/client/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ set(CPP-NETLIB_CLIENT_TESTS
1414
#endif()
1515

1616
foreach(test ${CPP-NETLIB_CLIENT_TESTS})
17-
#if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
18-
# set_source_files_properties(${test}.cpp
17+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
18+
set_source_files_properties(${test}.cpp
19+
PROPERTIES COMPILE_FLAGS "-g")
1920
# PROPERTIES COMPILE_FLAGS "-Wall")
20-
#endif()
21+
endif()
2122

2223
add_executable(cpp-netlib-http-v2-${test} ${test}.cpp)
2324
target_link_libraries(cpp-netlib-http-v2-${test}

http/test/v2/features/client/client_test.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,32 @@ namespace http = network::http::v2;
1111

1212
Describe(http_client) {
1313

14+
void SetUp() {
15+
client_.reset(new http::client{});
16+
}
17+
18+
void TearDown() {
19+
20+
}
21+
1422
It(gets_a_response) {
15-
http::request request(network::uri("http://127.0.0.1/"));
16-
auto response = client_.get(request);
23+
http::request request;
24+
request
25+
.method(http::method::GET)
26+
.path("/LICENSE_1_0.txt")
27+
.version("1.0")
28+
.append_header("Host", "www.boost.org")
29+
.append_header("User-Agent", "cpp-netlib client_test")
30+
.append_header("Connection", "close");
31+
auto future_response = client_->get(request);
32+
auto response = future_response.get();
33+
34+
Assert::That(response.version(), Equals("1.1"));
35+
Assert::That(response.status(), Equals(http::status::code::OK));
36+
Assert::That(response.status_message(), Equals("OK"));
1737
}
1838

19-
http::client client_;
39+
std::unique_ptr<http::client> client_;
2040

2141
};
2242

http/test/v2/features/client/normal_connection_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace http = network::http::v2;
1717
Describe(normal_http_connection) {
1818

1919
void SetUp() {
20-
io_service_.reset(new boost::asio::io_service);
20+
io_service_.reset(new boost::asio::io_service{});
2121
resolver_.reset(new tcp::resolver(*io_service_));
2222
connection_.reset(new http::normal_connection(*io_service_));
2323
}
@@ -34,7 +34,7 @@ Describe(normal_http_connection) {
3434

3535
// Make sure that the connection is successful.
3636
tcp::endpoint endpoint(it->endpoint());
37-
connection_->async_connect(endpoint, "www.boost.org",
37+
connection_->async_connect(endpoint,
3838
[&ec] (const boost::system::error_code &ec_) {
3939
ec = ec_;
4040
});
@@ -49,7 +49,7 @@ Describe(normal_http_connection) {
4949
.path("/LICENSE_1_0.txt")
5050
.version("1.0")
5151
.append_header("Host", "www.boost.org")
52-
.append_header("User-Agent", "normal_connection_test")
52+
.append_header("User-Agent", "cpp-netlib normal_connection_test")
5353
.append_header("Connection", "close");
5454

5555
// Write the HTTP request to the socket, sending it to the server.

0 commit comments

Comments
 (0)