]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | // Test that header file is self-contained. | |
9 | #include <beast/http/chunk_encode.hpp> | |
10 | ||
11 | #include <beast/core/to_string.hpp> | |
12 | #include <beast/unit_test/suite.hpp> | |
13 | ||
14 | namespace beast { | |
15 | namespace http { | |
16 | ||
17 | class chunk_encode_test : public beast::unit_test::suite | |
18 | { | |
19 | public: | |
20 | struct final_chunk | |
21 | { | |
22 | std::string s; | |
23 | ||
24 | final_chunk() = default; | |
25 | ||
26 | explicit | |
27 | final_chunk(std::string s_) | |
28 | : s(std::move(s_)) | |
29 | { | |
30 | } | |
31 | }; | |
32 | ||
33 | static | |
34 | void | |
35 | encode1(std::string& s, final_chunk const& fc) | |
36 | { | |
37 | using boost::asio::buffer; | |
38 | if(! fc.s.empty()) | |
39 | s.append(to_string(chunk_encode( | |
40 | false, buffer(fc.s.data(), fc.s.size())))); | |
41 | s.append(to_string(chunk_encode_final())); | |
42 | } | |
43 | ||
44 | static | |
45 | void | |
46 | encode1(std::string& s, std::string const& piece) | |
47 | { | |
48 | using boost::asio::buffer; | |
49 | s.append(to_string(chunk_encode( | |
50 | false, buffer(piece.data(), piece.size())))); | |
51 | } | |
52 | ||
53 | static | |
54 | inline | |
55 | void | |
56 | encode(std::string&) | |
57 | { | |
58 | } | |
59 | ||
60 | template<class Arg, class... Args> | |
61 | static | |
62 | void | |
63 | encode(std::string& s, Arg const& arg, Args const&... args) | |
64 | { | |
65 | encode1(s, arg); | |
66 | encode(s, args...); | |
67 | } | |
68 | ||
69 | template<class... Args> | |
70 | void | |
71 | check(std::string const& answer, Args const&... args) | |
72 | { | |
73 | std::string s; | |
74 | encode(s, args...); | |
75 | BEAST_EXPECT(s == answer); | |
76 | } | |
77 | ||
78 | void run() override | |
79 | { | |
80 | check( | |
81 | "0\r\n\r\n" | |
82 | "0\r\n\r\n", | |
83 | "", final_chunk{}); | |
84 | ||
85 | check( | |
86 | "1\r\n" | |
87 | "*\r\n" | |
88 | "0\r\n\r\n", | |
89 | final_chunk("*")); | |
90 | ||
91 | check( | |
92 | "2\r\n" | |
93 | "**\r\n" | |
94 | "0\r\n\r\n", | |
95 | final_chunk("**")); | |
96 | ||
97 | check( | |
98 | "1\r\n" | |
99 | "*\r\n" | |
100 | "1\r\n" | |
101 | "*\r\n" | |
102 | "0\r\n\r\n", | |
103 | "*", final_chunk("*")); | |
104 | ||
105 | check( | |
106 | "5\r\n" | |
107 | "*****\r\n" | |
108 | "7\r\n" | |
109 | "*******\r\n" | |
110 | "0\r\n\r\n", | |
111 | "*****", final_chunk("*******")); | |
112 | ||
113 | check( | |
114 | "1\r\n" | |
115 | "*\r\n" | |
116 | "1\r\n" | |
117 | "*\r\n" | |
118 | "0\r\n\r\n", | |
119 | "*", "*", final_chunk{}); | |
120 | ||
121 | check( | |
122 | "4\r\n" | |
123 | "****\r\n" | |
124 | "0\r\n\r\n", | |
125 | "****", final_chunk{}); | |
126 | ||
127 | BEAST_EXPECT(to_string(chunk_encode(true, | |
128 | boost::asio::buffer("****", 4))) == | |
129 | "4\r\n****\r\n0\r\n\r\n"); | |
130 | } | |
131 | }; | |
132 | ||
133 | BEAST_DEFINE_TESTSUITE(chunk_encode,http,beast); | |
134 | ||
135 | } // http | |
136 | } // beast |