summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-outgoing-message-write-callback.js
blob: 3a32285faaff557803e63bf08bd55a1174aad630 (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
'use strict';

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

// This test ensures that the callback of `OutgoingMessage.prototype.write()` is
// called also when writing empty chunks or when the message has no body.

const assert = require('assert');
const http = require('http');
const stream = require('stream');

for (const method of ['GET, HEAD']) {
  const expected = ['a', 'b', '', Buffer.alloc(0), 'c'];
  const results = [];

  const writable = new stream.Writable({
    write(chunk, encoding, callback) {
      callback();
    }
  });

  const res = new http.ServerResponse({
    method: method,
    httpVersionMajor: 1,
    httpVersionMinor: 1
  });

  res.assignSocket(writable);

  for (const chunk of expected) {
    res.write(chunk, () => {
      results.push(chunk);
    });
  }

  res.end(common.mustCall(() => {
    assert.deepStrictEqual(results, expected);
  }));
}