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 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
|
From: =?utf-8?q?Bastien_Roucari=C3=A8s?= <rouca@debian.org>
Date: Sun, 28 Jul 2024 23:41:33 +0000
Subject: Avoid parted
forwarded: not-needed
---
minipass-fetch/test/fixtures/server.js | 398 -------
minipass-fetch/test/index.js | 1968 --------------------------------
2 files changed, 2366 deletions(-)
delete mode 100644 minipass-fetch/test/fixtures/server.js
delete mode 100644 minipass-fetch/test/index.js
diff --git a/minipass-fetch/test/fixtures/server.js b/minipass-fetch/test/fixtures/server.js
deleted file mode 100644
index c2215c5..0000000
--- a/minipass-fetch/test/fixtures/server.js
+++ /dev/null
@@ -1,398 +0,0 @@
-const http = require('http')
-const zlib = require('minizlib')
-const { multipart: Multipart } = require('parted')
-const convert = require('encoding').convert
-
-class TestServer {
- constructor () {
- this.server = http.createServer((req, res) => this.router(req, res))
- this.port = 30000 + (+process.env.TAP_CHILD_ID || 1)
- this.hostname = 'localhost'
- // node 8 default keepalive timeout is 5000ms
- // make it shorter here as we want to close server
- // quickly at the end of tests
- this.server.keepAliveTimeout = 1000
- this.server.on('error', err => console.log(err.stack))
- this.server.on('connection', socket => socket.setTimeout(1500))
- }
-
- start (cb) {
- this.server.listen(this.port, this.hostname, cb)
- }
-
- stop (cb) {
- this.server.close(cb)
- }
-
- router (req, res) {
- const p = req.url
-
- if (p === '/hello') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.end('world')
- }
-
- if (p === '/plain') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.end('text')
- }
-
- if (p === '/options') {
- res.statusCode = 200
- res.setHeader('Allow', 'GET, HEAD, OPTIONS')
- res.end('hello world')
- }
-
- if (p === '/html') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.end('<html></html>')
- }
-
- if (p === '/json') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'application/json')
- res.end(JSON.stringify({ name: 'value' }))
- }
-
- if (p === '/gzip') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.setHeader('Content-Encoding', 'gzip')
- // eslint-disable-next-line promise/catch-or-return
- new zlib.Gzip().end('hello world').concat().then(buf => res.end(buf))
- }
-
- if (p === '/gzip-truncated') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.setHeader('Content-Encoding', 'gzip')
- // eslint-disable-next-line promise/catch-or-return
- new zlib.Gzip().end('hello world').concat().then(buf =>
- res.end(buf.slice(0, buf.length - 8)))
- }
-
- if (p === '/deflate') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.setHeader('Content-Encoding', 'deflate')
- // eslint-disable-next-line promise/catch-or-return
- new zlib.Deflate().end('hello world').concat().then(buf => res.end(buf))
- }
-
- if (p === '/brotli') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.setHeader('Content-Encoding', 'br')
- // pre-compressed 'hello world', in-lined here so tests will run when the
- // client doesn't support brotli
- const buf = Buffer.from([
- 0x0b, 0x05, 0x80, 0x68, 0x65, 0x6c, 0x6c,
- 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x03,
- ])
- res.end(buf)
- }
-
- if (p === '/deflate-raw') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.setHeader('Content-Encoding', 'deflate')
- // eslint-disable-next-line promise/catch-or-return
- new zlib.DeflateRaw().end('hello world').concat().then(buf => res.end(buf))
- }
-
- if (p === '/sdch') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.setHeader('Content-Encoding', 'sdch')
- res.end('fake sdch string')
- }
-
- if (p === '/invalid-content-encoding') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.setHeader('Content-Encoding', 'gzip')
- res.end('fake gzip string')
- }
-
- if (p === '/timeout') {
- setTimeout(() => {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.end('text')
- }, 1000)
- }
-
- if (p === '/slow') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.write('test')
- setTimeout(() => res.end('test'), 1000)
- }
-
- if (p === '/cookie') {
- res.statusCode = 200
- res.setHeader('Set-Cookie', ['a=1', 'b=1'])
- res.end('cookie')
- }
-
- if (p === '/size/chunk') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- setTimeout(() => res.write('test'), 10)
- setTimeout(() => res.end('test'), 20)
- }
-
- if (p === '/size/long') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain')
- res.end('testtest')
- }
-
- if (p === '/encoding/gbk') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.end(convert('<meta charset="gbk"><div>中文</div>', 'gbk'))
- }
-
- if (p === '/encoding/gb2312') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.end(convert('<meta http-equiv="Content-Type" content="text/html; charset=gb2312">' +
- '<div>中文</div>', 'gb2312'))
- }
-
- if (p === '/encoding/gb2312-reverse') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.end(convert('<meta content="text/html; charset=gb2312" http-equiv="Content-Type">' +
- '<div>中文</div>', 'gb2312'))
- }
-
- if (p === '/encoding/shift-jis') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html; charset=Shift-JIS')
- res.end(convert('<div>日本語</div>', 'Shift_JIS'))
- }
-
- if (p === '/encoding/euc-jp') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/xml')
- res.end(convert('<?xml version="1.0" encoding="EUC-JP"?><title>日本語</title>', 'EUC-JP'))
- }
-
- if (p === '/encoding/utf8') {
- res.statusCode = 200
- res.end('中文')
- }
-
- if (p === '/encoding/order1') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'charset=gbk; text/plain')
- res.end(convert('中文', 'gbk'))
- }
-
- if (p === '/encoding/order2') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/plain; charset=gbk; qs=1')
- res.end(convert('中文', 'gbk'))
- }
-
- if (p === '/encoding/chunked') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.setHeader('Transfer-Encoding', 'chunked')
- res.write('a'.repeat(10))
- res.end(convert('<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />' +
- '<div>日本語</div>', 'Shift_JIS'))
- }
-
- if (p === '/encoding/invalid') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.setHeader('Transfer-Encoding', 'chunked')
- res.write('a'.repeat(1200))
- res.end(convert('中文', 'gbk'))
- }
-
- if (p === '/redirect/301') {
- res.statusCode = 301
- res.setHeader('Location', '/inspect')
- res.end()
- }
-
- if (p === '/redirect/301/invalid') {
- res.statusCode = 301
- res.setHeader('Location', '//super:invalid:url%/')
- res.end()
- }
-
- if (p === '/redirect/302') {
- res.statusCode = 302
- res.setHeader('Location', '/inspect')
- res.end()
- }
-
- if (p === '/redirect/303') {
- res.statusCode = 303
- res.setHeader('Location', '/inspect')
- res.end()
- }
-
- if (p === '/redirect/307') {
- res.statusCode = 307
- res.setHeader('Location', '/inspect')
- res.end()
- }
-
- if (p === '/redirect/308') {
- res.statusCode = 308
- res.setHeader('Location', '/inspect')
- res.end()
- }
-
- if (p === '/redirect/chain') {
- res.statusCode = 301
- res.setHeader('Location', '/redirect/301')
- res.end()
- }
-
- if (p === '/redirect/no-location') {
- res.statusCode = 301
- res.end()
- }
-
- if (p === '/redirect/slow') {
- res.statusCode = 301
- res.setHeader('Location', '/redirect/301')
- setTimeout(() => res.end(), 1000)
- }
-
- if (p === '/redirect/slow-chain') {
- res.statusCode = 301
- res.setHeader('Location', '/redirect/slow')
- setTimeout(() => res.end(), 10)
- }
-
- if (p === '/redirect/slow-stream') {
- res.statusCode = 301
- res.setHeader('Location', '/slow')
- res.end()
- }
-
- if (p === '/error/400') {
- res.statusCode = 400
- res.setHeader('Content-Type', 'text/plain')
- res.end('client error')
- }
-
- if (p === '/error/404') {
- res.statusCode = 404
- res.setHeader('Content-Encoding', 'gzip')
- res.end()
- }
-
- if (p === '/error/500') {
- res.statusCode = 500
- res.setHeader('Content-Type', 'text/plain')
- res.end('server error')
- }
-
- if (p === '/error/reset') {
- res.destroy()
- }
-
- if (p === '/error/json') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'application/json')
- res.end('invalid json')
- }
-
- if (p === '/no-content') {
- res.statusCode = 204
- res.end()
- }
-
- if (p === '/no-content/gzip') {
- res.statusCode = 204
- res.setHeader('Content-Encoding', 'gzip')
- res.end()
- }
-
- if (p === '/no-content/brotli') {
- res.statusCode = 204
- res.setHeader('Content-Encoding', 'br')
- res.end()
- }
-
- if (p === '/not-modified') {
- res.statusCode = 304
- res.end()
- }
-
- if (p === '/not-modified/gzip') {
- res.statusCode = 304
- res.setHeader('Content-Encoding', 'gzip')
- res.end()
- }
-
- if (p === '/inspect') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'application/json')
- let body = ''
- req.on('data', c => body += c)
- req.on('end', () => res.end(JSON.stringify({
- method: req.method,
- url: req.url,
- headers: req.headers,
- body,
- })))
- }
-
- if (p === '/multipart') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'application/json')
- // the path option passed to the Multipart constructor cannot be an
- // absolute path in Windows, we set it here manually because the default
- // provided by 'parsed' is an absolute path
- // ref: https://github.com/chjj/parsed/issues/10
- const parser = new Multipart(req.headers['content-type'], { path: './' })
- let body = ''
- parser.on('part', (field, part) => body += field + '=' + part)
- parser.on('end', () => res.end(JSON.stringify({
- method: req.method,
- url: req.url,
- headers: req.headers,
- body: body,
- })))
- req.pipe(parser)
- }
-
- if (p === '/trailers') {
- res.statusCode = 200
- res.setHeader('Transfer-Encoding', 'chunked')
- res.setHeader('Trailer', 'X-Node-Fetch')
- res.write('Body of the response')
- res.addTrailers({ 'X-Node-Fetch': 'hello world!' })
- res.end()
- }
-
- if (p === '/host-redirect') {
- if (req.headers.host !== `localhost:${this.port}`) {
- res.setHeader('location', `http://localhost:${this.port}/host-redirect`)
- res.statusCode = 302
- }
- res.end(`http://${req.headers.host}/host-redirect`)
- }
- }
-}
-
-module.exports = TestServer
-
-if (require.main === module) {
- const server = new TestServer()
- server.start(() =>
- console.log(`Server started listening at port ${server.port}`))
-}
diff --git a/minipass-fetch/test/index.js b/minipass-fetch/test/index.js
deleted file mode 100644
index ab92af2..0000000
--- a/minipass-fetch/test/index.js
+++ /dev/null
@@ -1,1968 +0,0 @@
-'use strict'
-const t = require('tap')
-const TestServer = require('./fixtures/server.js')
-const fetch = require('../lib/index.js')
-//const stringToArrayBuffer = require('string-to-arraybuffer')
-const URLSearchParamsPolyfill = require('@ungap/url-search-params')
-const { AbortError, FetchError, Headers, Request, Response } = fetch
-const AbortErrorOrig = require('../lib/abort-error.js')
-const FetchErrorOrig = require('../lib/fetch-error.js')
-const HeadersOrig = require('../lib/headers.js')
-const { createHeadersLenient } = HeadersOrig
-const RequestOrig = require('../lib/request.js')
-const ResponseOrig = require('../lib/response.js')
-const Body = require('../lib/body.js')
-const { getTotalBytes, extractContentType } = Body
-const Blob = require('../lib/blob.js')
-const realZlib = require('zlib')
-const { lookup } = require('dns')
-const { promisify } = require('util')
-const supportToString = ({
- [Symbol.toStringTag]: 'z',
-}).toString() === '[object z]'
-const FormData = require('form-data')
-const fs = require('fs')
-const http = require('http')
-// use of url.parse here is intentional and for coverage purposes
-// eslint-disable-next-line node/no-deprecated-api
-const { parse: parseURL, URLSearchParams } = require('url')
-const nock = require('nock')
-
-const vm = require('vm')
-const {
- ArrayBuffer: VMArrayBuffer,
- Uint8Array: VMUint8Array,
-} = vm.runInNewContext('this')
-
-const { spawn } = require('child_process')
-const path = require('path')
-
-const { Minipass } = require('minipass')
-const supportStreamDestroy = 'destroy' in Minipass.prototype
-
-const { AbortController } = require('abortcontroller-polyfill/dist/abortcontroller')
-const AbortController2 = require('abort-controller')
-
-const local = new TestServer()
-const base = `http://${local.hostname}:${local.port}/`
-
-t.Test.prototype.addAssert('contain', 2, function (list, key, m, e) {
- m = m || 'expected item to be contained in list'
- e.found = list
- e.wanted = key
- return this.ok(list.indexOf(key) !== -1, m, e)
-})
-t.Test.prototype.addAssert('notContain', 2, function (list, key, m, e) {
- m = m || 'expected item to not be contained in list'
- e.found = list
- e.wanted = key
- return this.notOk(list.indexOf(key) !== -1, m, e)
-})
-
-const streamToPromise = (stream, dataHandler) =>
- new Promise((resolve, reject) => {
- stream.on('data', (...args) =>
- Promise.resolve()
- .then(() => dataHandler(...args))
- .catch(reject))
-
- stream.on('end', resolve)
- stream.on('error', reject)
- })
-
-t.test('start server', t => {
- local.start(t.end)
- t.parent.teardown(() => local.stop())
-})
-
-t.test('return a promise', t => {
- const p = fetch(`${base}hello`)
- t.type(p, Promise)
- t.equal(typeof p.then, 'function')
- t.end()
-})
-
-t.test('expose AbortError, FetchError, Headers, Response and Request constructors', t => {
- t.equal(AbortError, AbortErrorOrig)
- t.equal(FetchError, FetchErrorOrig)
- t.equal(Headers, HeadersOrig)
- t.equal(Response, ResponseOrig)
- t.equal(Request, RequestOrig)
- t.end()
-})
-
-t.test('support proper toString output', { skip: !supportToString }, t => {
- t.equal(new Headers().toString(), '[object Headers]')
- t.equal(new Response().toString(), '[object Response]')
- t.equal(new Request('http://localhost:30000').toString(), '[object Request]')
- t.end()
-})
-
-t.test('reject with error if url is protocol relative', t =>
- t.rejects(fetch('//example.com/'), {
- code: 'ERR_INVALID_URL',
- name: 'TypeError',
- }))
-
-t.test('reject if url is relative path', t =>
- t.rejects(fetch('/some/path'), {
- code: 'ERR_INVALID_URL',
- name: 'TypeError',
- }))
-
-t.test('reject if protocol unsupported', t =>
- t.rejects(fetch('ftp://example.com/'), new TypeError(
- 'Only HTTP(S) protocols are supported')))
-
-t.test('reject with error on network failure', t =>
- t.rejects(fetch('http://localhost:55555/'), {
- name: 'FetchError',
- code: 'ECONNREFUSED',
- errno: 'ECONNREFUSED',
- type: 'system',
- }))
-
-t.test('resolve into response', async t => {
- const res = await fetch(`${base}hello`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = await res.text()
- t.equal(res.bodyUsed, true)
- t.equal(result, 'world')
-})
-
-t.test('accept html response (like plain text)', async t => {
- const res = await fetch(`${base}html`)
- t.equal(res.headers.get('content-type'), 'text/html')
- const result = await res.text()
- t.equal(res.bodyUsed, true)
- t.equal(result, '<html></html>')
-})
-
-t.test('accept json response', async t => {
- const res = await fetch(`${base}json`)
- t.equal(res.headers.get('content-type'), 'application/json')
- const result = await res.json()
- t.equal(res.bodyUsed, true)
- t.strictSame(result, { name: 'value' })
-})
-
-t.test('send request with custom hedaers', async t => {
- const res = await fetch(`${base}inspect`, {
- headers: { 'x-custom-header': 'abc' },
- })
- const json = await res.json()
- t.equal(json.headers['x-custom-header'], 'abc')
-})
-
-t.test('accept headers instance', async t => {
- const res = await fetch(`${base}inspect`, {
- headers: new Headers({ 'x-custom-header': 'abc' }),
- })
- const json = await res.json()
- t.equal(json.headers['x-custom-header'], 'abc')
-})
-
-t.test('accept custom host header', async t => {
- const res = await fetch(`${base}inspect`, {
- headers: {
- host: 'example.com',
- },
- })
- const json = await res.json()
- t.equal(json.headers.host, 'example.com')
-})
-
-t.test('accept custom HoSt header', async t => {
- const res = await fetch(`${base}inspect`, {
- headers: {
- HoSt: 'example.com',
- },
- })
- const json = await res.json()
- t.equal(json.headers.host, 'example.com')
-})
-
-t.test('follow redirects', async t => {
- const codes = [301, 302, 303, 307, 308, 'chain']
- t.plan(codes.length)
- for (const code of codes) {
- t.test(code, async t => {
- const res = await fetch(`${base}redirect/${code}`)
- t.equal(res.url, `${base}inspect`)
- t.equal(res.status, 200)
- t.equal(res.ok, true)
- })
- }
-})
-
-t.test('redirect to different host strips headers', async (t) => {
- nock.disableNetConnect()
- t.teardown(() => {
- nock.cleanAll()
- nock.enableNetConnect()
- })
-
- const first = nock('http://x.y', {
- reqheaders: {
- authorization: 'totally-authed-request',
- cookie: 'fake-cookie',
- },
- })
- .get('/')
- .reply(301, null, { location: 'http://a.b' })
-
- const second = nock('http://a.b', {
- badheaders: ['authorization', 'cookie'],
- })
- .get('/')
- .reply(200)
-
- const res = await fetch('http://x.y', {
- headers: {
- authorization: 'totally-authed-request',
- cookie: 'fake-cookie',
- },
- })
- await res.text() // drain the response stream
-
- t.ok(first.isDone(), 'initial request made')
- t.ok(second.isDone(), 'redirect followed')
- t.equal(res.status, 200)
- t.ok(res.ok)
-})
-
-t.test('follow POST request redirect with GET', async t => {
- for (const code of [301, 302]) {
- t.test(code, async t => {
- const url = `${base}redirect/${code}`
- const opts = {
- method: 'POST',
- body: 'a=1',
- }
- const res = await fetch(url, opts)
- t.equal(res.url, `${base}inspect`)
- t.equal(res.status, 200)
- const result = await res.json()
- t.equal(result.method, 'GET')
- t.equal(result.body, '')
- })
- }
-})
-
-t.test('follow PATCH request redirect with PATCH', async t => {
- const codes = [301, 302, 307]
- t.plan(codes.length)
- for (const code of codes) {
- t.test(code, async t => {
- const url = `${base}redirect/${code}`
- const opts = {
- method: 'PATCH',
- body: 'a=1',
- }
- const res = await fetch(url, opts)
- t.equal(res.url, `${base}inspect`)
- t.equal(res.status, 200)
- const result = await res.json()
- t.equal(result.method, 'PATCH')
- t.equal(result.body, 'a=1')
- })
- }
-})
-
-t.test('no follow non-GET redirect if body is readable stream', async t => {
- const url = `${base}redirect/307`
- const body = new Minipass()
- body.pause()
- body.end('a=1')
- setTimeout(() => body.resume(), 100)
- const opts = {
- method: 'PATCH',
- body,
- }
- await t.rejects(fetch(url, opts), {
- name: 'FetchError',
- type: 'unsupported-redirect',
- })
-})
-
-t.test('obey maximum redirect, reject case', async t => {
- const url = `${base}redirect/chain`
- const opts = {
- follow: 1,
- }
- await t.rejects(fetch(url, opts), {
- name: 'FetchError',
- type: 'max-redirect',
- })
-})
-
-t.test('obey redirect chain, resolve case', async t => {
- const url = `${base}redirect/chain`
- const opts = {
- follow: 2,
- }
- const res = await fetch(url, opts)
- t.equal(res.url, `${base}inspect`)
- t.equal(res.status, 200)
-})
-
-t.test('allow not following redirect', async t => {
- const url = `${base}redirect/301`
- const opts = {
- follow: 0,
- }
- await t.rejects(fetch(url, opts), {
- name: 'FetchError',
- type: 'max-redirect',
- })
-})
-
-t.test('redirect mode, manual flag', async t => {
- const url = `${base}redirect/301`
- const opts = {
- redirect: 'manual',
- }
- const res = await fetch(url, opts)
- t.equal(res.url, url)
- t.equal(res.status, 301)
- t.equal(res.headers.get('location'), `${base}inspect`)
-})
-
-t.test('redirect mode, error flag', async t => {
- const url = `${base}redirect/301`
- const opts = {
- redirect: 'error',
- }
- await t.rejects(fetch(url, opts), {
- name: 'FetchError',
- type: 'no-redirect',
- })
-})
-
-t.test('redirect mode, manual flag when there is no redirect', async t => {
- const url = `${base}hello`
- const opts = {
- redirect: 'manual',
- }
- const res = await fetch(url, opts)
- t.equal(res.url, url)
- t.equal(res.status, 200)
- t.equal(res.headers.get('location'), null)
-})
-
-t.test('redirect code 301 and keep existing headers', async t => {
- const url = `${base}redirect/301`
- const opts = {
- headers: new Headers({ 'x-custom-header': 'abc' }),
- }
- const res = await fetch(url, opts)
- t.equal(res.url, `${base}inspect`)
- const json = await res.json()
- t.equal(json.headers['x-custom-header'], 'abc')
-})
-
-t.test('treat broken redirect as ordinary response (follow)', async t => {
- const url = `${base}redirect/no-location`
- const res = await fetch(url)
- t.equal(res.url, url)
- t.equal(res.status, 301)
- t.equal(res.headers.get('location'), null)
-})
-
-t.test('treat broken redirect as ordinary response (manual)', async t => {
- const url = `${base}redirect/no-location`
- const opts = {
- redirect: 'manual',
- }
- const res = await fetch(url, opts)
- t.equal(res.url, url)
- t.equal(res.status, 301)
- t.equal(res.headers.get('location'), null)
-})
-
-t.test('should process an invalid redirect (manual)', async t => {
- const url = `${base}redirect/301/invalid`
- const options = {
- redirect: 'manual',
- }
- const res = await fetch(url, options)
- t.equal(res.url, url)
- t.equal(res.status, 301)
- t.equal(res.headers.get('location'), '//super:invalid:url%/')
-})
-
-t.test('should throw an error on invalid redirect url', async t => {
- const url = `${base}redirect/301/invalid`
- await t.rejects(fetch(url), {
- name: 'FetchError',
- message: 'uri requested responds with an invalid redirect URL: //super:invalid:url%/',
- })
-})
-
-t.test('set redirected property on response when redirect', t =>
- fetch(`${base}redirect/301`).then(res => t.equal(res.redirected, true)))
-
-t.test('no redirected property on response when not redirect', t =>
- fetch(`${base}hello`).then(res => t.equal(res.redirected, false)))
-
-t.test('ignore invalid headers', t => {
- var headers = {
- 'Invalid-Header ': 'abc\r\n',
- 'Invalid-Header-Value': '\x07k\r\n',
- 'Set-Cookie': ['\x07k\r\n', '\x07kk\r\n'],
- }
- headers = createHeadersLenient(headers)
- t.equal(headers['Invalid-Header '], undefined)
- t.equal(headers['Invalid-Header-Value'], undefined)
- t.equal(headers['Set-Cookie'], undefined)
- t.end()
-})
-
-t.test('handle client-error response', async t => {
- const url = `${base}error/400`
- const res = await fetch(url)
- t.equal(res.headers.get('content-type'), 'text/plain')
- t.equal(res.status, 400)
- t.equal(res.statusText, 'Bad Request')
- t.equal(res.ok, false)
- const result = await res.text()
- t.equal(res.bodyUsed, true)
- t.equal(result, 'client error')
-})
-
-t.test('handle server-error response', async t => {
- const url = `${base}error/500`
- const res = await fetch(url)
- t.equal(res.headers.get('content-type'), 'text/plain')
- t.equal(res.status, 500)
- t.equal(res.statusText, 'Internal Server Error')
- t.equal(res.ok, false)
- const result = await res.text()
- t.equal(res.bodyUsed, true)
- t.equal(result, 'server error')
-})
-
-t.test('handle network-error response', async t => {
- await t.rejects(fetch(`${base}error/reset`), {
- name: 'FetchError',
- code: 'ECONNRESET',
- })
-})
-
-t.test('handle DNS-error response', async t => {
- await t.rejects(fetch('http://domain.invalid'), {
- name: 'FetchError',
- // this error depends on the platform and dns server in use,
- // but it should be one of these two codes
- code: /^(ENOTFOUND|EAI_AGAIN)$/,
- })
-})
-
-t.test('reject invalid json response', async t => {
- const res = await fetch(`${base}error/json`)
- t.equal(res.headers.get('content-type'), 'application/json')
- await t.rejects(res.json(), {
- name: 'FetchError',
- type: 'invalid-json',
- })
-})
-
-t.test('reject invalid json response', async t => {
- const res = await fetch(`${base}error/json`)
- t.equal(res.headers.get('content-type'), 'application/json')
- await t.rejects(res.json(), {
- name: 'FetchError',
- type: 'invalid-json',
- })
-})
-
-t.test('handle no content response', async t => {
- const res = await fetch(`${base}no-content`)
- t.equal(res.status, 204)
- t.equal(res.statusText, 'No Content')
- t.equal(res.ok, true)
- const result = await res.text()
- t.equal(result, '')
-})
-
-t.test('reject parsing no content response as json', async t => {
- const res = await fetch(`${base}no-content`)
- t.equal(res.status, 204)
- t.equal(res.statusText, 'No Content')
- t.equal(res.ok, true)
- await t.rejects(res.json(), {
- name: 'FetchError',
- type: 'invalid-json',
- })
-})
-
-t.test('handle no content response with gzip encoding', async t => {
- const res = await fetch(`${base}no-content/gzip`)
- t.equal(res.status, 204)
- t.equal(res.statusText, 'No Content')
- t.equal(res.headers.get('content-encoding'), 'gzip')
- t.equal(res.ok, true)
- const result = await res.text()
- t.equal(result, '')
-})
-
-t.test('handle not modified response', async t => {
- const res = await fetch(`${base}not-modified`)
- t.equal(res.status, 304)
- t.equal(res.statusText, 'Not Modified')
- t.equal(res.ok, false)
- const result = await res.text()
- t.equal(result, '')
-})
-
-t.test('handle not modified response with gzip encoding', async t => {
- const res = await fetch(`${base}not-modified/gzip`)
- t.equal(res.status, 304)
- t.equal(res.statusText, 'Not Modified')
- t.equal(res.headers.get('content-encoding'), 'gzip')
- t.equal(res.ok, false)
- const result = await res.text()
- t.equal(result, '')
-})
-
-t.test('decompress gzip response', async t => {
- const res = await fetch(`${base}gzip`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = await res.text()
- t.equal(result, 'hello world')
-})
-
-t.test('decompress slightly invalid gzip response', async t => {
- const res = await fetch(`${base}gzip-truncated`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = await res.text()
- t.equal(result, 'hello world')
-})
-
-t.test('decompress deflate response', async t => {
- const res = await fetch(`${base}deflate`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = await res.text()
- t.equal(result, 'hello world')
-})
-
-t.test('decompress deflate raw response from old apache server', async t => {
- const res = await fetch(`${base}deflate-raw`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = await res.text()
- t.equal(result, 'hello world')
-})
-
-t.test('decompress brotli response', async t => {
- // if the node core zlib doesn't export brotli functions, we'll end up
- // rejecting the request with an error that comes from minizlib, assert
- // that here
- if (typeof realZlib.BrotliCompress !== 'function') {
- return t.rejects(fetch(`${base}brotli`), {
- message: 'Brotli is not supported in this version of Node.js',
- }, 'rejects the promise')
- }
-
- const res = await fetch(`${base}brotli`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = await res.text()
- t.equal(result, 'hello world')
-})
-
-t.test('handle no content response with brotli encoding', async t => {
- const res = await fetch(`${base}no-content/brotli`)
- t.equal(res.status, 204)
- t.equal(res.statusText, 'No Content')
- t.equal(res.headers.get('content-encoding'), 'br')
- t.equal(res.ok, true)
- const result = await res.text()
- t.equal(result, '')
-})
-
-t.test('skip decompression if unsupported', async t => {
- const res = await fetch(`${base}sdch`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = await res.text()
- t.equal(result, 'fake sdch string')
-})
-
-t.test('reject if response compression is invalid', async t => {
- const res = await fetch(`${base}invalid-content-encoding`)
- t.equal(res.headers.get('content-type'), 'text/plain')
- await t.rejects(res.text(), {
- name: 'FetchError',
- code: 'Z_DATA_ERROR',
- })
-})
-
-t.test('handle errors on the body stream even if it is not used', async t => {
- const res = await fetch(`${base}invalid-content-encoding`)
- t.equal(res.status, 200)
- // Wait a few ms to see if a uncaught error occurs
- await promisify(setTimeout)(20)
-})
-
-t.test('collect handled errors on body stream, reject if used later', async t => {
- const delay = value => new Promise(resolve =>
- setTimeout(() => resolve(value), 20))
-
- const res = await fetch(`${base}invalid-content-encoding`).then(delay)
- const delayed = await delay(res)
- t.equal(delayed.headers.get('content-type'), 'text/plain')
- t.rejects(delayed.text(), {
- name: 'FetchError',
- code: 'Z_DATA_ERROR',
- })
-})
-
-t.test('allow disabling auto decompression', t =>
- fetch(`${base}gzip`, { compress: false }).then(res => {
- t.equal(res.headers.get('content-type'), 'text/plain')
- return res.text().then(result => t.not(result, 'hello world'))
- }))
-
-t.test('do not overwrite accept-encoding when auto decompression', t =>
- fetch(`${base}inspect`, {
- compress: true,
- headers: {
- 'Accept-Encoding': 'gzip',
- },
- })
- .then(res => res.json())
- .then(res => t.equal(res.headers['accept-encoding'], 'gzip')))
-
-t.test('allow custom timeout', t => {
- return t.rejects(fetch(`${base}timeout`, { timeout: 20 }), {
- name: 'FetchError',
- type: 'request-timeout',
- })
-})
-
-t.test('allow custom timeout on response body', t => {
- return fetch(`${base}slow`, { timeout: 50 }).then(res => {
- t.equal(res.ok, true)
- return t.rejects(res.text(), {
- name: 'FetchError',
- type: 'body-timeout',
- })
- })
-})
-
-t.test('allow custom timeout on redirected requests', t =>
- t.rejects(fetch(`${base}redirect/slow-chain`, { timeout: 50 }), {
- name: 'FetchError',
- type: 'request-timeout',
- }))
-
-t.test('clear internal timeout on fetch response', { timeout: 2000 }, t => {
- const args = ['-e', `require('./')('${base}hello', { timeout: 10000 })`]
- spawn(process.execPath, args, { cwd: path.resolve(__dirname, '..') })
- .on('close', (code, signal) => {
- t.equal(code, 0)
- t.equal(signal, null)
- t.end()
- })
-})
-
-t.test('clear internal timeout on fetch redirect', { timeout: 2000 }, t => {
- const args = ['-e', `require('./')('${base}redirect/301', { timeout: 10000 })`]
- spawn(process.execPath, args, { cwd: path.resolve(__dirname, '..') })
- .on('close', (code, signal) => {
- t.equal(code, 0)
- t.equal(signal, null)
- t.end()
- })
-})
-
-t.test('clear internal timeout on fetch error', { timeout: 2000 }, t => {
- const args = ['-e', `require('./')('${base}error/reset', { timeout: 10000 })`]
- // note: promise rejections started setting exit status code in node 15
- const stderr = []
- spawn(process.execPath, args, { cwd: path.resolve(__dirname, '..') })
- .on('close', (code, signal) => {
- t.match(Buffer.concat(stderr).toString(), 'FetchError')
- t.equal(signal, null)
- t.end()
- })
- .stderr.on('data', c => stderr.push(c))
-})
-
-t.test('request cancellation with signal', { timeout: 500 }, t => {
- const controller = new AbortController()
- const controller2 = new AbortController2()
-
- const fetches = [
- fetch(`${base}timeout`, { signal: controller.signal }),
- fetch(`${base}timeout`, { signal: controller2.signal }),
- fetch(
- `${base}timeout`,
- {
- method: 'POST',
- signal: controller.signal,
- headers: {
- 'Content-Type': 'application/json',
- body: JSON.stringify({ hello: 'world' }),
- },
- }
- ),
- ]
- setTimeout(() => {
- controller.abort()
- controller2.abort()
- }, 100)
-
- return Promise.all(fetches.map(fetched => t.rejects(fetched, {
- name: 'AbortError',
- type: 'aborted',
- })))
-})
-
-t.test('reject immediately if signal already aborted', t => {
- const url = `${base}timeout`
- const controller = new AbortController()
- const opts = {
- signal: controller.signal,
- }
- controller.abort()
- const fetched = fetch(url, opts)
- return t.rejects(fetched, {
- name: 'AbortError',
- type: 'aborted',
- })
-})
-
-t.test('clear internal timeout when cancelled with AbortSignal', { timeout: 2000 }, t => {
- const script = `
-const ACP = require('abortcontroller-polyfill/dist/cjs-ponyfill')
-var AbortController = ACP.AbortController
-var controller = new AbortController()
-require('./')(
-'${base}timeout',
-{ signal: controller.signal, timeout: 10000 }
-)
-setTimeout(function () { controller.abort(); }, 20)
- `
- // note: promise rejections started setting exit status code in node 15
- const stderr = []
- spawn('node', ['-e', script], { cwd: path.resolve(__dirname, '..') })
- .on('close', (code, signal) => {
- t.match(Buffer.concat(stderr).toString(), 'AbortError')
- t.equal(signal, null)
- t.end()
- })
- .stderr.on('data', c => stderr.push(c))
-})
-
-t.test('remove internal AbortSignal listener when request aborted', t => {
- const controller = new AbortController()
- const { signal } = controller
- const promise = fetch(
- `${base}timeout`,
- { signal }
- )
- const result = t.rejects(promise, { name: 'AbortError' })
- .then(() => t.equal(signal.listeners.abort.length, 0))
- controller.abort()
- return result
-})
-
-t.test('allow redirects to be aborted', t => {
- const abortController = new AbortController()
- const request = new Request(`${base}redirect/slow`, {
- signal: abortController.signal,
- })
- setTimeout(() => abortController.abort(), 20)
- return t.rejects(fetch(request), { name: 'AbortError' })
-})
-
-t.test('allow redirected response body to be aborted', t => {
- const abortController = new AbortController()
- const request = new Request(`${base}redirect/slow-stream`, {
- signal: abortController.signal,
- })
- return t.rejects(fetch(request).then(res => {
- t.equal(res.headers.get('content-type'), 'text/plain')
- const result = res.text()
- abortController.abort()
- return result
- }), { name: 'AbortError' })
-})
-
-t.test('remove internal AbortSignal listener when req/res complete', t => {
- const controller = new AbortController()
- const { signal } = controller
- const fetchHtml = fetch(`${base}html`, { signal })
- .then(res => res.text())
- const fetchResponseError = fetch(`${base}error/reset`, { signal })
- const fetchRedirect = fetch(`${base}redirect/301`, { signal })
- .then(res => res.json())
- return Promise.all([
- t.resolves(fetchHtml.then(result => t.equal(result, '<html></html>'))),
- t.rejects(fetchResponseError),
- t.resolves(fetchRedirect),
- ]).then(() => t.equal(signal.listeners.abort.length, 0))
-})
-
-t.test('reject body with AbortError when aborted before read completely', t => {
- const controller = new AbortController()
- return fetch(`${base}slow`, { signal: controller.signal }).then(res => {
- const promise = res.text()
- controller.abort()
- return t.rejects(promise, { name: 'AbortError' })
- })
-})
-
-t.test('reject body methods immediately with AbortError when aborted before disturbed', t => {
- const controller = new AbortController()
- return fetch(`${base}slow`, { signal: controller.signal })
- .then(res => {
- controller.abort()
- return t.rejects(res.text(), { name: 'AbortError' })
- })
-})
-
-t.test('raise AbortError when aborted before stream is closed', async t => {
- t.plan(1)
- const controller = new AbortController()
- const res = await fetch(`${base}slow`, { signal: controller.signal })
- res.body.once('error', (err) => {
- t.match(err, { name: 'AbortError', code: 'FETCH_ABORT' })
- })
- controller.abort()
-})
-
-t.test('cancel request body stream with AbortError when aborted', {
- skip: supportStreamDestroy ? false : 'stream.destroy not supported',
-}, t => {
- const controller = new AbortController()
- const body = new Minipass({ objectMode: true })
- const promise = fetch(`${base}slow`, {
- signal: controller.signal,
- body,
- method: 'POST',
- })
-
- const result = Promise.all([
- new Promise((resolve, reject) => {
- body.on('error', (error) => {
- t.match(error, { name: 'AbortError' })
- resolve()
- })
- }),
- t.rejects(promise, { name: 'AbortError' }),
- ])
-
- controller.abort()
-
- return result
-})
-
-t.test('immediately reject when attempting to cancel and unsupported', async t => {
- const controller = new AbortController()
- const body = new (class extends Minipass {
- get destroy () {
- return undefined
- }
- })({ objectMode: true })
-
- await t.rejects(fetch(`${base}slow`, {
- signal: controller.signal,
- body,
- method: 'POST',
- }), { message: 'not supported' })
-})
-
-t.test('throw TypeError if a signal is not AbortSignal', async t => {
- await t.rejects(fetch(`${base}inspect`, { signal: {} }), {
- name: 'TypeError',
- message: /AbortSignal/,
- })
- await t.rejects(fetch(`${base}inspect`, { signal: '' }), {
- name: 'TypeError',
- message: /AbortSignal/,
- })
- await t.rejects(fetch(`${base}inspect`, { signal: Object.create(null) }), {
- name: 'TypeError',
- message: /AbortSignal/,
- })
-})
-
-t.test('set default User-Agent', async t => {
- const res = await fetch(`${base}inspect`)
- const json = await res.json()
- t.match(json.headers['user-agent'], /^minipass-fetch/)
-})
-
-t.test('setting User-Agent', t =>
- fetch(`${base}inspect`, {
- headers: {
- 'user-agent': 'faked',
- },
- }).then(res => res.json()).then(res =>
- t.equal(res.headers['user-agent'], 'faked')))
-
-t.test('set default Accept header', async t => {
- const res = await fetch(`${base}inspect`)
- const json = await res.json()
- t.equal(json.headers.accept, '*/*')
-})
-
-t.test('allow setting Accept header', async t => {
- const res = await fetch(`${base}inspect`, {
- headers: {
- accept: 'application/json',
- },
- })
- const json = await res.json()
- t.equal(json.headers.accept, 'application/json')
-})
-
-t.test('allow POST request', async t => {
- const res = await fetch(`${base}inspect`, { method: 'POST' })
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '0')
-})
-
-t.test('POST request with string body', async t => {
- const res = await fetch(`${base}inspect`, {
- method: 'POST',
- body: 'a=1',
- })
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'a=1')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], 'text/plain;charset=UTF-8')
- t.equal(json.headers['content-length'], '3')
-})
-
-t.test('POST request with buffer body', async t => {
- const res = await fetch(`${base}inspect`, {
- method: 'POST',
- body: Buffer.from('a=1', 'utf-8'),
- })
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'a=1')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '3')
-})
-/*
-t.test('allow POST request with ArrayBuffer body', async t => {
- const res = await fetch(`${base}inspect`, {
- method: 'POST',
- body: stringToArrayBuffer('Hello, world!\n'),
- })
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'Hello, world!\n')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '14')
-})
-*/
-t.test('POST request with ArrayBuffer body from VM context', async t => {
- Buffer.from(new VMArrayBuffer())
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: new VMUint8Array(Buffer.from('Hello, world!\n')).buffer,
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'Hello, world!\n')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '14')
-})
-/*
-t.test('POST request with ArrayBufferView (Uint8Array) body', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: new Uint8Array(stringToArrayBuffer('Hello, world!\n')),
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'Hello, world!\n')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '14')
-})
-*/
-/*
-t.test('POST request with ArrayBufferView (DataView) body', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: new DataView(stringToArrayBuffer('Hello, world!\n')),
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'Hello, world!\n')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '14')
-})
-*/
-t.test('POST with ArrayBufferView (Uint8Array) body from a VM context', async t => {
- Buffer.from(new VMArrayBuffer())
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: new VMUint8Array(Buffer.from('Hello, world!\n')),
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'Hello, world!\n')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '14')
-})
-/*
-t.test('POST with ArrayBufferView (Uint8Array, offset, length) body', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: new Uint8Array(stringToArrayBuffer('Hello, world!\n'), 7, 6),
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'world!')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '6')
-})
-*/
-t.test('POST with blob body without type', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: new Blob(['a=1']),
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'a=1')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], '3')
-})
-
-t.test('POST with blob body with type', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: new Blob(['a=1'], {
- type: 'text/plain;charset=UTF-8',
- }),
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'a=1')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], 'text/plain;charset=utf-8')
- t.equal(json.headers['content-length'], '3')
-})
-
-t.test('POST with readable stream as body', async t => {
- const body = new Minipass()
- body.pause()
- body.end('a=1')
- setTimeout(() => {
- body.resume()
- }, 100)
-
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: body.pipe(new Minipass()),
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'a=1')
- t.equal(json.headers['transfer-encoding'], 'chunked')
- t.equal(json.headers['content-type'], undefined)
- t.equal(json.headers['content-length'], undefined)
-})
-
-t.test('POST with form-data as body', async t => {
- const form = new FormData()
- form.append('a', '1')
-
- const url = `${base}multipart`
- const opts = {
- method: 'POST',
- body: form,
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.match(json.headers['content-type'], /^multipart\/form-data;boundary=/)
- t.match(json.headers['content-length'], String)
- t.equal(json.body, 'a=1')
-})
-
-t.test('POST with form-data using stream as body', async t => {
- t.teardown(() => {
- const root = path.dirname(__dirname)
- // parted's multipart form parser writes a temporary file to disk, this removes it
- fs.readdirSync(root).filter((file) => {
- return file.startsWith('dummy.') && file.endsWith('.txt')
- }).forEach((file) => {
- fs.unlinkSync(path.join(root, file))
- })
- })
- const form = new FormData()
- form.append('my_field', fs.createReadStream(path.join(__dirname, 'fixtures/dummy.txt')))
-
- const url = `${base}multipart`
- const opts = {
- method: 'POST',
- body: form,
- }
-
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.match(json.headers['content-type'], /^multipart\/form-data;boundary=/)
- t.equal(json.headers['content-length'], undefined)
- t.match(json.body, 'my_field=')
-})
-
-t.test('POST with form-data as body and custom headers', async t => {
- const form = new FormData()
- form.append('a', '1')
-
- const headers = form.getHeaders()
- headers.b = '2'
-
- const url = `${base}multipart`
- const opts = {
- method: 'POST',
- body: form,
- headers,
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.match(json.headers['content-type'], /multipart\/form-data; boundary=/)
- t.match(json.headers['content-length'], String)
- t.equal(json.headers.b, '2')
- t.equal(json.body, 'a=1')
-})
-
-t.test('POST with object body', async t => {
- const url = `${base}inspect`
- // note that fetch simply calls tostring on an object
- const opts = {
- method: 'POST',
- body: { a: 1 },
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, '[object Object]')
- t.equal(json.headers['content-type'], 'text/plain;charset=UTF-8')
- t.equal(json.headers['content-length'], '15')
-})
-
-const uspOpt = {
- skip: typeof URLSearchParams === 'function' ? false
- : 'no URLSearchParams function',
-}
-
-t.test('constructing a Response with URLSearchParams as body has a Content-Type', uspOpt, t => {
- const params = new URLSearchParams()
- const res = new Response(params)
- res.headers.get('Content-Type')
- t.equal(res.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8')
-})
-
-t.test('constructing a Request with URLSearchParams as body has a Content-Type', uspOpt, t => {
- const params = new URLSearchParams()
- const req = new Request(base, { method: 'POST', body: params })
- t.equal(req.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8')
-})
-
-t.test('Reading a body with URLSearchParams should echo back the result', uspOpt, async t => {
- const params = new URLSearchParams()
- params.append('a', '1')
- const text = await new Response(params).text()
- t.equal(text, 'a=1')
-})
-
-// Body should been cloned...
-// eslint-disable-next-line max-len
-t.test('Request/Response with URLSearchParams and mutation should not affected body', uspOpt, async t => {
- const params = new URLSearchParams()
- const req = new Request(`${base}inspect`, { method: 'POST', body: params })
- params.append('a', '1')
- const text = await req.text()
- t.equal(text, '')
-})
-
-t.test('POST with URLSearchParams as body', uspOpt, async t => {
- const params = new URLSearchParams()
- params.append('a', '1')
-
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: params,
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8')
- t.equal(json.headers['content-length'], '3')
- t.equal(json.body, 'a=1')
-})
-
-t.test('recognize URLSearchParams when extended', uspOpt, async t => {
- class CustomSearchParams extends URLSearchParams {}
- const params = new CustomSearchParams()
- params.append('a', '1')
-
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: params,
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8')
- t.equal(json.headers['content-length'], '3')
- t.equal(json.body, 'a=1')
-})
-
-/* for 100% code coverage, checks for duck-typing-only detection
- * where both constructor.name and brand tests fail */
-t.test('recognize URLSearchParams when extended from polyfill', async t => {
- class CustomPolyfilledSearchParams extends URLSearchParamsPolyfill {}
- const params = new CustomPolyfilledSearchParams()
- params.append('a', '1')
-
- const url = `${base}inspect`
- const opts = {
- method: 'POST',
- body: params,
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8')
- t.equal(json.headers['content-length'], '3')
- t.equal(json.body, 'a=1')
-})
-
-t.test('overwrite Content-Length if possible', async t => {
- const url = `${base}inspect`
- // note that fetch simply calls tostring on an object
- const opts = {
- method: 'POST',
- headers: {
- 'Content-Length': '1000',
- },
- body: 'a=1',
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'POST')
- t.equal(json.body, 'a=1')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-type'], 'text/plain;charset=UTF-8')
- t.equal(json.headers['content-length'], '3')
-})
-
-t.test('PUT', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'PUT',
- body: 'a=1',
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'PUT')
- t.equal(json.body, 'a=1')
-})
-
-t.test('DELETE', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'DELETE',
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'DELETE')
-})
-
-t.test('DELETE with string body', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'DELETE',
- body: 'a=1',
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'DELETE')
- t.equal(json.body, 'a=1')
- t.equal(json.headers['transfer-encoding'], undefined)
- t.equal(json.headers['content-length'], '3')
-})
-
-t.test('PATCH', async t => {
- const url = `${base}inspect`
- const opts = {
- method: 'PATCH',
- body: 'a=1',
- }
- const res = await fetch(url, opts)
- const json = await res.json()
- t.equal(json.method, 'PATCH')
- t.equal(json.body, 'a=1')
-})
-
-t.test('HEAD', async t => {
- const url = `${base}hello`
- const opts = {
- method: 'HEAD',
- }
- const res = await fetch(url, opts)
- t.equal(res.status, 200)
- t.equal(res.statusText, 'OK')
- t.equal(res.headers.get('content-type'), 'text/plain')
- t.match(res.body, Minipass)
- const text = await res.text()
- t.equal(text, '')
-})
-
-t.test('HEAD with content-encoding header', async t => {
- const url = `${base}error/404`
- const opts = {
- method: 'HEAD',
- }
- const res = await fetch(url, opts)
- t.equal(res.status, 404)
- t.equal(res.headers.get('content-encoding'), 'gzip')
- const text = await res.text()
- t.equal(text, '')
-})
-
-t.test('OPTIONS', async t => {
- const url = `${base}options`
- const opts = {
- method: 'OPTIONS',
- }
- const res = await fetch(url, opts)
- t.equal(res.status, 200)
- t.equal(res.statusText, 'OK')
- t.equal(res.headers.get('allow'), 'GET, HEAD, OPTIONS')
- t.match(res.body, Minipass)
-})
-
-t.test('reject decoding body twice', async t => {
- const url = `${base}plain`
- const res = await fetch(url)
- t.equal(res.headers.get('content-type'), 'text/plain')
- await res.text()
- t.equal(res.bodyUsed, true)
- t.rejects(res.text())
-})
-
-t.test('response trailers', async t => {
- const res = await fetch(`${base}trailers`)
- t.equal(res.status, 200)
- t.equal(res.statusText, 'OK')
- t.equal(res.headers.get('Trailer'), 'X-Node-Fetch')
- const trailers = await res.trailer
- t.same(Array.from(trailers.keys()), ['x-node-fetch'])
- t.equal(trailers.get('x-node-fetch'), 'hello world!')
-})
-
-t.test('maximum response size, multiple chunk', async t => {
- const url = `${base}size/chunk`
- const opts = {
- size: 5,
- }
- const res = await fetch(url, opts)
- t.equal(res.status, 200)
- t.equal(res.headers.get('content-type'), 'text/plain')
- await t.rejects(res.text(), {
- name: 'FetchError',
- type: 'max-size',
- })
-})
-
-t.test('maximum response size, single chunk', t => {
- const url = `${base}size/long`
- const opts = {
- size: 5,
- }
- return t.rejects(fetch(url, opts).then(res => {
- t.equal(res.status, 200)
- t.equal(res.headers.get('content-type'), 'text/plain')
- return res.text()
- }), {
- name: 'FetchError',
- type: 'max-size',
- })
-})
-
-t.test('pipe response body as stream', async t => {
- t.plan(2)
- const url = `${base}hello`
- const res = await fetch(url)
- t.match(res.body, Minipass)
- await streamToPromise(res.body, chunk => {
- if (chunk === null) {
- return
- }
- t.equal(chunk.toString(), 'world')
- })
-})
-
-t.test('clone a response, and use both as stream', async t => {
- t.plan(4)
- const url = `${base}hello`
- const res = await fetch(url)
- const cloned = res.clone()
- t.match(res.body, Minipass)
- t.match(cloned.body, Minipass)
- const dataHandler = chunk => {
- if (chunk === null) {
- return
- }
- t.equal(chunk.toString(), 'world')
- }
-
- await Promise.all([
- streamToPromise(res.body, dataHandler),
- streamToPromise(cloned.body, dataHandler),
- ])
-})
-
-t.test('clone a json response and log it as text response', async t => {
- const url = `${base}json`
- const res = await fetch(url)
- const cloned = res.clone()
- const results = await Promise.all([res.json(), cloned.text()])
- t.same(results[0], { name: 'value' })
- t.equal(results[1], '{"name":"value"}')
-})
-
-t.test('clone a json response, and then log it as text response', async t => {
- const url = `${base}json`
- const res = await fetch(url)
- const cloned = res.clone()
- const jsonResult = await res.json()
- t.same(jsonResult, { name: 'value' })
- const textResult = await cloned.text()
- t.equal(textResult, '{"name":"value"}')
-})
-
-t.test('clone a json response, first log as text response, then return json object', async t => {
- const url = `${base}json`
- const res = await fetch(url)
- const cloned = res.clone()
- const textResult = await cloned.text()
- t.equal(textResult, '{"name":"value"}')
- const jsonResult = await res.json()
- t.same(jsonResult, { name: 'value' })
-})
-
-t.test('do not allow cloning a response after its been used', async t => {
- const url = `${base}hello`
- const res = await fetch(url)
- await res.text()
- t.throws(() => res.clone())
-})
-
-t.test('get all responses of a header', async t => {
- const url = `${base}cookie`
- const res = await fetch(url)
- const expected = 'a=1, b=1'
- t.equal(res.headers.get('set-cookie'), expected)
- t.equal(res.headers.get('Set-Cookie'), expected)
-})
-
-t.test('return all headers using raw()', async t => {
- const url = `${base}cookie`
- const res = await fetch(url)
- t.same(res.headers.raw()['set-cookie'], ['a=1', 'b=1'])
-})
-
-t.test('delete header', async t => {
- const url = `${base}cookie`
- const res = await fetch(url)
- res.headers.delete('set-cookie')
- t.equal(res.headers.get('set-cookie'), null)
-})
-
-t.test('send request with connection keep-alive if agent is provided', async t => {
- const url = `${base}inspect`
- const opts = {
- agent: new http.Agent({
- keepAlive: true,
- }),
- }
- const res = await fetch(url, opts)
- const body = await res.json()
- t.equal(body.headers.connection, 'keep-alive')
-})
-
-t.test('fetch with Request instance', async t => {
- const url = `${base}hello`
- const req = new Request(url)
- const res = await fetch(req)
- t.equal(res.url, url)
- t.equal(res.ok, true)
- t.equal(res.status, 200)
-})
-
-t.test('fetch with Node.js legacy URL object', async t => {
- const url = `${base}hello`
- const urlObj = parseURL(url)
- const req = new Request(urlObj)
- const res = await fetch(req)
- t.equal(res.url, url)
- t.equal(res.ok, true)
- t.equal(res.status, 200)
-})
-
-t.test('fetch with Node.js URL object', async t => {
- const url = `${base}hello`
- const urlObj = new URL(url)
- const req = new Request(urlObj)
- const res = await fetch(req)
- t.equal(res.url, url)
- t.equal(res.ok, true)
- t.equal(res.status, 200)
-})
-
-t.test('reading blob as text', async t => {
- const blob = await new Response(`hello`).blob()
- const body = await blob.text()
- t.equal(body, 'hello')
-})
-
-t.test('reading blob as arrayBuffer', async t => {
- const blob = await new Response(`hello`).blob()
- const ab = await blob.arrayBuffer()
- const str = String.fromCharCode.apply(null, new Uint8Array(ab))
- t.equal(str, 'hello')
-})
-
-t.test('reading blob as stream', t => {
- return new Response(`hello`)
- .blob()
- .then(blob => streamToPromise(blob.stream(), data => {
- const str = data.toString()
- t.equal(str, 'hello')
- }))
-})
-
-t.test('blob round-trip', async t => {
- const url = `${base}hello`
-
- const res = await fetch(url)
- const blob = await res.blob()
- const inspectUrl = `${base}inspect`
- const length = blob.size
- const type = blob.type
- const res2 = await fetch(inspectUrl, {
- method: 'POST',
- body: blob,
- })
- const { body, headers } = await res2.json()
- t.equal(body, 'world')
- t.equal(headers['content-type'], type)
- t.equal(headers['content-length'], String(length))
-})
-
-t.test('overwrite Request instance', async t => {
- const url = `${base}inspect`
- const req = new Request(url, {
- method: 'POST',
- headers: {
- a: '1',
- },
- })
- const res = await fetch(req, {
- method: 'GET',
- headers: {
- a: '2',
- },
- })
- const body = await res.json()
- t.equal(body.method, 'GET')
- t.equal(body.headers.a, '2')
-})
-
-t.test('arrayBuffer(), blob(), text(), json() and buffer() method in Body constructor', t => {
- const body = new Body('a=1')
- t.match(body.arrayBuffer, Function)
- t.match(body.blob, Function)
- t.match(body.text, Function)
- t.match(body.json, Function)
- t.match(body.buffer, Function)
- t.end()
-})
-
-t.test('https request', { timeout: 5000 }, async t => {
- const url = 'https://github.com/'
- const opts = {
- method: 'HEAD',
- }
- const res = await fetch(url, opts)
- t.equal(res.status, 200)
- t.equal(res.ok, true)
-})
-
-// issue #414
-t.test('reject if attempt to accumulate body stream throws', async t => {
- const body = new Minipass()
- body.pause()
- body.end('a=1')
- setTimeout(() => body.resume(), 100)
- const res = new Response(body.pipe(new Minipass()))
- const bufferConcat = Buffer.concat
- Buffer.concat = () => {
- throw new Error('embedded error')
- }
-
- t.teardown(() => {
- Buffer.concat = bufferConcat
- })
-
- return t.rejects(res.text(), {
- name: 'FetchError',
- type: 'system',
- message: /embedded error/,
- })
-})
-
-t.test('supports supplying a lookup function to the agent', async t => {
- const url = `${base}redirect/301`
- let called = 0
- function lookupSpy (hostname, options, callback) {
- called++
- return lookup(hostname, options, callback)
- }
- const agent = http.Agent({ lookup: lookupSpy })
- await fetch(url, { agent })
- t.equal(called, 2)
-})
-
-t.test('supports supplying a famliy option to the agent', async t => {
- const url = `${base}redirect/301`
- const families = []
- const family = Symbol('family')
- function lookupSpy (hostname, options, callback) {
- families.push(options.family)
- return lookup(hostname, {}, callback)
- }
- const agent = http.Agent({ lookup: lookupSpy, family })
- await fetch(url, { agent })
- t.same(families, [family, family])
-})
-
-t.test('function supplying the agent', async t => {
- const url = `${base}inspect`
-
- const agent = new http.Agent({
- keepAlive: true,
- })
-
- let parsedURL
-
- const res = await fetch(url, {
- agent: function (_parsedURL) {
- parsedURL = _parsedURL
- return agent
- },
- })
- const body = await res.json()
- // the agent provider should have been called
- t.equal(parsedURL.protocol, 'http:')
- // the agent we returned should have been used
- t.equal(body.headers.connection, 'keep-alive')
-})
-
-t.test('calculate content length and extract content type', t => {
- const url = `${base}hello`
- const bodyContent = 'a=1'
-
- let streamBody = new Minipass()
- streamBody.pause()
- streamBody.end(bodyContent)
- setTimeout(() => streamBody.resume(), 100)
- streamBody = streamBody.pipe(new Minipass())
-
- const streamRequest = new Request(url, {
- method: 'POST',
- body: streamBody,
- size: 1024,
- })
-
- const blobBody = new Blob([bodyContent], { type: 'text/plain' })
- const blobRequest = new Request(url, {
- method: 'POST',
- body: blobBody,
- size: 1024,
- })
-
- const formBody = new FormData()
- formBody.append('a', '1')
- const formRequest = new Request(url, {
- method: 'POST',
- body: formBody,
- size: 1024,
- })
-
- const bufferBody = Buffer.from(bodyContent)
- const bufferRequest = new Request(url, {
- method: 'POST',
- body: bufferBody,
- size: 1024,
- })
-
- const stringRequest = new Request(url, {
- method: 'POST',
- body: bodyContent,
- size: 1024,
- })
-
- const nullRequest = new Request(url, {
- method: 'GET',
- body: null,
- size: 1024,
- })
-
- t.equal(getTotalBytes(streamRequest), null)
- t.equal(getTotalBytes(blobRequest), blobBody.size)
- t.not(getTotalBytes(formRequest), null)
- t.equal(getTotalBytes(bufferRequest), bufferBody.length)
- t.equal(getTotalBytes(stringRequest), bodyContent.length)
- t.equal(getTotalBytes(nullRequest), 0)
-
- t.equal(extractContentType(streamBody), null)
- t.equal(extractContentType(blobBody), 'text/plain')
- t.match(extractContentType(formBody), /^multipart\/form-data/)
- t.equal(extractContentType(bufferBody), null)
- t.equal(extractContentType(bodyContent), 'text/plain;charset=UTF-8')
- t.equal(extractContentType(null), null)
- t.end()
-})
-
-t.test('with optional `encoding`', t => {
- t.test('only use UTF-8 decoding with text()', async t => {
- const res = await fetch(`${base}encoding/euc-jp`)
- t.equal(res.status, 200)
- const result = await res.text()
- t.equal(result, '<?xml version="1.0" encoding="EUC-JP"?>' +
- '<title>\ufffd\ufffd\ufffd\u0738\ufffd</title>')
- })
-
- t.test('encoding decode, xml dtd detect', async t => {
- const res = await fetch(`${base}encoding/euc-jp`)
- t.equal(res.status, 200)
- const result = await res.textConverted()
- t.equal(result, '<?xml version="1.0" encoding="EUC-JP"?><title>日本語</title>')
- })
-
- t.test('encoding decode, content-type detect', async t => {
- const res = await fetch(`${base}encoding/shift-jis`)
- t.equal(res.status, 200)
- const result = await res.textConverted()
- t.equal(result, '<div>日本語</div>')
- })
-
- t.test('encoding decode, html5 detect', async t => {
- const res = await fetch(`${base}encoding/gbk`)
- t.equal(res.status, 200)
- const result = await res.textConverted()
- t.equal(result, '<meta charset="gbk"><div>中文</div>')
- })
-
- t.test('encoding decode, html4 detect', async t => {
- const res = await fetch(`${base}encoding/gb2312`)
- t.equal(res.status, 200)
- const result = await res.textConverted()
- t.equal(result, '<meta http-equiv="Content-Type" content="text/html; charset=gb2312">' +
- '<div>中文</div>')
- })
-
- t.test('encoding decode, html4 detect reverse http-equiv', async t => {
- const res = await fetch(`${base}encoding/gb2312-reverse`)
- t.equal(res.status, 200)
- const result = await res.textConverted()
- t.equal(result, '<meta content="text/html; charset=gb2312" http-equiv="Content-Type">' +
- '<div>中文</div>')
- })
-
- t.test('default to utf8 encoding', async t => {
- const res = await fetch(`${base}encoding/utf8`)
- t.equal(res.status, 200)
- t.equal(res.headers.get('content-type'), null)
- const result = await res.textConverted()
- t.equal(result, '中文')
- })
-
- t.test('uncommon content-type order, charset in front', async t => {
- const res = await fetch(`${base}encoding/order1`)
- t.equal(res.status, 200)
- const result = await res.textConverted()
- t.equal(result, '中文')
- })
-
- t.test('uncommon content-type order, end with qs', async t => {
- const res = await fetch(`${base}encoding/order2`)
- t.equal(res.status, 200)
- const result = await res.textConverted()
- t.equal(result, '中文')
- })
-
- t.test('chunked encoding, html4 detect', async t => {
- const url = `${base}encoding/chunked`
- const res = await fetch(url)
- t.equal(res.status, 200)
- const padding = 'a'.repeat(10)
- const result = await res.textConverted()
- t.equal(result, `${padding}<meta http-equiv="Content-Type" content="text/html;` +
- ' charset=Shift_JIS" /><div>日本語</div>')
- })
-
- t.test('only do encoding detection up to 1024 bytes', async t => {
- const url = `${base}encoding/invalid`
- const res = await fetch(url)
- t.equal(res.status, 200)
- const padding = 'a'.repeat(1200)
- const result = await res.textConverted()
- t.not(result, `${padding}中文`)
- })
-
- t.end()
-})
-
-t.test('data uri', t => {
- const dataUrl = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs='
-
- const invalidDataUrl = 'data:@@@@'
-
- t.test('accept data uri', t =>
- fetch(dataUrl).then(r => {
- t.equal(r.status, 200)
- t.equal(r.headers.get('Content-Type'), 'image/gif')
- return r.buffer().then(b => t.type(b, Buffer))
- }))
-
- t.test('reject invalid data uri', t =>
- t.rejects(fetch(invalidDataUrl), {
- message: 'invalid data: URI',
- }))
-
- t.test('data uri not base64 encoded', t =>
- fetch('data:text/plain,hello, world!').then(r => {
- t.equal(r.status, 200)
- t.equal(r.headers.get('Content-Type'), 'text/plain')
- return r.buffer().then(b => t.equal(b.toString(), 'hello, world!'))
- }))
-
- t.test('data uri with no type specified', t =>
- fetch('data:,hello,%20world!').then(r => {
- t.equal(r.status, 200)
- t.equal(r.headers.get('Content-Type'), null)
- return r.buffer().then(b => t.equal(b.toString(), 'hello, world!'))
- }))
-
- t.test('search included, hash not included', t =>
- fetch('data:,hello?with=search#no%20hash').then(r => {
- t.equal(r.status, 200)
- t.equal(r.headers.get('Content-Type'), null)
- return r.buffer().then(b => t.equal(b.toString(), 'hello?with=search'))
- }))
-
- t.end()
-})
-
-t.test('aborting data uris', t => {
- const controllers = [AbortController, AbortController2]
- t.plan(controllers.length)
- const url = 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=='
- controllers.forEach((Controller, idx) => {
- t.test(`controller ${idx}`, async t => {
- t.test('pre-abort', async t => {
- const controller = new Controller()
- controller.abort()
- t.rejects(fetch(url, { signal: controller.signal }), {
- message: 'The user aborted a request.',
- })
- })
-
- t.test('post-abort', async t => {
- const controller = new Controller()
- t.rejects(fetch(url, { signal: controller.signal }), {
- message: 'The user aborted a request.',
- })
- controller.abort()
- })
-
- t.test('cannot abort after first tick', t => {
- const controller = new Controller()
- const testPromise = t.resolves(fetch(url, { signal: controller.signal }))
- process.nextTick(() => {
- controller.abort()
- })
- return testPromise
- })
- })
- })
-})
-
-t.test('redirect changes host header', t =>
- fetch(`http://${local.hostname}:${local.port}/host-redirect`, {
- redirect: 'follow',
- headers: { host: 'foo' },
- })
- .then(r => r.text())
- .then(text => t.equal(text, `${base}host-redirect`)))
-
-t.test('never apply backpressure to the underlying response stream', t => {
- const { request } = http
- t.teardown(() => http.request = request)
- http.request = (...args) => {
- const req = request(...args)
- const { emit } = req
- req.emit = (ev, ...emitArgs) => {
- if (ev === 'response') {
- const res = emitArgs[0]
- res.pause = () => {
- throw new Error('should not pause the response')
- }
- }
- return emit.call(req, ev, ...emitArgs)
- }
- return req
- }
-
- const dest = new Minipass()
- return fetch(`${base}hello`)
- .then(res => {
- // read it a bit later, so we'll see if any backpressure happens.
- setTimeout(() => dest.read())
- return res.body.pipe(dest).promise()
- })
-})
|