-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheaders-tests.cpp
109 lines (84 loc) · 3.79 KB
/
headers-tests.cpp
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
100
101
102
103
104
105
106
107
108
109
/**
Copyright 2010-2019 Red Anchor Trading Co. Ltd.
Distributed under the Boost Software License, Version 1.0.
See <http://www.boost.org/LICENSE_1_0.txt>
*/
#include "fost-inet-test.hpp"
#include <fost/mime.hpp>
using namespace fostlib;
FSL_TEST_SUITE(headers);
FSL_TEST_FUNCTION(ostream) {
// Use a std::stringstream here because all of these MIME types need to be
// printable on a narrow character stream as that is what all network
// connections are.
std::stringstream ss;
fostlib::headers_base::content c1, c2("applicaton/long_mime_type");
c2.subvalue(
"long-sub-key1", "long key value with long value and a long value");
c2.subvalue(
"long-sub-key2", "long key value with long value and a long value");
ss << c1 << "\n" << c2;
}
FSL_TEST_FUNCTION(parse) {
fostlib::mime::mime_headers h1, h2, h3;
h1.parse("Header-Value: One Two");
FSL_CHECK(h1.exists("header-value"));
FSL_CHECK_EQ(h1["Header-Value"].value(), "One Two");
h2.parse("Header-Value: One Two; two=2");
FSL_CHECK(h1.exists("header-value"));
FSL_CHECK_EQ(h1["header-value"].value(), "One Two");
h3.parse("Header_Value: One Two");
FSL_CHECK(not h3.exists("Header_Value"));
}
FSL_TEST_FUNCTION(field_setting) {
fostlib::mime::mime_headers headers;
FSL_CHECK(headers.end() == headers.begin());
headers.set("H1", "a");
FSL_CHECK(headers.end() != headers.begin());
FSL_CHECK(++headers.begin() == headers.end());
headers.set("H1", "b"); // Replaces the content
FSL_CHECK(headers.end() != headers.begin());
FSL_CHECK(++headers.begin() == headers.end());
FSL_CHECK_EQ(headers["H1"].value(), "b");
// Check sub values can be set properly
headers.set_subvalue("H1", "sub", "v1");
FSL_CHECK(headers.end() != headers.begin());
FSL_CHECK(++headers.begin() == headers.end());
FSL_CHECK_EQ(headers["H1"].subvalue("sub").value(), "v1");
headers.set("H1", "c"); // Replaces the content
FSL_CHECK(headers.end() != headers.begin());
FSL_CHECK(++headers.begin() == headers.end());
FSL_CHECK(not headers["H1"].subvalue("sub"));
headers.add("H1", "d"); // Adds a second H1 header
FSL_CHECK(headers.end() != headers.begin());
FSL_CHECK(++headers.begin() != headers.end());
FSL_CHECK(++++headers.begin() == headers.end());
}
FSL_TEST_FUNCTION(json_content) {
headers_base::content line("Header value");
FSL_CHECK_EQ(coerce<json>(line), json("Header value"));
mime::mime_headers headers;
FSL_CHECK_EQ(coerce<json>(headers).size(), 0u);
FSL_CHECK_EQ(coerce<json>(headers), json::object_t());
headers.add("H1", "Content 1");
FSL_CHECK_EQ(coerce<json>(headers).size(), 1u);
FSL_CHECK_EQ(coerce<json>(headers)["H1"], json("Content 1"));
headers.add("H2", "Content 2");
FSL_CHECK_EQ(coerce<json>(headers).size(), 2u);
FSL_CHECK_EQ(coerce<json>(headers)["H1"].size(), 1u);
FSL_CHECK_EQ(coerce<json>(headers)["H1"], json("Content 1"));
FSL_CHECK_EQ(coerce<json>(headers)["H2"], json("Content 2"));
headers.add("H1", "Content 1a");
FSL_CHECK_EQ(coerce<json>(headers).size(), 2u);
FSL_CHECK_EQ(coerce<json>(headers)["H1"].size(), 2u);
FSL_CHECK_EQ(coerce<json>(headers)["H1"][0], json("Content 1"));
FSL_CHECK_EQ(coerce<json>(headers)["H1"][1], json("Content 1a"));
FSL_CHECK_EQ(coerce<json>(headers)["H2"], json("Content 2"));
headers.add("H1", "Content 1b");
FSL_CHECK_EQ(coerce<json>(headers).size(), 2u);
FSL_CHECK_EQ(coerce<json>(headers)["H1"].size(), 3u);
FSL_CHECK_EQ(coerce<json>(headers)["H1"][0], json("Content 1"));
FSL_CHECK_EQ(coerce<json>(headers)["H1"][1], json("Content 1a"));
FSL_CHECK_EQ(coerce<json>(headers)["H1"][2], json("Content 1b"));
FSL_CHECK_EQ(coerce<json>(headers)["H2"], json("Content 2"));
}