• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const fixtures = require('../common/fixtures');
5if (!common.hasCrypto)
6  common.skip('missing crypto');
7const http2 = require('http2');
8const assert = require('assert');
9const fs = require('fs');
10
11const {
12  HTTP2_HEADER_CONTENT_TYPE,
13  HTTP2_HEADER_CONTENT_LENGTH
14} = http2.constants;
15
16const fname = fixtures.path('elipses.txt');
17const data = fs.readFileSync(fname);
18const stat = fs.statSync(fname);
19const fd = fs.openSync(fname, 'r');
20
21const server = http2.createServer();
22server.on('stream', (stream) => {
23  stream.respondWithFD(fd, {
24    [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain',
25    [HTTP2_HEADER_CONTENT_LENGTH]: stat.size,
26  });
27});
28server.on('close', common.mustCall(() => fs.closeSync(fd)));
29server.listen(0, () => {
30
31  const client = http2.connect(`http://localhost:${server.address().port}`);
32  const req = client.request();
33
34  req.on('response', common.mustCall((headers) => {
35    assert.strictEqual(headers[HTTP2_HEADER_CONTENT_TYPE], 'text/plain');
36    assert.strictEqual(+headers[HTTP2_HEADER_CONTENT_LENGTH], data.length);
37  }));
38  req.setEncoding('utf8');
39  let check = '';
40  req.on('data', (chunk) => check += chunk);
41  req.on('end', common.mustCall(() => {
42    assert.strictEqual(check, data.toString('utf8'));
43    client.close();
44    server.close();
45  }));
46  req.end();
47});
48