userver: utest::SimpleServer Class Reference
Loading...
Searching...
No Matches
utest::SimpleServer Class Referencefinal

#include <userver/utest/simple_server.hpp>

Detailed Description

Toy server for simple network testing.

In constructor opens specified ports in localhost address and listens on them. On each accepted data packet calls user callback.

Example usage:

namespace {
const std::string kOkRequest = "OK";
const std::string kOkResponse = "OK RESPONSE DATA";
SimpleServer::Response assert_received_ok(const SimpleServer::Request& r) {
EXPECT_EQ(r, kOkRequest) << "SimpleServer received: " << r;
return {kOkResponse, SimpleServer::Response::kWriteAndClose};
}
} // namespace
UTEST(SimpleServer, ExampleTcpIpV4) {
SimpleServer s(assert_received_ok);
// ... invoke code that sends "OK" to localhost
addr.SetPort(s.GetPort());
worksock.Connect(addr,
ASSERT_EQ(kOkRequest.size(),
worksock.SendAll(kOkRequest.data(), kOkRequest.size(), {}));
std::string response;
response.resize(100);
const auto size = worksock.RecvAll(response.data(), response.size(), {});
response.resize(size);
EXPECT_EQ(response, kOkResponse) << "Received " << response;
}

Definition at line 26 of file simple_server.hpp.

Classes

struct  Response
 

Public Types

enum  Protocol {
  kTcpIpV4 ,
  kTcpIpV6
}
 
enum class  Schema {
  kHttp ,
  kHttps
}
 
using Request = std::string
 
using OnRequest = std::function<Response(const Request&)>
 
using Port = unsigned short
 

Public Member Functions

 SimpleServer (OnRequest callback, Protocol protocol=kTcpIpV4)
 
Port GetPort () const
 
std::string GetBaseUrl (Schema type=Schema::kHttp) const
 
std::uint64_t GetConnectionsOpenedCount () const
 

Member Typedef Documentation

◆ OnRequest

using utest::SimpleServer::OnRequest = std::function<Response(const Request&)>

Definition at line 40 of file simple_server.hpp.

◆ Port

using utest::SimpleServer::Port = unsigned short

Definition at line 42 of file simple_server.hpp.

◆ Request

using utest::SimpleServer::Request = std::string

Definition at line 39 of file simple_server.hpp.

Member Enumeration Documentation

◆ Protocol

enum utest::SimpleServer::Protocol

Definition at line 43 of file simple_server.hpp.

◆ Schema

enum class utest::SimpleServer::Schema
strong

Definition at line 50 of file simple_server.hpp.


The documentation for this class was generated from the following file: