11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst http2 = require('http2');
81cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst server = http2.createServer();
111cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream, headers, flags) => {
121cb0ef41Sopenharmony_ci  if (headers[':path'] === '/') {
131cb0ef41Sopenharmony_ci    stream.pushStream({ ':path': '/foobar' }, (err, push, headers) => {
141cb0ef41Sopenharmony_ci      assert.ifError(err);
151cb0ef41Sopenharmony_ci      push.respond({
161cb0ef41Sopenharmony_ci        'content-type': 'text/html',
171cb0ef41Sopenharmony_ci        'x-push-data': 'pushed by server',
181cb0ef41Sopenharmony_ci      });
191cb0ef41Sopenharmony_ci      push.write('pushed by server ');
201cb0ef41Sopenharmony_ci      setImmediate(() => push.end('data'));
211cb0ef41Sopenharmony_ci      stream.end('st');
221cb0ef41Sopenharmony_ci    });
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci  stream.respond({ 'content-type': 'text/html' });
251cb0ef41Sopenharmony_ci  stream.write('te');
261cb0ef41Sopenharmony_ci}));
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
301cb0ef41Sopenharmony_ci  const port = server.address().port;
311cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${port}`);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  const req = client.request();
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const countdown = new Countdown(2, () => {
361cb0ef41Sopenharmony_ci    server.close();
371cb0ef41Sopenharmony_ci    client.close();
381cb0ef41Sopenharmony_ci  });
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  req.on('response', common.mustCall((headers) => {
411cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':status'], 200);
421cb0ef41Sopenharmony_ci    assert.strictEqual(headers['content-type'], 'text/html');
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  client.on('stream', common.mustCall((stream, headers, flags) => {
461cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':scheme'], 'http');
471cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':path'], '/foobar');
481cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':authority'], `localhost:${port}`);
491cb0ef41Sopenharmony_ci    stream.on('push', common.mustCall((headers, flags) => {
501cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 200);
511cb0ef41Sopenharmony_ci      assert.strictEqual(headers['content-type'], 'text/html');
521cb0ef41Sopenharmony_ci      assert.strictEqual(headers['x-push-data'], 'pushed by server');
531cb0ef41Sopenharmony_ci    }));
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    stream.setEncoding('utf8');
561cb0ef41Sopenharmony_ci    let pushData = '';
571cb0ef41Sopenharmony_ci    stream.on('data', (d) => pushData += d);
581cb0ef41Sopenharmony_ci    stream.on('end', common.mustCall(() => {
591cb0ef41Sopenharmony_ci      assert.strictEqual(pushData, 'pushed by server data');
601cb0ef41Sopenharmony_ci    }));
611cb0ef41Sopenharmony_ci    stream.on('close', () => countdown.dec());
621cb0ef41Sopenharmony_ci  }));
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  let data = '';
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  req.setEncoding('utf8');
671cb0ef41Sopenharmony_ci  req.on('data', common.mustCallAtLeast((d) => data += d));
681cb0ef41Sopenharmony_ci  req.on('end', common.mustCall(() => {
691cb0ef41Sopenharmony_ci    assert.strictEqual(data, 'test');
701cb0ef41Sopenharmony_ci  }));
711cb0ef41Sopenharmony_ci  req.on('close', () => countdown.dec());
721cb0ef41Sopenharmony_ci}));
73