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_ci
91cb0ef41Sopenharmony_ciconst server = http2.createServer();
101cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream, headers) => {
111cb0ef41Sopenharmony_ci  const port = server.address().port;
121cb0ef41Sopenharmony_ci  if (headers[':path'] === '/') {
131cb0ef41Sopenharmony_ci    stream.pushStream({
141cb0ef41Sopenharmony_ci      ':scheme': 'http',
151cb0ef41Sopenharmony_ci      ':path': '/foobar',
161cb0ef41Sopenharmony_ci      ':authority': `localhost:${port}`,
171cb0ef41Sopenharmony_ci    }, common.mustSucceed((push, headers) => {
181cb0ef41Sopenharmony_ci      push.respond({
191cb0ef41Sopenharmony_ci        'content-type': 'text/html',
201cb0ef41Sopenharmony_ci        ':status': 200,
211cb0ef41Sopenharmony_ci        'x-push-data': 'pushed by server',
221cb0ef41Sopenharmony_ci      });
231cb0ef41Sopenharmony_ci      push.end('pushed by server data');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci      assert.throws(() => {
261cb0ef41Sopenharmony_ci        push.pushStream({}, common.mustNotCall());
271cb0ef41Sopenharmony_ci      }, {
281cb0ef41Sopenharmony_ci        code: 'ERR_HTTP2_NESTED_PUSH',
291cb0ef41Sopenharmony_ci        name: 'Error'
301cb0ef41Sopenharmony_ci      });
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci      stream.end('test');
331cb0ef41Sopenharmony_ci    }));
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci  stream.respond({
361cb0ef41Sopenharmony_ci    'content-type': 'text/html',
371cb0ef41Sopenharmony_ci    ':status': 200
381cb0ef41Sopenharmony_ci  });
391cb0ef41Sopenharmony_ci}));
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
421cb0ef41Sopenharmony_ci  const port = server.address().port;
431cb0ef41Sopenharmony_ci  const headers = { ':path': '/' };
441cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${port}`);
451cb0ef41Sopenharmony_ci  const req = client.request(headers);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  client.on('stream', common.mustCall((stream, headers) => {
481cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':scheme'], 'http');
491cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':path'], '/foobar');
501cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':authority'], `localhost:${port}`);
511cb0ef41Sopenharmony_ci    stream.on('push', common.mustCall((headers, flags) => {
521cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 200);
531cb0ef41Sopenharmony_ci      assert.strictEqual(headers['content-type'], 'text/html');
541cb0ef41Sopenharmony_ci      assert.strictEqual(headers['x-push-data'], 'pushed by server');
551cb0ef41Sopenharmony_ci      assert.strictEqual(typeof flags === 'number', true);
561cb0ef41Sopenharmony_ci    }));
571cb0ef41Sopenharmony_ci    stream.on('aborted', common.mustNotCall());
581cb0ef41Sopenharmony_ci    // We have to read the data of the push stream to end gracefully.
591cb0ef41Sopenharmony_ci    stream.resume();
601cb0ef41Sopenharmony_ci  }));
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  let data = '';
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  req.on('data', common.mustCall((d) => data += d));
651cb0ef41Sopenharmony_ci  req.on('end', common.mustCall(() => {
661cb0ef41Sopenharmony_ci    assert.strictEqual(data, 'test');
671cb0ef41Sopenharmony_ci    server.close();
681cb0ef41Sopenharmony_ci    client.close();
691cb0ef41Sopenharmony_ci  }));
701cb0ef41Sopenharmony_ci  req.end();
711cb0ef41Sopenharmony_ci}));
72