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_ci
111cb0ef41Sopenharmony_ciserver.on('stream', (s) => {
121cb0ef41Sopenharmony_ci  assert(s.pushAllowed);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  s.pushStream({ ':path': '/file' }, common.mustSucceed((pushStream) => {
151cb0ef41Sopenharmony_ci    pushStream.respond();
161cb0ef41Sopenharmony_ci    pushStream.end('a push stream');
171cb0ef41Sopenharmony_ci  }));
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  s.respond();
201cb0ef41Sopenharmony_ci  s.end('hello world');
211cb0ef41Sopenharmony_ci});
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciserver.listen(0, () => {
241cb0ef41Sopenharmony_ci  server.unref();
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const url = `http://localhost:${server.address().port}`;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const client = http2.connect(url);
291cb0ef41Sopenharmony_ci  const req = client.request();
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  let pushStream;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  client.on('stream', common.mustCall((s, headers) => {
341cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':path'], '/file');
351cb0ef41Sopenharmony_ci    pushStream = s;
361cb0ef41Sopenharmony_ci  }));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  req.on('response', common.mustCall((headers) => {
391cb0ef41Sopenharmony_ci    let pushData = '';
401cb0ef41Sopenharmony_ci    pushStream.setEncoding('utf8');
411cb0ef41Sopenharmony_ci    pushStream.on('data', (d) => pushData += d);
421cb0ef41Sopenharmony_ci    pushStream.on('end', common.mustCall(() => {
431cb0ef41Sopenharmony_ci      assert.strictEqual(pushData, 'a push stream');
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci      // Removing the setImmediate causes the test to pass
461cb0ef41Sopenharmony_ci      setImmediate(function() {
471cb0ef41Sopenharmony_ci        let data = '';
481cb0ef41Sopenharmony_ci        req.setEncoding('utf8');
491cb0ef41Sopenharmony_ci        req.on('data', (d) => data += d);
501cb0ef41Sopenharmony_ci        req.on('end', common.mustCall(() => {
511cb0ef41Sopenharmony_ci          assert.strictEqual(data, 'hello world');
521cb0ef41Sopenharmony_ci          client.close();
531cb0ef41Sopenharmony_ci        }));
541cb0ef41Sopenharmony_ci      });
551cb0ef41Sopenharmony_ci    }));
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci});
58