MySQL 8.4.2
Source Code Documentation
mock_server_rest_client.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQLROUTER_MOCK_SERVER_REST_CLIENT_INCLUDED
27#define MYSQLROUTER_MOCK_SERVER_REST_CLIENT_INCLUDED
28
29#include <chrono>
30#include <string>
31
32/** @brief URI for the mock server globals */
33static constexpr const char kMockServerGlobalsRestUri[] =
34 "/api/v1/mock_server/globals/";
35
36/** @class MockServerHttpClient
37 *
38 * Allows communicating with mysql server mock via
39 * HTTP port
40 *
41 **/
43 public:
44 /** @brief Constructor
45 *
46 * @param http_port port on which http server handles the http requests
47 * @param http_hostname hostname of the http server that handles the http
48 * requests
49 */
50 MockServerRestClient(const uint16_t http_port,
51 const std::string &http_hostname = "127.0.0.1");
52
53 /** @brief Sets values of the all globals in the server mock via
54 * http interface.
55 * Example:
56 * set_globals("{\"secondary_removed\": true}");
57 *
58 * @param globals_json json string with the globals names and values to set
59 */
60 void set_globals(const std::string &globals_json);
61
62 /** @brief Gets all the mock server globals as a json string
63 */
64 std::string get_globals_as_json_string();
65
66 /** @brief Gets a selected mock server int global value
67 *
68 * @param global_name name of the global
69 */
70 int get_int_global(const std::string &global_name);
71
72 /** @brief Gets a selected mock server bool global value
73 *
74 * @param global_name name of the global
75 */
76 bool get_bool_global(const std::string &global_name);
77
78 /** @brief Sends Delete request to the mock server
79 * on the selected URI
80 *
81 * @param uri uri for the Delete request
82 */
83 void send_delete(const std::string &uri);
84
85 /**
86 * @brief Wait until a REST endpoint returns !404.
87 *
88 * at mock startup the socket starts to listen before the REST endpoint gets
89 * registered. As long as it returns 404 Not Found we should wait and retry.
90 *
91 * @param max_wait_time max time to wait for endpoint being ready
92 * @returns true once endpoint doesn't return 404 anymore, fails otherwise
93 */
95 std::chrono::milliseconds max_wait_time =
97
98 private:
99 static constexpr std::chrono::milliseconds kMockServerMaxRestEndpointStepTime{
100 100};
101 static constexpr std::chrono::milliseconds
103 const std::string http_hostname_;
104 const uint16_t http_port_;
105};
106
107#endif // MYSQLROUTER_MOCK_SERVER_REST_CLIENT_INCLUDED
Definition: mock_server_rest_client.h:42
bool get_bool_global(const std::string &global_name)
Gets a selected mock server bool global value.
Definition: mock_server_rest_client.cc:132
std::string get_globals_as_json_string()
Gets all the mock server globals as a json string.
Definition: mock_server_rest_client.cc:77
MockServerRestClient(const uint16_t http_port, const std::string &http_hostname="127.0.0.1")
Constructor.
Definition: mock_server_rest_client.cc:41
static constexpr std::chrono::milliseconds kMockServerMaxRestEndpointStepTime
Definition: mock_server_rest_client.h:99
const uint16_t http_port_
Definition: mock_server_rest_client.h:104
void send_delete(const std::string &uri)
Sends Delete request to the mock server on the selected URI.
Definition: mock_server_rest_client.cc:153
int get_int_global(const std::string &global_name)
Gets a selected mock server int global value.
Definition: mock_server_rest_client.cc:111
static constexpr std::chrono::milliseconds kMockServerDefaultRestEndpointTimeout
Definition: mock_server_rest_client.h:102
void set_globals(const std::string &globals_json)
Sets values of the all globals in the server mock via http interface.
Definition: mock_server_rest_client.cc:45
bool wait_for_rest_endpoint_ready(std::chrono::milliseconds max_wait_time=kMockServerDefaultRestEndpointTimeout) const noexcept
Wait until a REST endpoint returns !404.
Definition: mock_server_rest_client.cc:178
const std::string http_hostname_
Definition: mock_server_rest_client.h:103
static constexpr const char kMockServerGlobalsRestUri[]
URI for the mock server globals.
Definition: mock_server_rest_client.h:33