11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst http2 = require('http2');
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst fs = require('fs');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst {
141cb0ef41Sopenharmony_ci  HTTP2_HEADER_CONTENT_TYPE,
151cb0ef41Sopenharmony_ci  HTTP2_HEADER_CONTENT_LENGTH,
161cb0ef41Sopenharmony_ci  HTTP2_HEADER_LAST_MODIFIED
171cb0ef41Sopenharmony_ci} = http2.constants;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst fname = fixtures.path('elipses.txt');
201cb0ef41Sopenharmony_ciconst data = fs.readFileSync(fname);
211cb0ef41Sopenharmony_ciconst stat = fs.statSync(fname);
221cb0ef41Sopenharmony_ciconst fd = fs.openSync(fname, 'r');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst server = http2.createServer();
251cb0ef41Sopenharmony_ciserver.on('stream', (stream) => {
261cb0ef41Sopenharmony_ci  stream.respond({});
271cb0ef41Sopenharmony_ci  stream.end();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  stream.pushStream({
301cb0ef41Sopenharmony_ci    ':path': '/file.txt',
311cb0ef41Sopenharmony_ci    ':method': 'GET'
321cb0ef41Sopenharmony_ci  }, (err, stream) => {
331cb0ef41Sopenharmony_ci    assert.ifError(err);
341cb0ef41Sopenharmony_ci    stream.respondWithFD(fd, {
351cb0ef41Sopenharmony_ci      [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain',
361cb0ef41Sopenharmony_ci      [HTTP2_HEADER_CONTENT_LENGTH]: stat.size,
371cb0ef41Sopenharmony_ci      [HTTP2_HEADER_LAST_MODIFIED]: stat.mtime.toUTCString()
381cb0ef41Sopenharmony_ci    });
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  stream.end();
421cb0ef41Sopenharmony_ci});
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciserver.on('close', common.mustCall(() => fs.closeSync(fd)));
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciserver.listen(0, () => {
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  let expected = 2;
511cb0ef41Sopenharmony_ci  function maybeClose() {
521cb0ef41Sopenharmony_ci    if (--expected === 0) {
531cb0ef41Sopenharmony_ci      server.close();
541cb0ef41Sopenharmony_ci      client.close();
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  const req = client.request({});
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  req.on('response', common.mustCall());
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  client.on('stream', common.mustCall((stream, headers) => {
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    stream.on('push', common.mustCall((headers) => {
651cb0ef41Sopenharmony_ci      assert.strictEqual(headers[HTTP2_HEADER_CONTENT_TYPE], 'text/plain');
661cb0ef41Sopenharmony_ci      assert.strictEqual(+headers[HTTP2_HEADER_CONTENT_LENGTH], data.length);
671cb0ef41Sopenharmony_ci      assert.strictEqual(headers[HTTP2_HEADER_LAST_MODIFIED],
681cb0ef41Sopenharmony_ci                         stat.mtime.toUTCString());
691cb0ef41Sopenharmony_ci    }));
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci    stream.setEncoding('utf8');
721cb0ef41Sopenharmony_ci    let check = '';
731cb0ef41Sopenharmony_ci    stream.on('data', (chunk) => check += chunk);
741cb0ef41Sopenharmony_ci    stream.on('end', common.mustCall(() => {
751cb0ef41Sopenharmony_ci      assert.strictEqual(check, data.toString('utf8'));
761cb0ef41Sopenharmony_ci      maybeClose();
771cb0ef41Sopenharmony_ci    }));
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  }));
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  req.resume();
821cb0ef41Sopenharmony_ci  req.on('end', maybeClose);
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  req.end();
851cb0ef41Sopenharmony_ci});
86