userver: userver/utest/simple_server.hpp Source File
Loading...
Searching...
No Matches
simple_server.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utest/simple_server.hpp
4/// @brief @copybrief utest::SimpleServer
5
6#include <cstdint>
7#include <functional>
8#include <initializer_list>
9#include <memory>
10#include <string>
11#include <vector>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utest {
16
17/// @ingroup userver_utest
18///
19/// Toy server for simple network testing.
20///
21/// In constructor opens specified ports in localhost address and listens on
22/// them. On each accepted data packet calls user callback.
23///
24/// ## Example usage:
25/// @snippet testing_test.cpp Sample SimpleServer usage
26class SimpleServer final {
27 public:
28 struct Response {
29 enum Commands {
30 kWriteAndClose,
31 kTryReadMore,
32 kWriteAndContinue,
33 };
34
35 std::string data_to_send{};
36 Commands command{kWriteAndClose};
37 };
38
39 using Request = std::string;
40 using OnRequest = std::function<Response(const Request&)>;
41
42 using Port = unsigned short;
43 enum Protocol { kTcpIpV4, kTcpIpV6 };
44
45 SimpleServer(OnRequest callback, Protocol protocol = kTcpIpV4);
46 ~SimpleServer();
47
48 Port GetPort() const;
49
50 enum class Schema {
51 kHttp,
52 kHttps,
53 };
54
55 std::string GetBaseUrl(Schema type = Schema::kHttp) const;
56
57 std::uint64_t GetConnectionsOpenedCount() const;
58
59 private:
60 class Impl;
61 const std::unique_ptr<Impl> pimpl_;
62};
63
64} // namespace utest
65
66USERVER_NAMESPACE_END