brpc_hpack_unittest.cpp 19.1 KB
Newer Older
1
// brpc - A framework to host and access services throughout Baidu.
gejun's avatar
gejun committed
2
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
3 4 5 6 7

// Date: 2017/04/25 00:23:12

#include <gtest/gtest.h>
#include "brpc/details/hpack.h"
8
#include "butil/logging.h"
gejun's avatar
gejun committed
9 10 11 12 13 14 15 16 17 18 19

class HPackTest : public testing::Test {
};

// Copied test cases from example of rfc7541
TEST_F(HPackTest, header_with_indexing) {
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(4096));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(4096));
    brpc::HPacker::Header h;
Zhu Jiashun's avatar
Zhu Jiashun committed
20
    h.name = "Custom-Key";
gejun's avatar
gejun committed
21 22 23
    h.value = "custom-header";
    brpc::HPackOptions options;
    options.index_policy = brpc::HPACK_INDEX_HEADER;
24
    butil::IOBufAppender buf;
Zhu Jiashun's avatar
Zhu Jiashun committed
25
    p1.Encode(&buf, h, options);
gejun's avatar
gejun committed
26
    const ssize_t nwrite = buf.buf().size();
27
    LOG(INFO) << butil::ToPrintable(buf.buf());
gejun's avatar
gejun committed
28 29 30 31
    uint8_t expected[] = {
        0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79,
        0x0d, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x68, 0x65, 0x61, 0x64,
        0x65, 0x72};
32
    butil::StringPiece sp((char*)expected, sizeof(expected));
gejun's avatar
gejun committed
33 34 35 36 37
    ASSERT_TRUE(buf.buf().equals(sp));
    brpc::HPacker::Header h2;
    ssize_t nread = p2.Decode(&buf.buf(), &h2);
    ASSERT_EQ(nread, nwrite);
    ASSERT_TRUE(buf.buf().empty());
Zhu Jiashun's avatar
Zhu Jiashun committed
38 39 40
    std::string lowercase_name = h.name;
    brpc::tolower(&lowercase_name);
    ASSERT_EQ(lowercase_name, h2.name);
gejun's avatar
gejun committed
41 42 43 44 45 46 47 48 49 50 51 52 53
    ASSERT_EQ(h.value, h2.value);
}

TEST_F(HPackTest, header_without_indexing) {
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(4096));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(4096));
    brpc::HPacker::Header h;
    h.name = ":path";
    h.value = "/sample/path";
    brpc::HPackOptions options;
    options.index_policy = brpc::HPACK_NOT_INDEX_HEADER;
54
    butil::IOBufAppender buf;
Zhu Jiashun's avatar
Zhu Jiashun committed
55
    p1.Encode(&buf, h, options);
gejun's avatar
gejun committed
56
    const ssize_t nwrite = buf.buf().size();
57
    LOG(INFO) << butil::ToPrintable(buf.buf());
gejun's avatar
gejun committed
58 59 60 61
    uint8_t expected[] = {
        0x04, 0x0c, 0x2f, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x61,
        0x74, 0x68, 
    };
62
    butil::StringPiece sp((char*)expected, sizeof(expected));
gejun's avatar
gejun committed
63 64 65 66 67
    ASSERT_TRUE(buf.buf().equals(sp));
    brpc::HPacker::Header h2;
    ssize_t nread = p2.Decode(&buf.buf(), &h2);
    ASSERT_EQ(nread, nwrite);
    ASSERT_TRUE(buf.buf().empty());
Zhu Jiashun's avatar
Zhu Jiashun committed
68 69 70
    std::string lowercase_name = h.name;
    brpc::tolower(&lowercase_name);
    ASSERT_EQ(lowercase_name, h2.name);
gejun's avatar
gejun committed
71 72 73 74 75 76 77 78 79 80 81 82 83
    ASSERT_EQ(h.value, h2.value);
}

TEST_F(HPackTest, header_never_indexed) {
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(4096));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(4096));
    brpc::HPacker::Header h;
    h.name = "password";
    h.value = "secret";
    brpc::HPackOptions options;
    options.index_policy = brpc::HPACK_NEVER_INDEX_HEADER;
84
    butil::IOBufAppender buf;
Zhu Jiashun's avatar
Zhu Jiashun committed
85
    p1.Encode(&buf, h, options);
