srctree

Robin Linden parent 0392fba8 cd3b43df
protocol: Support creating Headers from init-lists

This makes e.g. tests more ergonomic.

inlinesplit
protocol/response.h added: 16, removed: 3, total 13
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 Mikael Larsson <c.mikael.larsson@gmail.com>
//
// SPDX-License-Identifier: BSD-2-Clause
@@ -7,6 +7,7 @@
#define PROTOCOL_RESPONSE_H_
 
#include <cstddef>
#include <initializer_list>
#include <map>
#include <optional>
#include <string>
@@ -32,6 +33,9 @@ struct StatusLine {
 
class Headers {
public:
Headers() = default;
Headers(std::initializer_list<std::map<std::string, std::string>::value_type> init) : headers_{std::move(init)} {}
 
void add(std::pair<std::string_view, std::string_view> nv);
[[nodiscard]] std::optional<std::string_view> get(std::string_view name) const;
[[nodiscard]] std::string to_string() const;
 
protocol/response_test.cpp added: 16, removed: 3, total 13
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2021-2022 Mikael Larsson <c.mikael.larsson@gmail.com>
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -6,6 +7,7 @@
 
#include "etest/etest.h"
 
#include <cstddef>
#include <string_view>
 
using namespace std::string_view_literals;
@@ -27,5 +29,12 @@ int main() {
expect_eq(headers.get("cOnTeNt-TyPe"sv).value(), "text/html");
});
 
etest::test("headers, init-list", [] {
protocol::Headers headers{{"Content-Type", "text/html"}};
expect_eq(headers.size(), std::size_t{1});
expect_eq(headers.get("CONTENT-TYPE"sv).value(), "text/html");
expect_eq(headers.get("cOnTeNt-TyPe"sv).value(), "text/html");
});
 
return etest::run_all_tests();
}