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