File: main.cpp

package info (click to toggle)
restinio 0.6.19%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,184 kB
  • sloc: cpp: 110,712; ruby: 2,612; makefile: 22; sh: 21
file content (88 lines) | stat: -rw-r--r-- 2,384 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
	restinio
*/

/*!
	Socket options tls.
*/

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

#include <restinio/all.hpp>
#include <restinio/tls.hpp>

#include <test/common/utest_logger.hpp>
#include <test/common/pub.hpp>

TEST_CASE( "Socket_options TLS" , "[socket][options][tls]" )
{
	bool socket_options_setter_was_called = false;
	using http_server_t =
		restinio::http_server_t<
			restinio::tls_traits_t<
				restinio::asio_timer_manager_t,
				utest_logger_t > >;

	http_server_t http_server{
		restinio::own_io_context(),
		[&socket_options_setter_was_called]( auto & settings ){
			settings
				.port( utest_default_port() )
				.address( "127.0.0.1" )
				.request_handler(
					[]( auto req ){
						if( restinio::http_method_post() == req->header().method() )
						{
							req->create_response()
								.append_header( "Server", "RESTinio utest server" )
								.append_header_date_field()
								.append_header( "Content-Type", "text/plain; charset=utf-8" )
								.set_body( req->body() )
								.done();
							return restinio::request_accepted();
						}

						return restinio::request_rejected();
					} )
				.socket_options_setter(
					[&socket_options_setter_was_called]( auto options ){
						restinio::asio_ns::ip::tcp::no_delay no_delay{ true };
						options.set_option( no_delay );
						socket_options_setter_was_called = true;
					} );
		}
	};

	other_work_thread_for_server_t<http_server_t> other_thread(http_server);
	other_thread.run();

	// TODO: when tls client will be available use it instead of the following code:
	do_with_socket(
		[]( auto & socket, auto &  ){
			// Ensure we get connected:
			std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );

			socket.shutdown( restinio::asio_ns::ip::tcp::socket::shutdown_both );
			socket.close(); // Close without doing anything.

			//! Ensure we closed.
			std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
		} );

	REQUIRE( socket_options_setter_was_called );

	other_thread.stop_and_join();
}

TEST_CASE( "Usage of asio_ssl_stream() method", "[socket][tls][asio_ssl_stream]" )
{
	restinio::asio_ns::io_context ctx;
	auto ssl_context = std::make_shared< restinio::asio_ns::ssl::context >(
			restinio::asio_ns::ssl::context::sslv23 );

	restinio::tls_socket_t socket{ ctx, ssl_context };

	REQUIRE( nullptr != socket.asio_ssl_stream().native_handle() );
}