|
3 | 3 | // (See accompanying file LICENSE_1_0.txt or copy at
|
4 | 4 | // http://www.boost.org/LICENSE_1_0.txt)
|
5 | 5 |
|
6 |
| -#include <igloo/igloo.h> |
| 6 | +#include <memory> |
| 7 | +#include <thread> |
| 8 | +#include <igloo/igloo_alt.h> |
7 | 9 | #include <boost/asio.hpp>
|
8 | 10 | #include "network/http/v2/client/connection/ssl_connection_delegate.hpp"
|
| 11 | +#include "network/http/v2/client/request.hpp" |
9 | 12 |
|
10 | 13 | using namespace igloo;
|
11 | 14 | using boost::asio::ip::tcp;
|
12 | 15 | namespace http = network::http::v2;
|
13 | 16 |
|
| 17 | +Describe(https_connection) { |
| 18 | + |
| 19 | + void SetUp() { |
| 20 | + io_service_.reset(new boost::asio::io_service); |
| 21 | + resolver_.reset(new tcp::resolver(*io_service_)); |
| 22 | + options_.reset(new http::client_options); |
| 23 | + connection_.reset(new http::ssl_connection_delegate(*io_service_, *options_)); |
| 24 | + socket_.reset(new tcp::socket(*io_service_)); |
| 25 | + } |
| 26 | + |
| 27 | + void TearDown() { |
| 28 | + socket_->close(); |
| 29 | + } |
| 30 | + |
| 31 | + It(connects_to_localhost) { |
| 32 | + // Resolve the host. |
| 33 | + boost::system::error_code ec; |
| 34 | + tcp::resolver::query query("127.0.0.1", "80"); |
| 35 | + auto it = resolver_->resolve(query, ec); |
| 36 | + Assert::That(ec, Equals(boost::system::error_code())); |
| 37 | + |
| 38 | + // Make sure that the connection is successful. |
| 39 | + tcp::endpoint endpoint(it->endpoint()); |
| 40 | + connection_->async_connect(endpoint, "127.0.0.1", |
| 41 | + [&ec] (const boost::system::error_code &ec_) { |
| 42 | + ec = ec_; |
| 43 | + }); |
| 44 | + io_service_->run_one(); |
| 45 | + Assert::That(ec, Equals(boost::system::error_code())); |
| 46 | + } |
| 47 | + |
| 48 | + /* |
| 49 | + It(writes_to_localhost) { |
| 50 | + // Resolve the host. |
| 51 | + boost::system::error_code ec; |
| 52 | + tcp::resolver::query query("127.0.0.1", "80"); |
| 53 | + auto it = resolver_->resolve(query, ec); |
| 54 | + Assert::That(ec, Equals(boost::system::error_code())); |
| 55 | +
|
| 56 | + // Make sure that the connection is successful. |
| 57 | + tcp::endpoint endpoint(it->endpoint()); |
| 58 | + connection_->async_connect(endpoint, "127.0.0.1", |
| 59 | + [&ec] (const boost::system::error_code &ec_) { |
| 60 | + Assert::That(ec_, Equals(boost::system::error_code())); |
| 61 | + }); |
| 62 | +
|
| 63 | + // Create an HTTPS request. |
| 64 | + http::request request{network::uri{"http://127.0.0.1/"}}; |
| 65 | + request.set_method(http::method::GET); |
| 66 | + request.set_version("1.0"); |
| 67 | + request.append_header("User-Agent", "ssl_connection_test"); |
| 68 | + request.append_header("Connection", "close"); |
| 69 | +
|
| 70 | + // Write the HTTP request to the socket, sending it to the server. |
| 71 | + boost::asio::streambuf request_; |
| 72 | + std::ostream request_stream(&request_); |
| 73 | + request_stream << request; |
| 74 | + std::size_t bytes_written = 0; |
| 75 | + connection_->async_write(request_, |
| 76 | + [&bytes_written] (const boost::system::error_code &ec_, |
| 77 | + std::size_t bytes_written_) { |
| 78 | + Assert::That(ec_, Equals(boost::system::error_code())); |
| 79 | + bytes_written = bytes_written_; |
| 80 | + }); |
| 81 | + io_service_->run(); |
| 82 | + Assert::That(bytes_written, IsGreaterThan(0)); |
| 83 | + } |
| 84 | +
|
| 85 | + It(reads_from_localhost) { |
| 86 | + // Resolve the host. |
| 87 | + boost::system::error_code ec; |
| 88 | + tcp::resolver::query query("127.0.0.1", "80"); |
| 89 | + auto it = resolver_->resolve(query, ec); |
| 90 | + Assert::That(ec, Equals(boost::system::error_code())); |
| 91 | +
|
| 92 | + // Make sure that the connection is successful. |
| 93 | + tcp::endpoint endpoint(it->endpoint()); |
| 94 | + connection_->async_connect(endpoint, "127.0.0.1", |
| 95 | + [] (const boost::system::error_code &ec_) { |
| 96 | + Assert::That(ec_, Equals(boost::system::error_code())); |
| 97 | + }); |
| 98 | +
|
| 99 | + // Create an HTTP request. |
| 100 | + http::request request{network::uri{"http://127.0.0.1/"}}; |
| 101 | + request.set_method(http::method::GET); |
| 102 | + request.set_version("1.0"); |
| 103 | + request.append_header("User-Agent", "ssl_connection_test"); |
| 104 | + request.append_header("Connection", "close"); |
| 105 | +
|
| 106 | + // Write the HTTP request to the socket, sending it to the server. |
| 107 | + boost::asio::streambuf request_; |
| 108 | + std::ostream request_stream(&request_); |
| 109 | + request_stream << request; |
| 110 | + std::size_t bytes_written = 0; |
| 111 | + connection_->async_write(request_, |
| 112 | + [&bytes_written] (const boost::system::error_code &ec_, |
| 113 | + std::size_t bytes_written_) { |
| 114 | + Assert::That(ec_, Equals(boost::system::error_code())); |
| 115 | + bytes_written = bytes_written_; |
| 116 | + }); |
| 117 | +
|
| 118 | + // Read the HTTP response on the socket from the server. |
| 119 | + char output[8192]; |
| 120 | + std::memset(output, 0, sizeof(output)); |
| 121 | + std::size_t bytes_read = 0; |
| 122 | + connection_->async_read_some(boost::asio::mutable_buffers_1(output, sizeof(output)), |
| 123 | + [&bytes_read] (const boost::system::error_code &ec_, |
| 124 | + std::size_t bytes_read_) { |
| 125 | + Assert::That(ec_, Equals(boost::system::error_code())); |
| 126 | + bytes_read = bytes_read_; |
| 127 | + }); |
| 128 | +
|
| 129 | + io_service_->run(); |
| 130 | + Assert::That(bytes_read, IsGreaterThan(0)); |
| 131 | + } |
| 132 | + */ |
| 133 | + |
| 134 | + std::unique_ptr<boost::asio::io_service> io_service_; |
| 135 | + std::unique_ptr<tcp::resolver> resolver_; |
| 136 | + std::unique_ptr<http::client_options> options_; |
| 137 | + std::unique_ptr<http::connection_delegate> connection_; |
| 138 | + std::unique_ptr<tcp::socket> socket_; |
| 139 | + |
| 140 | +}; |
| 141 | + |
14 | 142 | int
|
15 | 143 | main(int argc, char *argv[]) {
|
16 | 144 | return TestRunner::RunAllTests(argc, const_cast<const char **>(argv));
|
|
0 commit comments