gejun's avatar
gejun committed
86
    const ssize_t nwrite = buf.buf().size();
87
    LOG(INFO) << butil::ToPrintable(buf.buf());
gejun's avatar
gejun committed
88
    uint8_t expected[] = {
Zhu Jiashun's avatar
Zhu Jiashun committed
89 90
        0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64,
        0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 
gejun's avatar
gejun committed
91
    };
92
    butil::StringPiece sp((char*)expected, sizeof(expected));
gejun's avatar
gejun committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    ASSERT_TRUE(buf.buf().equals(sp));
    brpc::HPacker::Header h2;
    ssize_t nread = p2.Decode(&buf.buf(), &h2);
    ASSERT_EQ(nread, nwrite);
    ASSERT_TRUE(buf.buf().empty());
    ASSERT_EQ(h.name, h2.name);
    ASSERT_EQ(h.value, h2.value);
}

TEST_F(HPackTest, indexed_header) {
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(4096));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(4096));
    brpc::HPacker::Header h;
    h.name = ":method";
    h.value = "GET";
    brpc::HPackOptions options;
    options.index_policy = brpc::HPACK_INDEX_HEADER;
112
    butil::IOBufAppender buf;
Zhu Jiashun's avatar
Zhu Jiashun committed
113 114
    p1.Encode(&buf, h, options);
    const ssize_t nwrite = buf.buf().size();
115
    LOG(INFO) << butil::ToPrintable(buf.buf());
gejun's avatar
gejun committed
116 117 118
    uint8_t expected[] = {
        0x82,
    };
119
    butil::StringPiece sp((char*)expected, sizeof(expected));
gejun's avatar
gejun committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    ASSERT_TRUE(buf.buf().equals(sp));
    brpc::HPacker::Header h2;
    ssize_t nread = p2.Decode(&buf.buf(), &h2);
    ASSERT_EQ(nread, nwrite);
    ASSERT_TRUE(buf.buf().empty());
    ASSERT_EQ(h.name, h2.name);
    ASSERT_EQ(h.value, h2.value);
}

struct ConstHeader {
    const char* name;
    const char* value;
};

TEST_F(HPackTest, requests_without_huffman) {
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(4096));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(4096));

    ConstHeader header1[] = { 
        {":method", "GET"},
        {":scheme", "http"},
        {":path", "/"},
        {":authority", "www.example.com"},
    };
146
    butil::IOBufAppender buf;
gejun's avatar
gejun committed
147 148 149 150 151 152 153 154 155 156 157 158
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        h.name = header1[i].name;
        h.value = header1[i].value;
        brpc::HPackOptions options;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected1[] = {
        0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78,
        0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 
    };
159
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1))));
gejun's avatar
gejun committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header1[i].name, h.name);
        ASSERT_EQ(header1[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());

    ConstHeader header2[] = { 
        {":method", "GET"},
        {":scheme", "http"},
        {":path", "/"},
        {":authority", "www.example.com"},
        {"cache-control", "no-cache"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        h.name = header2[i].name;
        h.value = header2[i].value;
        brpc::HPackOptions options;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected2[] = {
        0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d,
        0x63, 0x61, 0x63, 0x68, 0x65, 
    };
188
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected2, sizeof(expected2))));
gejun's avatar
gejun committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header2[i].name, h.name);
        ASSERT_EQ(header2[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
    ConstHeader header3[] = { 
        {":method", "GET"},
        {":scheme", "https"},
        {":path", "/index.html"},
        {":authority", "www.example.com"},
        {"custom-key", "custom-value"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        h.name = header3[i].name;
        h.value = header3[i].value;
        brpc::HPackOptions options;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected3[] = {
        0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d,
        0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d,
        0x76, 0x61, 0x6c, 0x75, 0x65, 
    };
217
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected3, sizeof(expected3))));
gejun's avatar
gejun committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header3[i].name, h.name);
        ASSERT_EQ(header3[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
}

TEST_F(HPackTest, requests_with_huffman) {
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(4096));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(4096));

    ConstHeader header1[] = { 
        {":method", "GET"},
        {":scheme", "http"},
        {":path", "/"},
        {":authority", "www.example.com"},
    };
239
    butil::IOBufAppender buf;
