1'use strict'; 2 3// Verifies that uploading data from a client works 4 5const common = require('../common'); 6if (!common.hasCrypto) 7 common.skip('missing crypto'); 8const assert = require('assert'); 9const http2 = require('http2'); 10const fs = require('fs'); 11const fixtures = require('../common/fixtures'); 12const Countdown = require('../common/countdown'); 13 14const loc = fixtures.path('person-large.jpg'); 15let fileData; 16 17assert(fs.existsSync(loc)); 18 19fs.readFile(loc, common.mustSucceed((data) => { 20 fileData = data; 21 22 const server = http2.createServer(); 23 let client; 24 25 const countdown = new Countdown(3, () => { 26 server.close(); 27 client.close(); 28 }); 29 30 server.on('stream', common.mustCall((stream) => { 31 let data = Buffer.alloc(0); 32 stream.on('data', (chunk) => data = Buffer.concat([data, chunk])); 33 stream.on('end', common.mustCall(() => { 34 assert.deepStrictEqual(data, fileData); 35 })); 36 // Waiting on close avoids spurious ECONNRESET seen in windows CI. 37 // Not sure if this is actually a bug; more details at 38 // https://github.com/nodejs/node/issues/20750#issuecomment-511015247 39 stream.on('close', () => countdown.dec()); 40 stream.respond(); 41 stream.end(); 42 })); 43 44 server.listen(0, common.mustCall(() => { 45 client = http2.connect(`http://localhost:${server.address().port}`); 46 47 const req = client.request({ ':method': 'POST' }); 48 req.on('response', common.mustCall()); 49 50 req.resume(); 51 req.on('end', common.mustCall()); 52 53 req.on('finish', () => countdown.dec()); 54 const str = fs.createReadStream(loc); 55 str.on('end', common.mustCall()); 56 str.on('close', () => countdown.dec()); 57 str.pipe(req); 58 })); 59})); 60