http: always call response.write() callback by lpinca · Pull Request #27709 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions lib/_http_outgoing.js
16 changes: 10 additions & 6 deletions test/parallel/test-http-outgoing-message-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ const assert = require('assert');
// Fixes: https://github.com/nodejs/node/issues/14381

class Response extends OutgoingMessage {
constructor() {
super({ method: 'GET', httpVersionMajor: 1, httpVersionMinor: 1 });
}

_implicitHeader() {}
}

const res = new Response();

let firstChunk = true;

const ws = new Writable({
write: common.mustCall((chunk, encoding, callback) => {
assert(chunk.toString().match(/hello world/));
if (firstChunk) {
assert(chunk.toString().endsWith('hello world'));
firstChunk = false;
} else {
assert.strictEqual(chunk.length, 0);
}
setImmediate(callback);
})
}, 2)
});

res.socket = ws;
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-http-outgoing-message-write-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

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

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

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

const expected = ['a', 'b', '', Buffer.alloc(0), 'c'];
const results = [];

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

const res = new http.ServerResponse({
method: 'GET',
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);
}));
11 changes: 9 additions & 2 deletions test/parallel/test-http-server-response-standalone.js