gejun's avatar
gejun committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        h.name = header1[i].name;
        h.value = header1[i].value;
        brpc::HPackOptions options;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        options.encode_name = true;
        options.encode_value = true;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected1[] = {
        0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b,
        0xa0, 0xab, 0x90, 0xf4, 0xff
    };
254
    LOG(INFO) << butil::ToPrintable(buf.buf());
255
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1))));
gejun's avatar
gejun committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header1[i].name, h.name);
        ASSERT_EQ(header1[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());

    ConstHeader header2[] = { 
        {":method", "GET"},
        {":scheme", "http"},
        {":path", "/"},
        {":authority", "www.example.com"},
        {"cache-control", "no-cache"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        h.name = header2[i].name;
        h.value = header2[i].value;
        brpc::HPackOptions options;
        options.encode_name = true;
        options.encode_value = true;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected2[] = {
        0x82, 0x86, 0x84, 0xbe, 0x58, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf, 
    };
285
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected2, sizeof(expected2))));
gejun's avatar
gejun committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header2[i].name, h.name);
        ASSERT_EQ(header2[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
    ConstHeader header3[] = { 
        {":method", "GET"},
        {":scheme", "https"},
        {":path", "/index.html"},
        {":authority", "www.example.com"},
        {"custom-key", "custom-value"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        h.name = header3[i].name;
        h.value = header3[i].value;
        brpc::HPackOptions options;
        options.encode_name = true;
        options.encode_value = true;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected3[] = {
        0x82, 0x87, 0x85, 0xbf, 0x40, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9,
        0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf, 
    };
315
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected3, sizeof(expected3))));
gejun's avatar
gejun committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header3[i].name, h.name);
        ASSERT_EQ(header3[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
}

TEST_F(HPackTest, responses_without_huffman) {
    // https://tools.ietf.org/html/rfc7541#appendix-C.5
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(256));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(256));

    ConstHeader header1[] = { 
        {":status", "302"},
        {"cache-control", "private"},
        {"date", "Mon, 21 Oct 2013 20:13:21 GMT"},
        {"location", "https://www.example.com"},
    };
338
    butil::IOBufAppender buf;
gejun's avatar
gejun committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        h.name = header1[i].name;
        h.value = header1[i].value;
        brpc::HPackOptions options;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected1[] = {
        0x48, 0x03, 0x33, 0x30, 0x32, 0x58, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61,
        0x74, 0x65, 0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20,
        0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a,
        0x31, 0x33, 0x3a, 0x32, 0x31, 0x20, 0x47, 0x4d, 0x54, 0x6e, 0x17, 0x68, 
        0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65,
        0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 
    };
355
    LOG(INFO) << butil::ToPrintable(buf.buf());
