userver: utest::SimpleServer Class Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts

Your opinion will help to improve our service

Leave a feedback >

#include <userver/utest/simple_server.hpp>

Detailed Description

Toy server for simple network testing; for testing HTTP prefer using utest::HttpServerMock.

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());
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
 Response to return from the OnRequest callback. More...
 

Public Types

enum  Protocol {
  kTcpIpV4 ,
  kTcpIpV6
}
 
enum class  Schema {
  kHttp ,
  kHttps
}
 
using Request = std::string
 Request that is passed to the OnRequest callback.
 
using OnRequest = std::function<Response(const Request&)>
 Callback that is invoked on each network request.
 
using Port = unsigned short
 

Public Member Functions

 SimpleServer (OnRequest callback, Protocol protocol=kTcpIpV4)
 
Port GetPort () const
 Returns Port the server listens on.
 
std::string GetBaseUrl (Schema type=Schema::kHttp) const
 Returns URL to the server, for example 'http://127.0.0.1:8080'.
 
std::uint64_t GetConnectionsOpenedCount () const
 

Member Typedef Documentation

◆ OnRequest

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

Callback that is invoked on each network request.

Definition at line 46 of file simple_server.hpp.

◆ Port

using utest::SimpleServer::Port = unsigned short

Definition at line 48 of file simple_server.hpp.

◆ Request

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

Request that is passed to the OnRequest callback.

Definition at line 43 of file simple_server.hpp.

Member Enumeration Documentation

◆ Protocol

enum utest::SimpleServer::Protocol

Definition at line 49 of file simple_server.hpp.

◆ Schema

enum class utest::SimpleServer::Schema
strong

Definition at line 57 of file simple_server.hpp.


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