File: header_test.cpp

package info (click to toggle)
net-cpp 2.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 892 kB
  • sloc: cpp: 3,643; ansic: 185; makefile: 24
file content (99 lines) | stat: -rw-r--r-- 2,861 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <core/net/http/header.h>

#include <gtest/gtest.h>

namespace http = core::net::http;

TEST(Header, canonicalizing_empty_string_does_not_throw)
{
    std::string key{};

    EXPECT_TRUE(key.empty());

    auto result = http::Header::canonicalize_key(key);

    EXPECT_TRUE(result.empty());
}

TEST(Header, canonicalizing_a_valid_key_works)
{
    std::string key{"accept-encoding"};

    auto result = http::Header::canonicalize_key(key);

    EXPECT_EQ("Accept-Encoding", result);
}

TEST(Header, canonicalizing_is_idempotent)
{
    std::string key{"Accept-Encoding"};

    auto result = http::Header::canonicalize_key(key);

    EXPECT_EQ("Accept-Encoding", result);
}

TEST(Header, canonicalizing_corrects_random_capitalization)
{
    std::string key{"aCcEpT-eNcOdInG"};

    auto result = http::Header::canonicalize_key(key);

    EXPECT_EQ("Accept-Encoding", result);
}

TEST(Header, adding_values_works_correctly)
{
    http::Header header;
    header.add("Accept-Encoding", "utf8");
    EXPECT_TRUE(header.has("Accept-Encoding"));
    EXPECT_TRUE(header.has("Accept-Encoding", "utf8"));
    header.add("Accept-Encoding", "utf16");
    EXPECT_TRUE(header.has("Accept-Encoding", "utf8"));
    EXPECT_TRUE(header.has("Accept-Encoding", "utf16"));
}

TEST(Header, removing_values_works_correctly)
{
    http::Header header;
    header.add("Accept-Encoding", "utf8");
    EXPECT_TRUE(header.has("Accept-Encoding"));
    EXPECT_TRUE(header.has("Accept-Encoding", "utf8"));
    header.remove("Accept-Encoding", "utf8");
    EXPECT_TRUE(header.has("Accept-Encoding"));
    EXPECT_FALSE(header.has("Accept-Encoding", "utf8"));
    header.remove("Accept-Encoding");
    EXPECT_FALSE(header.has("Accept-Encoding"));
}

TEST(Header, setting_values_works_correctly)
{
    http::Header header;
    header.add("Accept-Encoding", "utf8");

    EXPECT_TRUE(header.has("Accept-Encoding"));
    EXPECT_TRUE(header.has("Accept-Encoding", "utf8"));

    header.set("Accept-Encoding", "utf16");
    EXPECT_TRUE(header.has("Accept-Encoding"));
    EXPECT_FALSE(header.has("Accept-Encoding", "utf8"));
    EXPECT_TRUE(header.has("Accept-Encoding", "utf16"));
}