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.mustCall((err, data) => { 20 assert.ifError(err); 21 fileData = data; 22 23 const server = http2.createServer(); 24 let client; 25 26 const countdown = new Countdown(3, () => { 27 server.close(); 28 client.close(); 29 }); 30 31 server.on('stream', common.mustCall((stream) => { 32 let data = Buffer.alloc(0); 33 stream.on('data', (chunk) => data = Buffer.concat([data, chunk])); 34 stream.on('end', common.mustCall(() => { 35 assert.deepStrictEqual(data, fileData); 36 })); 37 // Waiting on close avoids spurious ECONNRESET seen in windows CI. 38 // Not sure if this is actually a bug; more details at 39 // https://github.com/nodejs/node/issues/20750#issuecomment-511015247 40 stream.on('close', () => countdown.dec()); 41 stream.respond(); 42 stream.end(); 43 })); 44 45 server.listen(0, common.mustCall(() => { 46 client = http2.connect(`http://localhost:${server.address().port}`); 47 48 const req = client.request({ ':method': 'POST' }); 49 req.on('response', common.mustCall()); 50 51 req.resume(); 52 req.on('end', common.mustCall()); 53 54 req.on('finish', () => countdown.dec()); 55 const str = fs.createReadStream(loc); 56 str.on('end', common.mustCall()); 57 str.on('close', () => countdown.dec()); 58 str.pipe(req); 59 })); 60})); 61