httpmockserver
mock_server.h
Go to the documentation of this file.
1 
12 #pragma once
13 
14 #include <string>
15 #include <vector>
16 #include <memory>
17 
18 
19 namespace httpmock {
20 
21 
22 class IMockServer {
23  public:
24  virtual ~IMockServer() = 0;
25 
27  virtual void start() = 0;
28 
30  virtual void stop() = 0;
31 
33  virtual bool isRunning() const = 0;
34 };
35 
36 
37 class MockServer: public IMockServer {
38  class Server;
40  std::unique_ptr<Server> server;
42  int port;
43  private:
44  MockServer(const MockServer &);
46  // Move is not allowed as a callback is registered for moved object
47  // address in the internal daemon...
49  public:
51  explicit MockServer(int port = 8080);
52  virtual ~MockServer();
53 
55  virtual void start() override;
57  virtual void stop() override;
58 
60  virtual bool isRunning() const override;
61 
63  int getPort() const;
64  protected:
66  struct KeyValue {
67  std::string key;
68  std::string value;
69 
70  explicit KeyValue(const std::string &key, const std::string &value = "")
71  : key(key), value(value)
72  {}
73  };
74  struct Header: public KeyValue {
75  Header(const std::string &key, const std::string &value = "")
76  : KeyValue(key, value)
77  {}
78  };
80  struct UrlArg: public KeyValue {
82  bool hasValue;
83 
84  explicit UrlArg(const std::string &key)
85  : KeyValue(key), hasValue(false)
86  {}
87  UrlArg(const std::string &key, const std::string &value)
88  : KeyValue(key, value), hasValue(true)
89  {}
90  };
91 
93  struct Response {
94  int status;
95  std::string body;
96  std::vector<Header> headers;
97 
98  explicit Response(int status = 200, const std::string &body = "OK")
99  : status(status), body(body), headers()
100  {}
101 
103  Response &addHeader(const Header &h) {
104  headers.push_back(h);
105  return *this;
106  }
107  };
108  private:
117  virtual Response responseHandler(
118  const std::string &url,
119  const std::string &method,
120  const std::string &data,
121  const std::vector<UrlArg> &urlArguments = {},
122  const std::vector<Header> &headers = {}) = 0;
123 };
124 
125 
126 } // namespace httpmock
httpmock::IMockServer
Definition: mock_server.h:22
httpmock::MockServer::~MockServer
virtual ~MockServer()
Definition: mock_server.cc:109
httpmock::MockServer::KeyValue::KeyValue
KeyValue(const std::string &key, const std::string &value="")
Definition: mock_server.h:70
httpmock::MockServer::Response::headers
std::vector< Header > headers
Response headers.
Definition: mock_server.h:96
httpmock::MockServer::server
std::unique_ptr< Server > server
Server implementation.
Definition: mock_server.h:38
httpmock::MockServer::isRunning
virtual bool isRunning() const override
Return true if server is running.
Definition: mock_server.cc:124
httpmock::MockServer::UrlArg::UrlArg
UrlArg(const std::string &key, const std::string &value)
Definition: mock_server.h:87
httpmock::MockServer
Definition: mock_server.h:37
httpmock::MockServer::Response
Response object.
Definition: mock_server.h:93
httpmock
Definition: mock_server.cc:20
httpmock::MockServer::Response::body
std::string body
Body sent to the client.
Definition: mock_server.h:95
httpmock::MockServer::Response::Response
Response(int status=200, const std::string &body="OK")
Definition: mock_server.h:98
httpmock::MockServer::Header
Definition: mock_server.h:74
httpmock::MockServer::stop
virtual void stop() override
Stops the server.
Definition: mock_server.cc:119
httpmock::IMockServer::isRunning
virtual bool isRunning() const =0
Return true if server is running.
httpmock::MockServer::Server
Definition: mock_server.cc:26
httpmock::MockServer::Response::addHeader
Response & addHeader(const Header &h)
Add header to the response.
Definition: mock_server.h:103
httpmock::IMockServer::~IMockServer
virtual ~IMockServer()=0
httpmock::MockServer::UrlArg
Url argument (with optional value)
Definition: mock_server.h:80
httpmock::MockServer::port
int port
Port to run server on.
Definition: mock_server.h:42
httpmock::MockServer::MockServer
MockServer(const MockServer &)
httpmock::MockServer::UrlArg::UrlArg
UrlArg(const std::string &key)
Definition: mock_server.h:84
httpmock::IMockServer::start
virtual void start()=0
Allow to start the server.
httpmock::MockServer::Header::Header
Header(const std::string &key, const std::string &value="")
Definition: mock_server.h:75
httpmock::MockServer::start
virtual void start() override
Starts the server.
Definition: mock_server.cc:114
httpmock::MockServer::getPort
int getPort() const
Return port number server is running on.
Definition: mock_server.cc:129
httpmock::MockServer::Response::status
int status
Status code returned to the client.
Definition: mock_server.h:94
httpmock::MockServer::UrlArg::hasValue
bool hasValue
Whether the value field has been set.
Definition: mock_server.h:82
httpmock::IMockServer::stop
virtual void stop()=0
Allow to stop the server.
httpmock::MockServer::KeyValue
Key-Value storage.
Definition: mock_server.h:66
httpmock::MockServer::responseHandler
virtual Response responseHandler(const std::string &url, const std::string &method, const std::string &data, const std::vector< UrlArg > &urlArguments={}, const std::vector< Header > &headers={})=0
httpmock::MockServer::KeyValue::key
std::string key
Definition: mock_server.h:67
httpmock::MockServer::KeyValue::value
std::string value
Definition: mock_server.h:68
httpmock::MockServer::operator=
MockServer & operator=(const MockServer &)