mirror of
https://github.com/nodejs/node.git
synced 2025-05-09 14:16:57 +00:00

Extensive re-work of http1 compatibility layer based on tests in express, on-finished and finalhandler. Fix handling of HEAD method to match http1. Adjust write, end, etc. to call writeHead as in http1 and as expected by user-land modules. Add socket proxy that instead uses the Http2Stream for the vast majority of socket interactions. Add and change tests to closer represent http1 behaviour. Refs: https://github.com/nodejs/node/pull/15633 Refs: https://github.com/expressjs/express/tree/master/test Refs: https://github.com/jshttp/on-finished/blob/master/test/test.js Refs: https://github.com/pillarjs/finalhandler/blob/master/test/test.js PR-URL: https://github.com/nodejs/node/pull/15702 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
const assert = require('assert');
|
|
const http2 = require('http2');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// piping should work as expected with createWriteStream
|
|
|
|
const loc = path.join(common.fixturesDir, 'person.jpg');
|
|
const fn = path.join(common.tmpDir, 'http2pipe.jpg');
|
|
common.refreshTmpDir();
|
|
|
|
const server = http2.createServer();
|
|
|
|
server.on('request', common.mustCall((req, res) => {
|
|
const dest = req.pipe(fs.createWriteStream(fn));
|
|
dest.on('finish', common.mustCall(() => {
|
|
assert.strictEqual(req.complete, true);
|
|
assert.deepStrictEqual(fs.readFileSync(loc), fs.readFileSync(fn));
|
|
fs.unlinkSync(fn);
|
|
res.end();
|
|
}));
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const port = server.address().port;
|
|
const client = http2.connect(`http://localhost:${port}`);
|
|
|
|
let remaining = 2;
|
|
function maybeClose() {
|
|
if (--remaining === 0) {
|
|
server.close();
|
|
client.destroy();
|
|
}
|
|
}
|
|
|
|
const req = client.request({ ':method': 'POST' });
|
|
req.on('response', common.mustCall());
|
|
req.resume();
|
|
req.on('end', common.mustCall(maybeClose));
|
|
const str = fs.createReadStream(loc);
|
|
str.on('end', common.mustCall(maybeClose));
|
|
str.pipe(req);
|
|
}));
|