summaryrefslogtreecommitdiff
path: root/test/parallel/test-pipe-outgoing-message-data-emitted-after-ended.js
blob: c6191a906b32d681249c4b161f6d51d241585693 (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
'use strict';
const common = require('../common');
const http = require('http');
const stream = require('stream');

// Verify that when piping a stream to an `OutgoingMessage` (or a type that
// inherits from `OutgoingMessage`), if data is emitted after the
// `OutgoingMessage` was closed - a `write after end` error is raised

class MyStream extends stream {}

const server = http.createServer(common.mustCall(function(req, res) {
  const myStream = new MyStream();
  myStream.pipe(res);

  process.nextTick(common.mustCall(() => {
    res.end();
    myStream.emit('data', 'some data');
    res.on('error', common.expectsError({
      code: 'ERR_STREAM_WRITE_AFTER_END',
      type: Error
    }));

    process.nextTick(common.mustCall(() => server.close()));
  }));
}));

server.listen(0);

server.on('listening', common.mustCall(function() {
  http.request({
    port: server.address().port,
    method: 'GET',
    path: '/'
  }).end();
}));