|
| 1 | +/* |
| 2 | + * Copyright 2022 Robert Bosch GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | +/** |
| 19 | + * \file oak/server_test.cpp |
| 20 | + * \see oak/server_test.hpp |
| 21 | + */ |
| 22 | + |
| 23 | +#include <gtest/gtest.h> // for TEST, EXPECT_TRUE, ... |
| 24 | + |
| 25 | +#include <iostream> |
| 26 | +#include <map> // for map<> |
| 27 | +#include <string> // for string |
| 28 | +#include <utility> // for tie |
| 29 | +#include <vector> // for vector<> |
| 30 | + |
| 31 | +#include <array> |
| 32 | +#include <cstdio> |
| 33 | +#include <iostream> |
| 34 | +#include <memory> |
| 35 | +#include <stdexcept> |
| 36 | +#include <string> |
| 37 | + |
| 38 | +#include <cloe/registrar.hpp> |
| 39 | +#include <fable/utility/gtest.hpp> |
| 40 | + |
| 41 | +#include "oak/registrar.hpp" // for Registrar |
| 42 | +#include "oak/server.hpp" // for Server |
| 43 | +#include "oak/curl.hpp" // for Curl |
| 44 | + |
| 45 | +using namespace std; // NOLINT(build/namespaces) |
| 46 | + |
| 47 | +/** |
| 48 | + * Executes a (shell) command and return the StdOut |
| 49 | + */ |
| 50 | +std::string exec(const char* cmd) { |
| 51 | + std::cerr << "Exec: " << cmd << std::endl; |
| 52 | + |
| 53 | + std::array<char, 128> buffer; |
| 54 | + std::string result; |
| 55 | + std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose); |
| 56 | + if (!pipe) { |
| 57 | + throw std::runtime_error("popen() failed!"); |
| 58 | + } |
| 59 | + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { |
| 60 | + result += buffer.data(); |
| 61 | + } |
| 62 | + return result; |
| 63 | +} |
| 64 | + |
| 65 | +std::string exec(const std::string& cmd) { return exec(cmd.c_str()); } |
| 66 | +std::string exec(const oak::Curl& curl) { return exec(curl.to_string()); } |
| 67 | + |
| 68 | +std::string address; |
| 69 | +std::atomic<unsigned short> port; |
| 70 | + |
| 71 | +class Environment : public ::testing::Environment { |
| 72 | + public: |
| 73 | + ~Environment() override {} |
| 74 | + void SetUp() override { |
| 75 | + address = "127.0.0.1"; |
| 76 | + port = 12340; |
| 77 | + } |
| 78 | + void TearDown() override {} |
| 79 | +}; |
| 80 | +testing::Environment* const foo_env = testing::AddGlobalTestEnvironment(new Environment); |
| 81 | + |
| 82 | +std::unique_ptr<oak::Server> create_server() { |
| 83 | + int retry = 0; |
| 84 | + do { |
| 85 | + try { |
| 86 | + auto port_ = port++; |
| 87 | + std::cout << "Trying " << address << ":" << port_ << std::endl; |
| 88 | + auto server = std::make_unique<oak::Server>(address, port_); |
| 89 | + server->listen(); |
| 90 | + // if server is listening, return otherwise retry |
| 91 | + if (server->is_listening()) { |
| 92 | + return server; |
| 93 | + } else { |
| 94 | + retry++; |
| 95 | + } |
| 96 | + } catch (...) { |
| 97 | + EXPECT_TRUE(false) << "Unexpected exception"; |
| 98 | + } |
| 99 | + } while (retry < 100); |
| 100 | + |
| 101 | + return nullptr; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Try to create a server. |
| 106 | + */ |
| 107 | +TEST(oak_server, listen) { auto server = create_server(); } |
| 108 | + |
| 109 | +/** |
| 110 | + * Test that stopping a not-listening server results in an exception. |
| 111 | + */ |
| 112 | +TEST(oak_server, fail_not_listening) { |
| 113 | + oak::Server server; |
| 114 | + EXPECT_THROW(server.stop(), std::runtime_error); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Try to GET a non-existing endpoint. |
| 119 | + */ |
| 120 | +TEST(oak_server, get_nohandler) { |
| 121 | + auto server = create_server(); |
| 122 | + ASSERT_NE(server, nullptr); |
| 123 | + auto address_ = server->address(); |
| 124 | + auto port_ = server->port(); |
| 125 | + |
| 126 | + auto result = exec(oak::Curl::get(address_, port_, "test")); |
| 127 | + |
| 128 | + // Compare result |
| 129 | + fable::assert_eq(fable::parse_json(result.c_str()), R"({ |
| 130 | + "endpoints": [], |
| 131 | + "error": "cannot find handler" |
| 132 | + })"); |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Try to GET an endpoint. |
| 137 | + */ |
| 138 | +TEST(oak_server, get_handler) { |
| 139 | + auto server = create_server(); |
| 140 | + ASSERT_NE(server, nullptr); |
| 141 | + auto address_ = server->address(); |
| 142 | + auto port_ = server->port(); |
| 143 | + |
| 144 | + // Create registrar |
| 145 | + oak::StaticRegistrar registrar(server.get(), "", nullptr); |
| 146 | + // Register one pseudo-endpoint |
| 147 | + std::vector<std::string> data{ "1", "2", "3" }; |
| 148 | + registrar.register_handler("/simulators", cloe::handler::StaticJson(data)); |
| 149 | + |
| 150 | + // Read the pseudo-endpoint |
| 151 | + auto result = exec(oak::Curl::get(address_, port_, "simulators")); |
| 152 | + |
| 153 | + // Compare result |
| 154 | + fable::assert_eq(fable::parse_json(result.c_str()), R"([ |
| 155 | + "1", |
| 156 | + "2", |
| 157 | + "3" |
| 158 | + ])"); |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * @brief Tries to POST to an endpoint which echos the data back |
| 163 | + */ |
| 164 | +TEST(oak_server, post_handler) { |
| 165 | + auto server = create_server(); |
| 166 | + ASSERT_NE(server, nullptr); |
| 167 | + auto address_ = server->address(); |
| 168 | + auto port_ = server->port(); |
| 169 | + |
| 170 | + // Create registrar |
| 171 | + oak::StaticRegistrar registrar(server.get(), "", nullptr); |
| 172 | + // Register one pseudo-endpoint |
| 173 | + std::vector<std::string> data{ |
| 174 | + "1", |
| 175 | + "2", |
| 176 | + "3", |
| 177 | + }; |
| 178 | + registrar.register_handler( |
| 179 | + "/echo", [this](const cloe::Request& request, cloe::Response& response) { |
| 180 | + switch (request.method()) { |
| 181 | + case cloe::RequestMethod::POST: { |
| 182 | + try { |
| 183 | + const auto json = request.as_json(); |
| 184 | + response.write(json); |
| 185 | + response.set_status(cloe::StatusCode::OK); |
| 186 | + } catch (const std::exception& ex) { |
| 187 | + response.bad_request(cloe::Json{ |
| 188 | + {"error", ex.what()}, |
| 189 | + }); |
| 190 | + } |
| 191 | + break; |
| 192 | + } |
| 193 | + default: { |
| 194 | + response.not_allowed(cloe::RequestMethod::POST, |
| 195 | + cloe::Json{ |
| 196 | + {"error", "only GET or POST method allowed"}, |
| 197 | + }); |
| 198 | + } |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | + // Read the pseudo-endpoint |
| 203 | + auto result = exec(oak::Curl::post(address_, port_, "echo", R"({"a": "b", "c": "d"})", "application/json")); |
| 204 | + |
| 205 | + // Compare result |
| 206 | + fable::assert_eq(fable::parse_json(result.c_str()), R"({ |
| 207 | + "a": "b", |
| 208 | + "c": "d" |
| 209 | + })"); |
| 210 | +} |
0 commit comments