summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-respond-file-with-pipe.js
blob: d0557c2fd01e3adc568ee32d90aec3cad8b8eaae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
if (common.isWindows)
  common.skip('no mkfifo on Windows');
const child_process = require('child_process');
const path = require('path');
const fs = require('fs');
const http2 = require('http2');
const assert = require('assert');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const pipeName = path.join(tmpdir.path, 'pipe');

const mkfifo = child_process.spawnSync('mkfifo', [ pipeName ]);
if (mkfifo.error && mkfifo.error.code === 'ENOENT') {
  common.skip('missing mkfifo');
}

const server = http2.createServer();
server.on('stream', (stream) => {
  stream.respondWithFile(pipeName, {
    'content-type': 'text/plain'
  }, {
    onError: common.mustNotCall(),
    statCheck: common.mustCall()
  });
});

server.listen(0, () => {
  const client = http2.connect(`http://localhost:${server.address().port}`);
  const req = client.request();

  req.on('response', common.mustCall((headers) => {
    assert.strictEqual(headers[':status'], 200);
  }));
  let body = '';
  req.setEncoding('utf8');
  req.on('data', (chunk) => body += chunk);
  req.on('end', common.mustCall(() => {
    assert.strictEqual(body, 'Hello, world!\n');
    client.close();
    server.close();
  }));
  req.end();
});

fs.open(pipeName, 'w', common.mustCall((err, fd) => {
  assert.ifError(err);
  fs.writeSync(fd, 'Hello, world!\n');
  fs.closeSync(fd);
}));