356
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1))));
gejun's avatar
gejun committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header1[i].name, h.name);
        ASSERT_EQ(header1[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());

    ConstHeader header2[] = { 
        {":status", "307"},
        {"cache-control", "private"},
        {"date", "Mon, 21 Oct 2013 20:13:21 GMT"},
        {"location", "https://www.example.com"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        h.name = header2[i].name;
        h.value = header2[i].value;
        brpc::HPackOptions options;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected2[] = {
        0x48, 0x03, 0x33, 0x30, 0x37, 0xc1, 0xc0, 0xbf, 
    };
383
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected2, sizeof(expected2))));
gejun's avatar
gejun committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header2[i].name, h.name);
        ASSERT_EQ(header2[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
    ConstHeader header3[] = { 
        {":status", "200"},
        {"cache-control", "private"},
        {"date", "Mon, 21 Oct 2013 20:13:22 GMT"},
        {"location", "https://www.example.com"},
        {"content-encoding", "gzip"},
        {"set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        h.name = header3[i].name;
        h.value = header3[i].value;
        brpc::HPackOptions options;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected3[] = {
        0x88, 0xc1, 0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20,
        0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a,
        0x31, 0x33, 0x3a, 0x32, 0x32, 0x20, 0x47, 0x4d, 0x54, 0xc0, 0x5a, 0x04,
        0x67, 0x7a, 0x69, 0x70, 0x77, 0x38, 0x66, 0x6f, 0x6f, 0x3d, 0x41, 0x53, 
        0x44, 0x4a, 0x4b, 0x48, 0x51, 0x4b, 0x42, 0x5a, 0x58, 0x4f, 0x51, 0x57,
        0x45, 0x4f, 0x50, 0x49, 0x55, 0x41, 0x58, 0x51, 0x57, 0x45, 0x4f, 0x49,
        0x55, 0x3b, 0x20, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65, 0x3d, 0x33,
        0x36, 0x30, 0x30, 0x3b, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 
        0x3d, 0x31, 
    };
419
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected3, sizeof(expected3))));
gejun's avatar
gejun committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header3[i].name, h.name);
        ASSERT_EQ(header3[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
}

TEST_F(HPackTest, responses_with_huffman) {
    // https://tools.ietf.org/html/rfc7541#appendix-C.5
    brpc::HPacker p1;
    ASSERT_EQ(0, p1.Init(256));
    brpc::HPacker p2;
    ASSERT_EQ(0, p2.Init(256));

    ConstHeader header1[] = { 
        {":status", "302"},
        {"cache-control", "private"},
        {"date", "Mon, 21 Oct 2013 20:13:21 GMT"},
        {"location", "https://www.example.com"},
    };
442
    butil::IOBufAppender buf;
gejun's avatar
gejun committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        h.name = header1[i].name;
        h.value = header1[i].value;
        brpc::HPackOptions options;
        options.encode_name = true;
        options.encode_value = true;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected1[] = {
        0x48, 0x82, 0x64, 0x02, 0x58, 0x85, 0xae, 0xc3, 0x77, 0x1a, 0x4b, 0x61,
        0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05,
        0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff, 0x6e,
        0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 
        0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3,             
    };
460
    LOG(INFO) << butil::ToPrintable(buf.buf());
461
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1))));
gejun's avatar
gejun committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header1[i].name, h.name);
        ASSERT_EQ(header1[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());

    ConstHeader header2[] = { 
        {":status", "307"},
        {"cache-control", "private"},
        {"date", "Mon, 21 Oct 2013 20:13:21 GMT"},
        {"location", "https://www.example.com"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        h.name = header2[i].name;
        h.value = header2[i].value;
        brpc::HPackOptions options;
        options.encode_name = true;
        options.encode_value = true;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected2[] = {
        0x48, 0x83, 0x64, 0x0e, 0xff, 0xc1, 0xc0, 0xbf, 
    };
490
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected2, sizeof(expected2))));
gejun's avatar
gejun committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    for (size_t i = 0; i < ARRAY_SIZE(header2); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header2[i].name, h.name);
        ASSERT_EQ(header2[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
    ConstHeader header3[] = { 
        {":status", "200"},
        {"cache-control", "private"},
        {"date", "Mon, 21 Oct 2013 20:13:22 GMT"},
        {"location", "https://www.example.com"},
        {"content-encoding", "gzip"},
        {"set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"},
    };

    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        h.name = header3[i].name;
        h.value = header3[i].value;
        brpc::HPackOptions options;
        options.encode_name = true;
        options.encode_value = true;
        options.index_policy = brpc::HPACK_INDEX_HEADER;
        p1.Encode(&buf, h, options);
    }
    uint8_t expected3[] = {
        0x88, 0xc1, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44,
        0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x84, 0xa6, 0x2d,
        0x1b, 0xff, 0xc0, 0x5a, 0x83, 0x9b, 0xd9, 0xab, 0x77, 0xad, 0x94, 0xe7,
        0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b, 
        0x39, 0x60, 0xd5, 0xaf, 0x27, 0x08, 0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27,
        0x0f, 0xb5, 0x29, 0x1f, 0x95, 0x87, 0x31, 0x60, 0x65, 0xc0, 0x03, 0xed,
        0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07,
    };
526
    ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected3, sizeof(expected3))));
gejun's avatar
gejun committed
527 528 529 530 531 532 533 534
    for (size_t i = 0; i < ARRAY_SIZE(header3); ++i) {
        brpc::HPacker::Header h;
        ASSERT_GT(p2.Decode(&buf.buf(), &h), 0);
        ASSERT_EQ(header3[i].name, h.name);
        ASSERT_EQ(header3[i].value, h.value);
    }
    ASSERT_TRUE(buf.buf().empty());
}