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

#include <userver/utest/http_server_mock.hpp>

Detailed Description

Toy HTTP server for testing; for raw TCP or TLS testing use utest::SimpleServer.

In constructor opens specified ports in localhost address and listens on them. On each HTTP request calls user callback.

Example usage:

constexpr http::headers::PredefinedHeader kHeader{"HeaderName"};
EXPECT_EQ(clients::http::HttpMethod::kPost, request.method);
EXPECT_EQ("/path", request.path);
EXPECT_EQ("value1", request.headers.at(std::string_view{"header_a"}));
EXPECT_EQ("body", request.body);
EXPECT_EQ((std::multimap<std::string, std::string>{{"arg1", "val1"}}), request.query);
response.response_status = 200;
response.headers[kHeader] = "HeaderValue";
response.body = "returned from mock body";
return response;
});
// Making a request to the `mock`
auto http_client_ptr = utest::CreateHttpClient();
auto response = http_client_ptr->CreateRequest()
.post(mock.GetBaseUrl() + "/path?arg1=val1", "body")
.headers({std::make_pair("header_a", "value1")})
.retry(1)
.perform();
// Response from the `mock`
EXPECT_EQ(200, response->status_code());
EXPECT_EQ("returned from mock body", response->body());
EXPECT_EQ("HeaderValue", response->headers()[kHeader]);

Definition at line 27 of file http_server_mock.hpp.

Classes

struct  HttpRequest
 Structure with HTTP request that is passed to the HttpHandler callback. More...
 
struct  HttpResponse
 Structure with HTTP response to return from the HttpHandler callback. More...
 

Public Types

using HttpHandler = std::function<HttpResponse(const HttpRequest&)>
 Callback that is invoked on each HTTP request.
 

Public Member Functions

 HttpServerMock (HttpHandler http_handler, SimpleServer::Protocol protocol=SimpleServer::kTcpIpV4)
 
std::string GetBaseUrl () const
 Returns URL to the server, for example 'http://127.0.0.1:8080'.
 
std::uint64_t GetConnectionsOpenedCount () const
 

Member Typedef Documentation

◆ HttpHandler

Callback that is invoked on each HTTP request.

Definition at line 47 of file http_server_mock.hpp.

Friends And Related Symbol Documentation

◆ HttpConnection

friend class HttpConnection
friend

Definition at line 57 of file http_server_mock.hpp.


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