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 (86 lines) | stat: -rw-r--r-- 2,154 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
/*
	restinio
*/

/*!
	Socket options.
*/

#include <catch2/catch.hpp>

#include <restinio/all.hpp>

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

TEST_CASE( "Socket options" , "[socket][options]" )
{
	bool socket_options_setter_was_called = false;
	using http_server_t =
		restinio::http_server_t<
			restinio::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();

	std::string response;
	auto create_request = []( const std::string & body ){
		return
			"POST /data HTTP/1.0\r\n"
			"From: unit-test\r\n"
			"User-Agent: unit-test\r\n"
			"Content-Type: application/x-www-form-urlencoded\r\n"
			"Content-Length: " + std::to_string( body.size() ) + "\r\n"
			"Connection: close\r\n"
			"\r\n" +
			body;
	};

	{
		const std::string body = "01234567890123456789";
		REQUIRE_NOTHROW( response = do_request( create_request( body ) ) );

		REQUIRE_THAT(
			response,
			Catch::Matchers::Contains(
				"Content-Length: " + std::to_string( body.size() ) ) );
		REQUIRE_THAT( response, Catch::Matchers::EndsWith( body ) );

		REQUIRE( socket_options_setter_was_called );
	}

	other_thread.stop_and_join();
}