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 110 111 112 113 114 115 116 117 118 119 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 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 285 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 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 383 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 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 490 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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/http/http_util.h"
#include <algorithm>
#include <array>
#include <limits>
#include <string_view>
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
TEST(HttpUtilTest, IsSafeHeader) {
static const char* const unsafe_headers[] = {
"sec-",
"sEc-",
"sec-foo",
"sEc-FoO",
"proxy-",
"pRoXy-",
"proxy-foo",
"pRoXy-FoO",
"accept-charset",
"accept-encoding",
"access-control-request-headers",
"access-control-request-method",
"access-control-request-private-network",
"connection",
"content-length",
"cookie",
"cookie2",
"date",
"dnt",
"expect",
"host",
"keep-alive",
"origin",
"referer",
"set-cookie",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"user-agent",
"via",
};
for (const auto* unsafe_header : unsafe_headers) {
EXPECT_FALSE(HttpUtil::IsSafeHeader(unsafe_header, "")) << unsafe_header;
EXPECT_FALSE(HttpUtil::IsSafeHeader(base::ToUpperASCII(unsafe_header), ""))
<< unsafe_header;
}
static const char* const safe_headers[] = {
"foo",
"x-",
"x-foo",
"content-disposition",
"update",
"accept-charseta",
"accept_charset",
"accept-encodinga",
"accept_encoding",
"access-control-request-headersa",
"access-control-request-header",
"access_control_request_header",
"access-control-request-methoda",
"access_control_request_method",
"connectiona",
"content-lengtha",
"content_length",
"content-transfer-encoding",
"cookiea",
"cookie2a",
"cookie3",
"content-transfer-encodinga",
"content_transfer_encoding",
"datea",
"expecta",
"hosta",
"keep-alivea",
"keep_alive",
"origina",
"referera",
"referrer",
"tea",
"trailera",
"transfer-encodinga",
"transfer_encoding",
"upgradea",
"user-agenta",
"user_agent",
"viaa",
// Following 3 headers are safe if there is no forbidden method in values.
"x-http-method",
"x-http-method-override",
"x-method-override",
};
for (const auto* safe_header : safe_headers) {
EXPECT_TRUE(HttpUtil::IsSafeHeader(safe_header, "")) << safe_header;
EXPECT_TRUE(HttpUtil::IsSafeHeader(base::ToUpperASCII(safe_header), ""))
<< safe_header;
}
static const char* const disallowed_with_forbidden_methods_headers[] = {
"x-http-method",
"x-http-method-override",
"x-method-override",
};
static const struct {
const char* value;
bool is_safe;
} disallowed_values[] = {{"connect", false},
{"trace", false},
{"track", false},
{"CONNECT", false},
{"cOnnEcT", false},
{"get", true},
{"get,post", true},
{"get,connect", false},
{"get, connect", false},
{"get,connect ", false},
{"get,connect ,post", false},
{"get,,,,connect", false},
{"trace,get,PUT", false}};
for (const auto* header : disallowed_with_forbidden_methods_headers) {
for (const auto& test_case : disallowed_values) {
EXPECT_EQ(test_case.is_safe,
HttpUtil::IsSafeHeader(header, test_case.value))
<< header << ": " << test_case.value;
}
}
}
TEST(HttpUtilTest, HeadersIterator) {
std::string headers = "foo: 1\t\r\nbar: hello world\r\nbaz: 3 \r\n";
HttpUtil::HeadersIterator it(headers, "\r\n");
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("foo", it.name());
EXPECT_EQ("1", it.values());
EXPECT_EQ(0u, it.name_begin());
EXPECT_EQ(3u, it.name_end());
EXPECT_EQ(5u, it.values_begin());
EXPECT_EQ(6u, it.values_end());
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("bar", it.name());
EXPECT_EQ("hello world", it.values());
EXPECT_EQ(9u, it.name_begin());
EXPECT_EQ(12u, it.name_end());
EXPECT_EQ(14u, it.values_begin());
EXPECT_EQ(25u, it.values_end());
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("baz", it.name());
EXPECT_EQ("3", it.values());
EXPECT_EQ(27u, it.name_begin());
EXPECT_EQ(30u, it.name_end());
EXPECT_EQ(32u, it.values_begin());
EXPECT_EQ(33u, it.values_end());
EXPECT_FALSE(it.GetNext());
}
TEST(HttpUtilTest, HeadersIterator_MalformedLine) {
std::string headers = "foo: 1\n: 2\n3\nbar: 4";
HttpUtil::HeadersIterator it(headers, "\n");
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("foo", it.name());
EXPECT_EQ("1", it.values());
EXPECT_EQ(0u, it.name_begin());
EXPECT_EQ(3u, it.name_end());
EXPECT_EQ(5u, it.values_begin());
EXPECT_EQ(6u, it.values_end());
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("bar", it.name());
EXPECT_EQ("4", it.values());
EXPECT_EQ(13u, it.name_begin());
EXPECT_EQ(16u, it.name_end());
EXPECT_EQ(18u, it.values_begin());
EXPECT_EQ(19u, it.values_end());
EXPECT_FALSE(it.GetNext());
}
TEST(HttpUtilTest, HeadersIterator_MalformedName) {
std::string headers = "[ignore me] /: 3\r\n";
HttpUtil::HeadersIterator it(headers, "\r\n");
EXPECT_FALSE(it.GetNext());
}
TEST(HttpUtilTest, HeadersIterator_MalformedNameFollowedByValidLine) {
std::string headers = "[ignore me] /: 3\r\nbar: 4\n";
HttpUtil::HeadersIterator it(headers, "\r\n");
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("bar", it.name());
EXPECT_EQ("4", it.values());
EXPECT_EQ(18u, it.name_begin());
EXPECT_EQ(21u, it.name_end());
EXPECT_EQ(23u, it.values_begin());
EXPECT_EQ(24u, it.values_end());
EXPECT_FALSE(it.GetNext());
}
TEST(HttpUtilTest, ValuesIterator) {
std::string values = " must-revalidate, no-cache=\"foo, bar\"\t, private ";
HttpUtil::ValuesIterator it(values, ',',
/*ignore_empty_values=*/true);
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("must-revalidate", it.value());
EXPECT_EQ(1, it.value_begin());
EXPECT_EQ(16, it.value_end());
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("no-cache=\"foo, bar\"", it.value());
EXPECT_EQ(20, it.value_begin());
EXPECT_EQ(39, it.value_end());
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("private", it.value());
EXPECT_EQ(42, it.value_begin());
EXPECT_EQ(49, it.value_end());
EXPECT_FALSE(it.GetNext());
}
TEST(HttpUtilTest, ValuesIterator_EmptyValues) {
std::string values = ", foopy , \t ,,,";
HttpUtil::ValuesIterator it(values, ',', /*ignore_empty_values=*/true);
ASSERT_TRUE(it.GetNext());
EXPECT_EQ("foopy", it.value());
EXPECT_EQ(2, it.value_begin());
EXPECT_EQ(7, it.value_end());
EXPECT_FALSE(it.GetNext());
HttpUtil::ValuesIterator it_with_empty_values(values, ',',
/*ignore_empty_values=*/false);
ASSERT_TRUE(it_with_empty_values.GetNext());
EXPECT_EQ("", it_with_empty_values.value());
EXPECT_EQ(0, it_with_empty_values.value_begin());
EXPECT_EQ(0, it_with_empty_values.value_end());
ASSERT_TRUE(it_with_empty_values.GetNext());
EXPECT_EQ("foopy", it_with_empty_values.value());
EXPECT_EQ(2, it_with_empty_values.value_begin());
EXPECT_EQ(7, it_with_empty_values.value_end());
ASSERT_TRUE(it_with_empty_values.GetNext());
EXPECT_EQ("", it_with_empty_values.value());
EXPECT_EQ(12, it_with_empty_values.value_begin());
EXPECT_EQ(12, it_with_empty_values.value_end());
ASSERT_TRUE(it_with_empty_values.GetNext());
EXPECT_EQ("", it_with_empty_values.value());
EXPECT_EQ(13, it_with_empty_values.value_begin());
EXPECT_EQ(13, it_with_empty_values.value_end());
ASSERT_TRUE(it_with_empty_values.GetNext());
EXPECT_EQ("", it_with_empty_values.value());
EXPECT_EQ(14, it_with_empty_values.value_begin());
EXPECT_EQ(14, it_with_empty_values.value_end());
ASSERT_TRUE(it_with_empty_values.GetNext());
EXPECT_EQ("", it_with_empty_values.value());
EXPECT_EQ(15, it_with_empty_values.value_begin());
EXPECT_EQ(15, it_with_empty_values.value_end());
EXPECT_FALSE(it_with_empty_values.GetNext());
}
TEST(HttpUtilTest, ValuesIterator_Blanks) {
std::string values = " \t ";
HttpUtil::ValuesIterator it(values, ',', /*ignore_empty_values=*/true);
EXPECT_FALSE(it.GetNext());
HttpUtil::ValuesIterator it_with_empty_values(values, ',',
/*ignore_empty_values=*/false);
ASSERT_TRUE(it_with_empty_values.GetNext());
EXPECT_EQ("", it_with_empty_values.value());
EXPECT_EQ(3, it_with_empty_values.value_begin());
EXPECT_EQ(3, it_with_empty_values.value_end());
EXPECT_FALSE(it_with_empty_values.GetNext());
}
TEST(HttpUtilTest, Unquote) {
// Replace <backslash> " with ".
EXPECT_STREQ("xyz\"abc", HttpUtil::Unquote("\"xyz\\\"abc\"").c_str());
// Replace <backslash> <backslash> with <backslash>
EXPECT_STREQ("xyz\\abc", HttpUtil::Unquote("\"xyz\\\\abc\"").c_str());
EXPECT_STREQ("xyz\\\\\\abc",
HttpUtil::Unquote("\"xyz\\\\\\\\\\\\abc\"").c_str());
// Replace <backslash> X with X
EXPECT_STREQ("xyzXabc", HttpUtil::Unquote("\"xyz\\Xabc\"").c_str());
// Act as identity function on unquoted inputs.
EXPECT_STREQ("X", HttpUtil::Unquote("X").c_str());
EXPECT_STREQ("\"", HttpUtil::Unquote("\"").c_str());
// Allow quotes in the middle of the input.
EXPECT_STREQ("foo\"bar", HttpUtil::Unquote("\"foo\"bar\"").c_str());
// Allow the final quote to be escaped.
EXPECT_STREQ("foo", HttpUtil::Unquote("\"foo\\\"").c_str());
}
TEST(HttpUtilTest, StrictUnquote) {
std::string out;
// Replace <backslash> " with ".
EXPECT_TRUE(HttpUtil::StrictUnquote("\"xyz\\\"abc\"", &out));
EXPECT_STREQ("xyz\"abc", out.c_str());
// Replace <backslash> <backslash> with <backslash>.
EXPECT_TRUE(HttpUtil::StrictUnquote("\"xyz\\\\abc\"", &out));
EXPECT_STREQ("xyz\\abc", out.c_str());
EXPECT_TRUE(HttpUtil::StrictUnquote("\"xyz\\\\\\\\\\\\abc\"", &out));
EXPECT_STREQ("xyz\\\\\\abc", out.c_str());
// Replace <backslash> X with X.
EXPECT_TRUE(HttpUtil::StrictUnquote("\"xyz\\Xabc\"", &out));
EXPECT_STREQ("xyzXabc", out.c_str());
// Empty quoted string.
EXPECT_TRUE(HttpUtil::StrictUnquote("\"\"", &out));
EXPECT_STREQ("", out.c_str());
// Return false on unquoted inputs.
EXPECT_FALSE(HttpUtil::StrictUnquote("X", &out));
EXPECT_FALSE(HttpUtil::StrictUnquote("", &out));
// Return false on mismatched quotes.
EXPECT_FALSE(HttpUtil::StrictUnquote("\"", &out));
EXPECT_FALSE(HttpUtil::StrictUnquote("\"xyz", &out));
EXPECT_FALSE(HttpUtil::StrictUnquote("\"abc'", &out));
// Return false on escaped terminal quote.
EXPECT_FALSE(HttpUtil::StrictUnquote("\"abc\\\"", &out));
EXPECT_FALSE(HttpUtil::StrictUnquote("\"\\\"", &out));
// Allow escaped backslash before terminal quote.
EXPECT_TRUE(HttpUtil::StrictUnquote("\"\\\\\"", &out));
EXPECT_STREQ("\\", out.c_str());
// Don't allow single quotes to act as quote marks.
EXPECT_FALSE(HttpUtil::StrictUnquote("'x\"'", &out));
EXPECT_TRUE(HttpUtil::StrictUnquote("\"x'\"", &out));
EXPECT_STREQ("x'", out.c_str());
EXPECT_FALSE(HttpUtil::StrictUnquote("''", &out));
}
TEST(HttpUtilTest, Quote) {
EXPECT_STREQ("\"xyz\\\"abc\"", HttpUtil::Quote("xyz\"abc").c_str());
// Replace <backslash> <backslash> with <backslash>
EXPECT_STREQ("\"xyz\\\\abc\"", HttpUtil::Quote("xyz\\abc").c_str());
// Replace <backslash> X with X
EXPECT_STREQ("\"xyzXabc\"", HttpUtil::Quote("xyzXabc").c_str());
}
TEST(HttpUtilTest, LocateEndOfHeaders) {
struct {
const std::string_view input;
size_t expected_result;
} tests[] = {
{"\r\n", std::string::npos},
{"\n", std::string::npos},
{"\r", std::string::npos},
{"foo", std::string::npos},
{"\r\n\r\n", 4},
{"foo\r\nbar\r\n\r\n", 12},
{"foo\nbar\n\n", 9},
{"foo\r\nbar\r\n\r\njunk", 12},
{"foo\nbar\n\njunk", 9},
{"foo\nbar\n\r\njunk", 10},
{"foo\nbar\r\n\njunk", 10},
};
for (const auto& test : tests) {
size_t eoh = HttpUtil::LocateEndOfHeaders(base::as_byte_span(test.input));
EXPECT_EQ(test.expected_result, eoh);
}
}
TEST(HttpUtilTest, LocateEndOfAdditionalHeaders) {
struct {
const std::string_view input;
size_t expected_result;
} tests[] = {
{"\r\n", 2},
{"\n", 1},
{"\r", std::string::npos},
{"foo", std::string::npos},
{"\r\n\r\n", 2},
{"foo\r\nbar\r\n\r\n", 12},
{"foo\nbar\n\n", 9},
{"foo\r\nbar\r\n\r\njunk", 12},
{"foo\nbar\n\njunk", 9},
{"foo\nbar\n\r\njunk", 10},
{"foo\nbar\r\n\njunk", 10},
};
for (const auto& test : tests) {
size_t eoh =
HttpUtil::LocateEndOfAdditionalHeaders(base::as_byte_span(test.input));
EXPECT_EQ(test.expected_result, eoh);
}
}
TEST(HttpUtilTest, AssembleRawHeaders) {
// clang-format off
struct {
const char* const input; // with '|' representing '\0'
const char* const expected_result; // with '\0' changed to '|'
} tests[] = {
{ "HTTP/1.0 200 OK\r\nFoo: 1\r\nBar: 2\r\n\r\n",
"HTTP/1.0 200 OK|Foo: 1|Bar: 2||" },
{ "HTTP/1.0 200 OK\nFoo: 1\nBar: 2\n\n",
"HTTP/1.0 200 OK|Foo: 1|Bar: 2||" },
// Valid line continuation (single SP).
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
" continuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1 continuation|"
"Bar: 2||"
},
// Valid line continuation (single HT).
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"\tcontinuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1 continuation|"
"Bar: 2||"
},
// Valid line continuation (multiple SP).
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
" continuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1 continuation|"
"Bar: 2||"
},
// Valid line continuation (multiple HT).
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"\t\t\tcontinuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1 continuation|"
"Bar: 2||"
},
// Valid line continuation (mixed HT, SP).
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
" \t \t continuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1 continuation|"
"Bar: 2||"
},
// Valid multi-line continuation
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
" continuation1\n"
"\tcontinuation2\n"
" continuation3\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1 continuation1 continuation2 continuation3|"
"Bar: 2||"
},
// Continuation of quoted value.
// This is different from what Firefox does, since it
// will preserve the LWS.
{
"HTTP/1.0 200 OK\n"
"Etag: \"34534-d3\n"
" 134q\"\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Etag: \"34534-d3 134q\"|"
"Bar: 2||"
},
// Valid multi-line continuation, full LWS lines
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
" \n"
"\t\t\t\t\n"
"\t continuation\n"
"Bar: 2\n\n",
// One SP per continued line = 3.
"HTTP/1.0 200 OK|"
"Foo: 1 continuation|"
"Bar: 2||"
},
// Valid multi-line continuation, all LWS
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
" \n"
"\t\t\t\t\n"
"\t \n"
"Bar: 2\n\n",
// One SP per continued line = 3.
"HTTP/1.0 200 OK|"
"Foo: 1 |"
"Bar: 2||"
},
// Valid line continuation (No value bytes in first line).
{
"HTTP/1.0 200 OK\n"
"Foo:\n"
" value\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: value|"
"Bar: 2||"
},
// Not a line continuation (can't continue status line).
{
"HTTP/1.0 200 OK\n"
" Foo: 1\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
" Foo: 1|"
"Bar: 2||"
},
// Not a line continuation (can't continue status line).
{
"HTTP/1.0\n"
" 200 OK\n"
"Foo: 1\n"
"Bar: 2\n\n",
"HTTP/1.0|"
" 200 OK|"
"Foo: 1|"
"Bar: 2||"
},
// Not a line continuation (can't continue status line).
{
"HTTP/1.0 404\n"
" Not Found\n"
"Foo: 1\n"
"Bar: 2\n\n",
"HTTP/1.0 404|"
" Not Found|"
"Foo: 1|"
"Bar: 2||"
},
// Unterminated status line.
{
"HTTP/1.0 200 OK",
"HTTP/1.0 200 OK||"
},
// Single terminated, with headers
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"Bar: 2\n",
"HTTP/1.0 200 OK|"
"Foo: 1|"
"Bar: 2||"
},
// Not terminated, with headers
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"Bar: 2",
"HTTP/1.0 200 OK|"
"Foo: 1|"
"Bar: 2||"
},
// Not a line continuation (VT)
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"\vInvalidContinuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1|"
"\vInvalidContinuation|"
"Bar: 2||"
},
// Not a line continuation (formfeed)
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"\fInvalidContinuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1|"
"\fInvalidContinuation|"
"Bar: 2||"
},
// Not a line continuation -- can't continue header names.
{
"HTTP/1.0 200 OK\n"
"Serv\n"
" er: Apache\n"
"\tInvalidContinuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Serv|"
" er: Apache|"
"\tInvalidContinuation|"
"Bar: 2||"
},
// Not a line continuation -- no value to continue.
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"garbage\n"
" not-a-continuation\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
"Foo: 1|"
"garbage|"
" not-a-continuation|"
"Bar: 2||",
},
// Not a line continuation -- no valid name.
{
"HTTP/1.0 200 OK\n"
": 1\n"
" garbage\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
": 1|"
" garbage|"
"Bar: 2||",
},
// Not a line continuation -- no valid name (whitespace)
{
"HTTP/1.0 200 OK\n"
" : 1\n"
" garbage\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|"
" : 1|"
" garbage|"
"Bar: 2||",
},
// Embed NULLs in the status line. They should not be understood
// as line separators.
{
"HTTP/1.0 200 OK|Bar2:0|Baz2:1\r\nFoo: 1\r\nBar: 2\r\n\r\n",
"HTTP/1.0 200 OKBar2:0Baz2:1|Foo: 1|Bar: 2||"
},
// Embed NULLs in a header line. They should not be understood as
// line separators.
{
"HTTP/1.0 200 OK\nFoo: 1|Foo2: 3\nBar: 2\n\n",
"HTTP/1.0 200 OK|Foo: 1Foo2: 3|Bar: 2||"
},
// The embedded NUL at the start of the line (before "Blah:") should not be
// interpreted as LWS (as that would mistake it for a header line
// continuation).
{
"HTTP/1.0 200 OK\n"
"Foo: 1\n"
"|Blah: 3\n"
"Bar: 2\n\n",
"HTTP/1.0 200 OK|Foo: 1|Blah: 3|Bar: 2||"
},
};
// clang-format on
for (const auto& test : tests) {
std::string input = test.input;
std::replace(input.begin(), input.end(), '|', '\0');
std::string raw = HttpUtil::AssembleRawHeaders(input);
std::replace(raw.begin(), raw.end(), '\0', '|');
EXPECT_EQ(test.expected_result, raw);
}
}
// Test SpecForRequest().
TEST(HttpUtilTest, RequestUrlSanitize) {
struct Tests {
const char* const url;
const char* const expected_spec;
};
auto tests = std::to_array<Tests>({
{
// Check that #hash is removed.
"http://www.google.com:78/foobar?query=1#hash",
"http://www.google.com:78/foobar?query=1",
},
{
// The reference may itself contain # -- strip all of it.
"http://192.168.0.1?query=1#hash#10#11#13#14",
"http://192.168.0.1/?query=1",
},
{
// Strip username/password.
"http://user:pass@google.com",
"http://google.com/",
},
{
// https scheme
"https://www.google.com:78/foobar?query=1#hash",
"https://www.google.com:78/foobar?query=1",
},
{
// WebSocket's ws scheme
"ws://www.google.com:78/foobar?query=1#hash",
"ws://www.google.com:78/foobar?query=1",
},
{
// WebSocket's wss scheme
"wss://www.google.com:78/foobar?query=1#hash",
"wss://www.google.com:78/foobar?query=1",
},
});
for (size_t i = 0; i < std::size(tests); ++i) {
SCOPED_TRACE(i);
GURL url(GURL(tests[i].url));
std::string expected_spec(tests[i].expected_spec);
EXPECT_EQ(expected_spec, HttpUtil::SpecForRequest(url));
}
}
TEST(HttpUtilTest, GenerateAcceptLanguageHeader) {
std::string header = HttpUtil::GenerateAcceptLanguageHeader("");
EXPECT_TRUE(header.empty());
header = HttpUtil::GenerateAcceptLanguageHeader("es");
EXPECT_EQ(std::string("es"), header);
header = HttpUtil::GenerateAcceptLanguageHeader("en-US,fr,de");
EXPECT_EQ(std::string("en-US,fr;q=0.9,de;q=0.8"), header);
header = HttpUtil::GenerateAcceptLanguageHeader("en-US,fr,de,ko,zh-CN,ja");
EXPECT_EQ(
std::string("en-US,fr;q=0.9,de;q=0.8,ko;q=0.7,zh-CN;q=0.6,ja;q=0.5"),
header);
}
// HttpResponseHeadersTest.GetMimeType also tests ParseContentType.
TEST(HttpUtilTest, ParseContentType) {
// clang-format off
const struct {
const char* const content_type;
const char* const expected_mime_type;
const char* const expected_charset;
const bool expected_had_charset;
const char* const expected_boundary;
} tests[] = {
{ "text/html",
"text/html",
"",
false,
""
},
{ "text/html;",
"text/html",
"",
false,
""
},
{ "text/html; charset=utf-8",
"text/html",
"utf-8",
true,
""
},
// Parameter name is "charset ", not "charset". See https://crbug.com/772834.
{ "text/html; charset =utf-8",
"text/html",
"",
false,
""
},
{ "text/html; charset= utf-8",
"text/html",
"utf-8",
true,
""
},
{ "text/html; charset=utf-8 ",
"text/html",
"utf-8",
true,
""
},
{ "text/html; boundary=\"WebKit-ada-df-dsf-adsfadsfs\"",
"text/html",
"",
false,
"WebKit-ada-df-dsf-adsfadsfs"
},
// Parameter name is "boundary ", not "boundary".
// See https://crbug.com/772834.
{ "text/html; boundary =\"WebKit-ada-df-dsf-adsfadsfs\"",
"text/html",
"",
false,
""
},
// Parameter value includes leading space. See https://crbug.com/772834.
{ "text/html; boundary= \"WebKit-ada-df-dsf-adsfadsfs\"",
"text/html",
"",
false,
"WebKit-ada-df-dsf-adsfadsfs"
},
// Parameter value includes leading space. See https://crbug.com/772834.
{ "text/html; boundary= \"WebKit-ada-df-dsf-adsfadsfs\" ",
"text/html",
"",
false,
"WebKit-ada-df-dsf-adsfadsfs"
},
{ "text/html; boundary=\"WebKit-ada-df-dsf-adsfadsfs \"",
"text/html",
"",
false,
"WebKit-ada-df-dsf-adsfadsfs"
},
{ "text/html; boundary=WebKit-ada-df-dsf-adsfadsfs",
"text/html",
"",
false,
"WebKit-ada-df-dsf-adsfadsfs"
},
{ "text/html; charset",
"text/html",
"",
false,
""
},
{ "text/html; charset=",
"text/html",
"",
false,
""
},
{ "text/html; charset= ",
"text/html",
"",
false,
""
},
{ "text/html; charset= ;",
"text/html",
"",
false,
""
},
// Empty quoted strings are allowed.
{ "text/html; charset=\"\"",
"text/html",
"",
true,
""
},
// Leading and trailing whitespace in quotes is trimmed.
{ "text/html; charset=\" \"",
"text/html",
"",
true,
""
},
{ "text/html; charset=\" foo \"",
"text/html",
"foo",
true,
""
},
// With multiple values, should use the first one.
{ "text/html; charset=foo; charset=utf-8",
"text/html",
"foo",
true,
""
},
{ "text/html; charset; charset=; charset=utf-8",
"text/html",
"utf-8",
true,
""
},
{ "text/html; charset=utf-8; charset=; charset",
"text/html",
"utf-8",
true,
""
},
{ "text/html; boundary=foo; boundary=bar",
"text/html",
"",
false,
"foo"
},
// Stray quotes ignored.
{ "text/html; \"; \"\"; charset=utf-8",
"text/html",
"utf-8",
true,
""
},
// Non-leading quotes kept as-is.
{ "text/html; charset=u\"tf-8\"",
"text/html",
"u\"tf-8\"",
true,
""
},
{ "text/html; charset=\"utf-8\"",
"text/html",
"utf-8",
true,
""
},
// No closing quote.
{ "text/html; charset=\"utf-8",
"text/html",
"utf-8",
true,
""
},
// Check that \ is treated as an escape character.
{ "text/html; charset=\"\\utf\\-\\8\"",
"text/html",
"utf-8",
true,
""
},
// More interseting escape character test - test escaped backslash, escaped
// quote, and backslash at end of input in unterminated quoted string.
{ "text/html; charset=\"\\\\\\\"\\",
"text/html",
"\\\"\\",
true,
""
},
// Check quoted semicolon.
{ "text/html; charset=\";charset=utf-8;\"",
"text/html",
";charset=utf-8;",
true,
""
},
// Unclear if this one should just return utf-8 or not.
{ "text/html; charset= \"utf-8\"",
"text/html",
"utf-8",
true,
""
},
// Regression test for https://crbug.com/772350:
// Single quotes are not delimiters but must be treated as part of charset.
{ "text/html; charset='utf-8'",
"text/html",
"'utf-8'",
true,
""
},
// Empty subtype should be accepted.
{ "text/",
"text/",
"",
false,
""
},
// "*/*" is ignored unless it has params, or is not an exact match.
{ "*/*", "", "", false, "" },
{ "*/*; charset=utf-8", "*/*", "utf-8", true, "" },
{ "*/* ", "*/*", "", false, "" },
// Regression test for https://crbug.com/1326529
{ "teXT/html", "text/html", "", false, ""},
// TODO(abarth): Add more interesting test cases.
};
// clang-format on
for (const auto& test : tests) {
std::string mime_type;
std::string charset;
bool had_charset = false;
std::string boundary;
HttpUtil::ParseContentType(test.content_type, &mime_type, &charset,
&had_charset, &boundary);
EXPECT_EQ(test.expected_mime_type, mime_type)
<< "content_type=" << test.content_type;
EXPECT_EQ(test.expected_charset, charset)
<< "content_type=" << test.content_type;
EXPECT_EQ(test.expected_had_charset, had_charset)
<< "content_type=" << test.content_type;
EXPECT_EQ(test.expected_boundary, boundary)
<< "content_type=" << test.content_type;
}
}
TEST(HttpUtilTest, ParseContentResetCharset) {
std::string mime_type;
std::string charset;
bool had_charset = false;
std::string boundary;
// Set mime (capitalization should be ignored), but not charset.
HttpUtil::ParseContentType("Text/Html", &mime_type, &charset, &had_charset,
&boundary);
EXPECT_EQ("text/html", mime_type);
EXPECT_EQ("", charset);
EXPECT_FALSE(had_charset);
// The same mime, add charset.
HttpUtil::ParseContentType("tExt/hTml;charset=utf-8", &mime_type, &charset,
&had_charset, &boundary);
EXPECT_EQ("text/html", mime_type);
EXPECT_EQ("utf-8", charset);
EXPECT_TRUE(had_charset);
// The same mime (different capitalization), but no charset - should not clear
// charset.
HttpUtil::ParseContentType("teXt/htMl", &mime_type, &charset, &had_charset,
&boundary);
EXPECT_EQ("text/html", mime_type);
EXPECT_EQ("utf-8", charset);
EXPECT_TRUE(had_charset);
// A different mime will clear charset.
HttpUtil::ParseContentType("texT/plaiN", &mime_type, &charset, &had_charset,
&boundary);
EXPECT_EQ("text/plain", mime_type);
EXPECT_EQ("", charset);
EXPECT_TRUE(had_charset);
}
TEST(HttpUtilTest, ParseContentRangeHeader) {
const struct {
const char* const content_range_header_spec;
bool expected_return_value;
int64_t expected_first_byte_position;
int64_t expected_last_byte_position;
int64_t expected_instance_length;
} tests[] = {
{"", false, -1, -1, -1},
{"megabytes 0-10/50", false, -1, -1, -1},
{"0-10/50", false, -1, -1, -1},
{"Bytes 0-50/51", true, 0, 50, 51},
{"bytes 0-50/51", true, 0, 50, 51},
{"bytes\t0-50/51", false, -1, -1, -1},
{" bytes 0-50/51", true, 0, 50, 51},
{" bytes 0 - 50 \t / \t51", true, 0, 50, 51},
{"bytes 0\t-\t50\t/\t51\t", true, 0, 50, 51},
{" \tbytes\t\t\t 0\t-\t50\t/\t51\t", true, 0, 50, 51},
{"\t bytes \t 0 - 50 / 5 1", false, -1, -1, -1},
{"\t bytes \t 0 - 5 0 / 51", false, -1, -1, -1},
{"bytes 50-0/51", false, -1, -1, -1},
{"bytes * /*", false, -1, -1, -1},
{"bytes * / * ", false, -1, -1, -1},
{"bytes 0-50/*", false, -1, -1, -1},
{"bytes 0-50 / * ", false, -1, -1, -1},
{"bytes 0-10000000000/10000000001", true, 0, 10000000000ll,
10000000001ll},
{"bytes 0-10000000000/10000000000", false, -1, -1, -1},
// 64 bit wraparound.
{"bytes 0 - 9223372036854775807 / 100", false, -1, -1, -1},
// 64 bit wraparound.
{"bytes 0 - 100 / -9223372036854775808", false, -1, -1, -1},
{"bytes */50", false, -1, -1, -1},
{"bytes 0-50/10", false, -1, -1, -1},
{"bytes 40-50/45", false, -1, -1, -1},
{"bytes 0-50/-10", false, -1, -1, -1},
{"bytes 0-0/1", true, 0, 0, 1},
{"bytes 0-40000000000000000000/40000000000000000001", false, -1, -1, -1},
{"bytes 1-/100", false, -1, -1, -1},
{"bytes -/100", false, -1, -1, -1},
{"bytes -1/100", false, -1, -1, -1},
{"bytes 0-1233/*", false, -1, -1, -1},
{"bytes -123 - -1/100", false, -1, -1, -1},
};
for (const auto& test : tests) {
int64_t first_byte_position, last_byte_position, instance_length;
EXPECT_EQ(test.expected_return_value,
HttpUtil::ParseContentRangeHeaderFor206(
test.content_range_header_spec, &first_byte_position,
&last_byte_position, &instance_length))
<< test.content_range_header_spec;
EXPECT_EQ(test.expected_first_byte_position, first_byte_position)
<< test.content_range_header_spec;
EXPECT_EQ(test.expected_last_byte_position, last_byte_position)
<< test.content_range_header_spec;
EXPECT_EQ(test.expected_instance_length, instance_length)
<< test.content_range_header_spec;
}
}
TEST(HttpUtilTest, ParseRetryAfterHeader) {
base::Time::Exploded now_exploded = {2014, 11, 4, 5, 22, 39, 30, 0};
base::Time now;
EXPECT_TRUE(base::Time::FromUTCExploded(now_exploded, &now));
base::Time::Exploded later_exploded = {2015, 1, 5, 1, 12, 34, 56, 0};
base::Time later;
EXPECT_TRUE(base::Time::FromUTCExploded(later_exploded, &later));
struct Tests {
const char* retry_after_string;
bool expected_return_value;
base::TimeDelta expected_retry_after;
};
const auto tests = std::to_array<Tests>({
{"", false, base::TimeDelta()},
{"-3", false, base::TimeDelta()},
{"-2", false, base::TimeDelta()},
{"-1", false, base::TimeDelta()},
{"+0", false, base::TimeDelta()},
{"+1", false, base::TimeDelta()},
{"0", true, base::Seconds(0)},
{"1", true, base::Seconds(1)},
{"2", true, base::Seconds(2)},
{"3", true, base::Seconds(3)},
{"60", true, base::Seconds(60)},
{"3600", true, base::Seconds(3600)},
{"86400", true, base::Seconds(86400)},
{"Thu, 1 Jan 2015 12:34:56 GMT", true, later - now},
{"Mon, 1 Jan 1900 12:34:56 GMT", false, base::TimeDelta()},
});
for (size_t i = 0; i < std::size(tests); ++i) {
base::TimeDelta retry_after;
bool return_value = HttpUtil::ParseRetryAfterHeader(
tests[i].retry_after_string, now, &retry_after);
EXPECT_EQ(tests[i].expected_return_value, return_value)
<< "Test case " << i << ": expected " << tests[i].expected_return_value
<< " but got " << return_value << ".";
if (tests[i].expected_return_value && return_value) {
EXPECT_EQ(tests[i].expected_retry_after, retry_after)
<< "Test case " << i << ": expected "
<< tests[i].expected_retry_after.InSeconds() << "s but got "
<< retry_after.InSeconds() << "s.";
}
}
}
TEST(HttpUtilTest, TimeFormatHTTP) {
constexpr base::Time::Exploded kTime = {.year = 2011,
.month = 4,
.day_of_week = 6,
.day_of_month = 30,
.hour = 22,
.minute = 42,
.second = 7};
base::Time time;
EXPECT_TRUE(base::Time::FromUTCExploded(kTime, &time));
EXPECT_EQ("Sat, 30 Apr 2011 22:42:07 GMT", HttpUtil::TimeFormatHTTP(time));
}
namespace {
void CheckCurrentNameValuePair(HttpUtil::NameValuePairsIterator* parser,
bool expect_valid,
std::string expected_name,
std::string expected_value) {
ASSERT_EQ(expect_valid, parser->valid());
if (!expect_valid) {
return;
}
// Let's make sure that this never changes (i.e., when a quoted value is
// unquoted, it should be cached on the first calls and not regenerated
// later).
const std::string_view first_value = parser->value();
ASSERT_EQ(expected_name, parser->name());
ASSERT_EQ(expected_value, parser->value());
// Make sure they didn't/don't change.
ASSERT_TRUE(first_value.data() == parser->value().data());
ASSERT_TRUE(first_value.length() == parser->value().length());
}
void CheckNextNameValuePair(HttpUtil::NameValuePairsIterator* parser,
bool expect_next,
bool expect_valid,
std::string expected_name,
std::string expected_value) {
ASSERT_EQ(expect_next, parser->GetNext());
ASSERT_EQ(expect_valid, parser->valid());
if (!expect_next || !expect_valid) {
return;
}
CheckCurrentNameValuePair(parser,
expect_valid,
expected_name,
expected_value);
}
void CheckInvalidNameValuePair(std::string valid_part,
std::string invalid_part) {
std::string whole_string = valid_part + invalid_part;
HttpUtil::NameValuePairsIterator valid_parser(valid_part, /*delimiter=*/';');
HttpUtil::NameValuePairsIterator invalid_parser(whole_string,
/*delimiter=*/';');
ASSERT_TRUE(valid_parser.valid());
ASSERT_TRUE(invalid_parser.valid());
// Both parsers should return all the same values until "valid_parser" is
// exhausted.
while (valid_parser.GetNext()) {
ASSERT_TRUE(invalid_parser.GetNext());
ASSERT_TRUE(valid_parser.valid());
ASSERT_TRUE(invalid_parser.valid());
ASSERT_EQ(valid_parser.name(), invalid_parser.name());
ASSERT_EQ(valid_parser.value(), invalid_parser.value());
}
// valid_parser is exhausted and remains 'valid'
ASSERT_TRUE(valid_parser.valid());
// But all data in it should have been cleared.
EXPECT_TRUE(valid_parser.name().empty());
EXPECT_TRUE(valid_parser.value().empty());
EXPECT_TRUE(valid_parser.raw_value().empty());
EXPECT_FALSE(valid_parser.value_is_quoted());
// invalid_parser's corresponding call to GetNext also returns false...
ASSERT_FALSE(invalid_parser.GetNext());
// ...but the parser is in an invalid state.
ASSERT_FALSE(invalid_parser.valid());
// All values in an invalid parser should be cleared.
EXPECT_TRUE(invalid_parser.name().empty());
EXPECT_TRUE(invalid_parser.value().empty());
EXPECT_TRUE(invalid_parser.raw_value().empty());
EXPECT_FALSE(invalid_parser.value_is_quoted());
}
} // namespace
TEST(HttpUtilTest, NameValuePairsIteratorCopyAndAssign) {
std::string data =
"alpha=\"\\\"a\\\"\"; beta=\" b \"; cappa=\"c;\"; delta=\"d\"";
HttpUtil::NameValuePairsIterator parser_a(data, /*delimiter=*/';');
EXPECT_TRUE(parser_a.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser_a, true, true, "alpha", "\"a\""));
HttpUtil::NameValuePairsIterator parser_b(parser_a);
// a and b now point to same location
ASSERT_NO_FATAL_FAILURE(
CheckCurrentNameValuePair(&parser_b, true, "alpha", "\"a\""));
ASSERT_NO_FATAL_FAILURE(
CheckCurrentNameValuePair(&parser_a, true, "alpha", "\"a\""));
// advance a, no effect on b
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser_a, true, true, "beta", " b "));
ASSERT_NO_FATAL_FAILURE(
CheckCurrentNameValuePair(&parser_b, true, "alpha", "\"a\""));
// assign b the current state of a, no effect on a
parser_b = parser_a;
ASSERT_NO_FATAL_FAILURE(
CheckCurrentNameValuePair(&parser_b, true, "beta", " b "));
ASSERT_NO_FATAL_FAILURE(
CheckCurrentNameValuePair(&parser_a, true, "beta", " b "));
// advance b, no effect on a
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser_b, true, true, "cappa", "c;"));
ASSERT_NO_FATAL_FAILURE(
CheckCurrentNameValuePair(&parser_a, true, "beta", " b "));
}
TEST(HttpUtilTest, NameValuePairsIteratorEmptyInput) {
std::string data;
HttpUtil::NameValuePairsIterator parser(data, /*delimiter=*/';');
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(
&parser, false, true, std::string(), std::string()));
}
TEST(HttpUtilTest, NameValuePairsIterator) {
std::string data =
"alpha=1; beta= 2 ;"
"cappa =' 3; foo=';"
"cappa =\" 3; foo=\";"
"delta= \" \\\"4\\\" \"; e= \" '5'\"; e=6;"
"f=\"\\\"\\h\\e\\l\\l\\o\\ \\w\\o\\r\\l\\d\\\"\";"
"g=\"\"; h=\"hello\"";
HttpUtil::NameValuePairsIterator parser(data, /*delimiter=*/';');
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "alpha", "1"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "beta", "2"));
// Single quotes shouldn't be treated as quotes.
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "cappa", "' 3"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "foo", "'"));
// But double quotes should be, and can contain semi-colons and equal signs.
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "cappa", " 3; foo="));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "delta", " \"4\" "));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "e", " '5'"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "e", "6"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "f", "\"hello world\""));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "g", std::string()));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "h", "hello"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(
&parser, false, true, std::string(), std::string()));
}
TEST(HttpUtilTest, NameValuePairsIteratorOptionalValues) {
std::string data = "alpha=1; beta;cappa ; delta; e ; f=1";
// Test that the default parser requires values.
HttpUtil::NameValuePairsIterator default_parser(data, /*delimiter=*/';');
EXPECT_TRUE(default_parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&default_parser, true, true, "alpha", "1"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(&default_parser, false, false,
std::string(), std::string()));
HttpUtil::NameValuePairsIterator values_required_parser(
data, /*delimiter=*/';',
HttpUtil::NameValuePairsIterator::Values::REQUIRED,
HttpUtil::NameValuePairsIterator::Quotes::NOT_STRICT);
EXPECT_TRUE(values_required_parser.valid());
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(&values_required_parser, true,
true, "alpha", "1"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(
&values_required_parser, false, false, std::string(), std::string()));
HttpUtil::NameValuePairsIterator parser(
data, /*delimiter=*/';',
HttpUtil::NameValuePairsIterator::Values::NOT_REQUIRED,
HttpUtil::NameValuePairsIterator::Quotes::NOT_STRICT);
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "alpha", "1"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "beta", std::string()));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "cappa", std::string()));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "delta", std::string()));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "e", std::string()));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "f", "1"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(&parser, false, true,
std::string(), std::string()));
EXPECT_TRUE(parser.valid());
}
TEST(HttpUtilTest, NameValuePairsIteratorIllegalInputs) {
ASSERT_NO_FATAL_FAILURE(CheckInvalidNameValuePair("alpha=1", "; beta"));
ASSERT_NO_FATAL_FAILURE(CheckInvalidNameValuePair(std::string(), "beta"));
ASSERT_NO_FATAL_FAILURE(CheckInvalidNameValuePair("alpha=1", "; \"beta\"=2"));
ASSERT_NO_FATAL_FAILURE(
CheckInvalidNameValuePair(std::string(), "\"beta\"=2"));
ASSERT_NO_FATAL_FAILURE(CheckInvalidNameValuePair("alpha=1", ";beta="));
ASSERT_NO_FATAL_FAILURE(CheckInvalidNameValuePair("alpha=1",
";beta=;cappa=2"));
// According to the spec this is an error, but it doesn't seem appropriate to
// change our behaviour to be less permissive at this time.
// See NameValuePairsIteratorExtraSeparators test
// ASSERT_NO_FATAL_FAILURE(CheckInvalidNameValuePair("alpha=1", ";; beta=2"));
}
// If we are going to support extra separators against the spec, let's just make
// sure they work rationally.
TEST(HttpUtilTest, NameValuePairsIteratorExtraSeparators) {
std::string data = " ; ;;alpha=1; ;; ; beta= 2;cappa=3;;; ; ";
HttpUtil::NameValuePairsIterator parser(data, /*delimiter=*/';');
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "alpha", "1"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "beta", "2"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "cappa", "3"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(
&parser, false, true, std::string(), std::string()));
}
// See comments on the implementation of NameValuePairsIterator::GetNext
// regarding this derogation from the spec.
TEST(HttpUtilTest, NameValuePairsIteratorMissingEndQuote) {
std::string data = "name=\"value";
HttpUtil::NameValuePairsIterator parser(data, /*delimiter=*/';');
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "name", "value"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(
&parser, false, true, std::string(), std::string()));
}
TEST(HttpUtilTest, NameValuePairsIteratorStrictQuotesEscapedEndQuote) {
std::string data = "foo=bar; name=\"value\\\"";
HttpUtil::NameValuePairsIterator parser(
data, /*delimiter=*/';',
HttpUtil::NameValuePairsIterator::Values::REQUIRED,
HttpUtil::NameValuePairsIterator::Quotes::STRICT_QUOTES);
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "foo", "bar"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(&parser, false, false,
std::string(), std::string()));
}
TEST(HttpUtilTest, NameValuePairsIteratorStrictQuotesQuoteInValue) {
std::string data = "foo=\"bar\"; name=\"va\"lue\"";
HttpUtil::NameValuePairsIterator parser(
data, /*delimiter=*/';',
HttpUtil::NameValuePairsIterator::Values::REQUIRED,
HttpUtil::NameValuePairsIterator::Quotes::STRICT_QUOTES);
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "foo", "bar"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(&parser, false, false,
std::string(), std::string()));
}
TEST(HttpUtilTest, NameValuePairsIteratorStrictQuotesMissingEndQuote) {
std::string data = "foo=\"bar\"; name=\"value";
HttpUtil::NameValuePairsIterator parser(
data, /*delimiter=*/';',
HttpUtil::NameValuePairsIterator::Values::REQUIRED,
HttpUtil::NameValuePairsIterator::Quotes::STRICT_QUOTES);
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "foo", "bar"));
ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(&parser, false, false,
std::string(), std::string()));
}
TEST(HttpUtilTest, NameValuePairsIteratorStrictQuotesSingleQuotes) {
std::string data = "foo=\"bar\"; name='value; ok=it'";
HttpUtil::NameValuePairsIterator parser(
data, /*delimiter=*/';',
HttpUtil::NameValuePairsIterator::Values::REQUIRED,
HttpUtil::NameValuePairsIterator::Quotes::STRICT_QUOTES);
EXPECT_TRUE(parser.valid());
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "foo", "bar"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "name", "'value"));
ASSERT_NO_FATAL_FAILURE(
CheckNextNameValuePair(&parser, true, true, "ok", "it'"));
}
TEST(HttpUtilTest, HasValidators) {
const char* const kMissing = "";
const char* const kEtagEmpty = "\"\"";
const char* const kEtagStrong = "\"strong\"";
const char* const kEtagWeak = "W/\"weak\"";
const char* const kLastModified = "Tue, 15 Nov 1994 12:45:26 GMT";
const char* const kLastModifiedInvalid = "invalid";
const HttpVersion v0_9 = HttpVersion(0, 9);
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kMissing, kMissing));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagStrong, kMissing));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagWeak, kMissing));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagEmpty, kMissing));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kMissing, kLastModified));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagStrong, kLastModified));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagWeak, kLastModified));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagEmpty, kLastModified));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kMissing, kLastModifiedInvalid));
EXPECT_FALSE(
HttpUtil::HasValidators(v0_9, kEtagStrong, kLastModifiedInvalid));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagWeak, kLastModifiedInvalid));
EXPECT_FALSE(HttpUtil::HasValidators(v0_9, kEtagEmpty, kLastModifiedInvalid));
const HttpVersion v1_0 = HttpVersion(1, 0);
EXPECT_FALSE(HttpUtil::HasValidators(v1_0, kMissing, kMissing));
EXPECT_FALSE(HttpUtil::HasValidators(v1_0, kEtagStrong, kMissing));
EXPECT_FALSE(HttpUtil::HasValidators(v1_0, kEtagWeak, kMissing));
EXPECT_FALSE(HttpUtil::HasValidators(v1_0, kEtagEmpty, kMissing));
EXPECT_TRUE(HttpUtil::HasValidators(v1_0, kMissing, kLastModified));
EXPECT_TRUE(HttpUtil::HasValidators(v1_0, kEtagStrong, kLastModified));
EXPECT_TRUE(HttpUtil::HasValidators(v1_0, kEtagWeak, kLastModified));
EXPECT_TRUE(HttpUtil::HasValidators(v1_0, kEtagEmpty, kLastModified));
EXPECT_FALSE(HttpUtil::HasValidators(v1_0, kMissing, kLastModifiedInvalid));
EXPECT_FALSE(
HttpUtil::HasValidators(v1_0, kEtagStrong, kLastModifiedInvalid));
EXPECT_FALSE(HttpUtil::HasValidators(v1_0, kEtagWeak, kLastModifiedInvalid));
EXPECT_FALSE(HttpUtil::HasValidators(v1_0, kEtagEmpty, kLastModifiedInvalid));
const HttpVersion v1_1 = HttpVersion(1, 1);
EXPECT_FALSE(HttpUtil::HasValidators(v1_1, kMissing, kMissing));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagStrong, kMissing));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagWeak, kMissing));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagEmpty, kMissing));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kMissing, kLastModified));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagStrong, kLastModified));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagWeak, kLastModified));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagEmpty, kLastModified));
EXPECT_FALSE(HttpUtil::HasValidators(v1_1, kMissing, kLastModifiedInvalid));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagStrong, kLastModifiedInvalid));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagWeak, kLastModifiedInvalid));
EXPECT_TRUE(HttpUtil::HasValidators(v1_1, kEtagEmpty, kLastModifiedInvalid));
}
TEST(HttpUtilTest, IsValidHeaderValue) {
const char* const invalid_values[] = {
"X-Requested-With: chrome${NUL}Sec-Unsafe: injected",
"X-Requested-With: chrome\r\nSec-Unsafe: injected",
"X-Requested-With: chrome\nSec-Unsafe: injected",
"X-Requested-With: chrome\rSec-Unsafe: injected",
};
for (const std::string& value : invalid_values) {
std::string replaced = value;
base::ReplaceSubstringsAfterOffset(&replaced, 0, "${NUL}",
std::string(1, '\0'));
EXPECT_FALSE(HttpUtil::IsValidHeaderValue(replaced)) << replaced;
}
// Check that all characters permitted by RFC7230 3.2.6 are allowed.
std::string allowed = "\t";
for (char c = '\x20'; c < '\x7F'; ++c) {
allowed.append(1, c);
}
for (int c = 0x80; c <= 0xFF; ++c) {
allowed.append(1, static_cast<char>(c));
}
EXPECT_TRUE(HttpUtil::IsValidHeaderValue(allowed));
}
TEST(HttpUtilTest, IsToken) {
EXPECT_TRUE(HttpUtil::IsToken("valid"));
EXPECT_TRUE(HttpUtil::IsToken("!"));
EXPECT_TRUE(HttpUtil::IsToken("~"));
EXPECT_FALSE(HttpUtil::IsToken(""));
EXPECT_FALSE(HttpUtil::IsToken(std::string_view()));
EXPECT_FALSE(HttpUtil::IsToken("hello, world"));
EXPECT_FALSE(HttpUtil::IsToken(" "));
EXPECT_FALSE(HttpUtil::IsToken(std::string_view("\0", 1)));
EXPECT_FALSE(HttpUtil::IsToken("\x01"));
EXPECT_FALSE(HttpUtil::IsToken("\x7F"));
EXPECT_FALSE(HttpUtil::IsToken("\x80"));
EXPECT_FALSE(HttpUtil::IsToken("\xff"));
}
TEST(HttpUtilTest, IsLWS) {
EXPECT_FALSE(HttpUtil::IsLWS('\v'));
EXPECT_FALSE(HttpUtil::IsLWS('\0'));
EXPECT_FALSE(HttpUtil::IsLWS('1'));
EXPECT_FALSE(HttpUtil::IsLWS('a'));
EXPECT_FALSE(HttpUtil::IsLWS('.'));
EXPECT_FALSE(HttpUtil::IsLWS('\n'));
EXPECT_FALSE(HttpUtil::IsLWS('\r'));
EXPECT_TRUE(HttpUtil::IsLWS('\t'));
EXPECT_TRUE(HttpUtil::IsLWS(' '));
}
TEST(HttpUtilTest, TrimLWS) {
const struct {
std::string_view input;
// Input/expected values when calling the TrimLWS() size_t overload.
size_t begin_offset;
size_t end_offset;
size_t expected_begin_offset;
size_t expected_end_offset;
} kTestCases[] = {
/*{"", 0, 0, 0, 0},
{" a ", 0, 3, 1, 2},
{"\ta\t", 0, 3, 1, 2},
{" a ", 1, 2, 1, 2},
{" a ", 1, 3, 1, 2},
{" a ", 0, 2, 1, 2},
{" a ", 0, 1, 1, 1},
{" a ", 2, 3, 3, 3},
{" \x01z\xFF ", 0, 5, 1, 4},
{" a b ", 0, 5, 1, 4},
{"\ra\n", 0, 3, 0, 3},*/
{" \t a \t b\t \t", 0, 11, 3, 8},
};
for (const auto& test : kTestCases) {
SCOPED_TRACE(test.input);
// Test the TrimLWS() overload that uses size_ts as inputs/outputs.
size_t begin_offset = test.begin_offset;
size_t end_offset = test.end_offset;
HttpUtil::TrimLWS(test.input, begin_offset, end_offset);
EXPECT_EQ(test.expected_begin_offset, begin_offset);
EXPECT_EQ(test.expected_end_offset, end_offset);
EXPECT_EQ(test.expected_begin_offset, begin_offset);
EXPECT_EQ(test.expected_end_offset, end_offset);
// Test the TrimLWS() overload that provides a string_view as the
// output.
std::string_view input = test.input.substr(
test.begin_offset, test.end_offset - test.begin_offset);
std::string_view expected = test.input.substr(
test.expected_begin_offset,
test.expected_end_offset - test.expected_begin_offset);
EXPECT_EQ(HttpUtil::TrimLWS(input), expected);
}
}
TEST(HttpUtilTest, IsControlChar) {
EXPECT_FALSE(HttpUtil::IsControlChar('1'));
EXPECT_FALSE(HttpUtil::IsControlChar('a'));
EXPECT_FALSE(HttpUtil::IsControlChar('.'));
EXPECT_FALSE(HttpUtil::IsControlChar('$'));
EXPECT_FALSE(HttpUtil::IsControlChar('\x7E'));
EXPECT_FALSE(HttpUtil::IsControlChar('\x80'));
EXPECT_FALSE(HttpUtil::IsControlChar('\xFF'));
EXPECT_TRUE(HttpUtil::IsControlChar('\0'));
EXPECT_TRUE(HttpUtil::IsControlChar('\v'));
EXPECT_TRUE(HttpUtil::IsControlChar('\n'));
EXPECT_TRUE(HttpUtil::IsControlChar('\r'));
EXPECT_TRUE(HttpUtil::IsControlChar('\t'));
EXPECT_TRUE(HttpUtil::IsControlChar('\x01'));
EXPECT_TRUE(HttpUtil::IsControlChar('\x7F'));
}
TEST(HttpUtilTest, ParseAcceptEncoding) {
const struct {
const char* const value;
const char* const expected;
} tests[] = {
{"", "*"},
{"identity;q=1, *;q=0", "identity"},
{"identity", "identity"},
{"FOO, Bar", "bar|foo|identity"},
{"foo; q=1", "foo|identity"},
{"abc, foo; Q=1.0", "abc|foo|identity"},
{"abc, foo;q= 1.00 , bar", "abc|bar|foo|identity"},
{"abc, foo; q=1.000, bar", "abc|bar|foo|identity"},
{"abc, foo ; q = 0 , bar", "abc|bar|identity"},
{"abc, foo; q=0.0, bar", "abc|bar|identity"},
{"abc, foo; q=0.00, bar", "abc|bar|identity"},
{"abc, foo; q=0.000, bar", "abc|bar|identity"},
{"abc, foo; q=0.001, bar", "abc|bar|foo|identity"},
{"gzip", "gzip|identity|x-gzip"},
{"x-gzip", "gzip|identity|x-gzip"},
{"compress", "compress|identity|x-compress"},
{"x-compress", "compress|identity|x-compress"},
{"x-compress", "compress|identity|x-compress"},
{"foo bar", "INVALID"},
{"foo;", "INVALID"},
{"foo;w=1", "INVALID"},
{"foo;q+1", "INVALID"},
{"foo;q=2", "INVALID"},
{"foo;q=1.001", "INVALID"},
{"foo;q=0.", "INVALID"},
{"foo,\"bar\"", "INVALID"},
};
for (const auto& test : tests) {
std::string value(test.value);
std::string reformatted;
std::set<std::string> allowed_encodings;
if (!HttpUtil::ParseAcceptEncoding(value, &allowed_encodings)) {
reformatted = "INVALID";
} else {
std::vector<std::string> encodings_list;
for (auto const& encoding : allowed_encodings)
encodings_list.push_back(encoding);
reformatted = base::JoinString(encodings_list, "|");
}
EXPECT_STREQ(test.expected, reformatted.c_str())
<< "value=\"" << value << "\"";
}
}
TEST(HttpUtilTest, ParseContentEncoding) {
const struct {
const char* const value;
const char* const expected;
} tests[] = {
{"", ""},
{"identity;q=1, *;q=0", "INVALID"},
{"identity", "identity"},
{"FOO, zergli , Bar", "bar|foo|zergli"},
{"foo, *", "INVALID"},
{"foo,\"bar\"", "INVALID"},
};
for (const auto& test : tests) {
std::string value(test.value);
std::string reformatted;
std::set<std::string> used_encodings;
if (!HttpUtil::ParseContentEncoding(value, &used_encodings)) {
reformatted = "INVALID";
} else {
std::vector<std::string> encodings_list;
for (auto const& encoding : used_encodings)
encodings_list.push_back(encoding);
reformatted = base::JoinString(encodings_list, "|");
}
EXPECT_STREQ(test.expected, reformatted.c_str())
<< "value=\"" << value << "\"";
}
}
// Test the expansion of the Language List.
TEST(HttpUtilTest, ExpandLanguageList) {
EXPECT_EQ("", HttpUtil::ExpandLanguageList(""));
EXPECT_EQ("en-US,en", HttpUtil::ExpandLanguageList("en-US"));
EXPECT_EQ("fr", HttpUtil::ExpandLanguageList("fr"));
// The base language is added after all regional codes...
EXPECT_EQ("en-US,en-CA,en", HttpUtil::ExpandLanguageList("en-US,en-CA"));
// ... but before other language families.
EXPECT_EQ("en-US,en-CA,en,fr",
HttpUtil::ExpandLanguageList("en-US,en-CA,fr"));
EXPECT_EQ("en-US,en-CA,en,fr,en-AU",
HttpUtil::ExpandLanguageList("en-US,en-CA,fr,en-AU"));
EXPECT_EQ("en-US,en-CA,en,fr-CA,fr",
HttpUtil::ExpandLanguageList("en-US,en-CA,fr-CA"));
// Add a base language even if it's already in the list.
EXPECT_EQ("en-US,en,fr-CA,fr,it,es-AR,es,it-IT",
HttpUtil::ExpandLanguageList("en-US,fr-CA,it,fr,es-AR,it-IT"));
// Trims a whitespace.
EXPECT_EQ("en-US,en,fr", HttpUtil::ExpandLanguageList("en-US, fr"));
// Do not expand the single character subtag 'x' as a language.
EXPECT_EQ("x-private-agreement-subtags",
HttpUtil::ExpandLanguageList("x-private-agreement-subtags"));
// Do not expand the single character subtag 'i' as a language.
EXPECT_EQ("i-klingon", HttpUtil::ExpandLanguageList("i-klingon"));
}
} // namespace net
|