11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Verifies that uploading data from a client works
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst http2 = require('http2');
101cb0ef41Sopenharmony_ciconst fs = require('fs');
111cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
121cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst loc = fixtures.path('person-large.jpg');
151cb0ef41Sopenharmony_cilet fileData;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciassert(fs.existsSync(loc));
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cifs.readFile(loc, common.mustSucceed((data) => {
201cb0ef41Sopenharmony_ci  fileData = data;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const server = http2.createServer();
231cb0ef41Sopenharmony_ci  let client;
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  const countdown = new Countdown(2, () => {
261cb0ef41Sopenharmony_ci    server.close();
271cb0ef41Sopenharmony_ci    client.close();
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  server.on('stream', common.mustCall((stream) => {
311cb0ef41Sopenharmony_ci    let data = Buffer.alloc(0);
321cb0ef41Sopenharmony_ci    stream.on('data', (chunk) => data = Buffer.concat([data, chunk]));
331cb0ef41Sopenharmony_ci    stream.on('end', common.mustCall(() => {
341cb0ef41Sopenharmony_ci      assert.deepStrictEqual(data, fileData);
351cb0ef41Sopenharmony_ci    }));
361cb0ef41Sopenharmony_ci    // Waiting on close avoids spurious ECONNRESET seen in windows CI.
371cb0ef41Sopenharmony_ci    // Not sure if this is actually a bug; more details at
381cb0ef41Sopenharmony_ci    // https://github.com/nodejs/node/issues/20750#issuecomment-511015247
391cb0ef41Sopenharmony_ci    stream.on('close', () => countdown.dec());
401cb0ef41Sopenharmony_ci    stream.respond();
411cb0ef41Sopenharmony_ci    stream.end();
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
451cb0ef41Sopenharmony_ci    client = http2.connect(`http://localhost:${server.address().port}`);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    const req = client.request({ ':method': 'POST' });
481cb0ef41Sopenharmony_ci    req.on('response', common.mustCall());
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    req.resume();
511cb0ef41Sopenharmony_ci    req.on('end', common.mustCall());
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    const str = fs.createReadStream(loc);
541cb0ef41Sopenharmony_ci    str.on('end', common.mustCall());
551cb0ef41Sopenharmony_ci    str.on('close', () => countdown.dec());
561cb0ef41Sopenharmony_ci    str.pipe(req);
571cb0ef41Sopenharmony_ci  }));
581cb0ef41Sopenharmony_ci}));
59