summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuigi Pinca <luigipinca@gmail.com>2019-05-20 10:29:03 +0200
committerRich Trott <rtrott@gmail.com>2019-06-01 15:18:06 +0200
commit2a850cd0664a4eee51f44d0bb8c2f7a3fe444154 (patch)
tree2eb2566f74f4516ab5000ca2807eda590d47f29c
parent74b498682284c7393df018ff026ce1687a75ebb8 (diff)
downloadandroid-node-v8-2a850cd0664a4eee51f44d0bb8c2f7a3fe444154.tar.gz
android-node-v8-2a850cd0664a4eee51f44d0bb8c2f7a3fe444154.tar.bz2
android-node-v8-2a850cd0664a4eee51f44d0bb8c2f7a3fe444154.zip
http: call write callback even if there is no message body
Ensure that the callback of `OutgoingMessage.prototype.write()` is called when `outgoingMessage._hasBody` is `false` (HEAD method, 204 status code, etc.). Refs: https://github.com/nodejs/node/pull/27709 PR-URL: https://github.com/nodejs/node/pull/27777 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
-rw-r--r--lib/_http_outgoing.js1
-rw-r--r--test/parallel/test-http-outgoing-message-write-callback.js46
2 files changed, 25 insertions, 22 deletions
diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js
index cb09e764fe..a4a2b3ab14 100644
--- a/lib/_http_outgoing.js
+++ b/lib/_http_outgoing.js
@@ -573,6 +573,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
if (!msg._hasBody) {
debug('This type of response MUST NOT have a body. ' +
'Ignoring write() calls.');
+ if (callback) process.nextTick(callback);
return true;
}
diff --git a/test/parallel/test-http-outgoing-message-write-callback.js b/test/parallel/test-http-outgoing-message-write-callback.js
index a51d37351d..3a32285faa 100644
--- a/test/parallel/test-http-outgoing-message-write-callback.js
+++ b/test/parallel/test-http-outgoing-message-write-callback.js
@@ -3,35 +3,37 @@
const common = require('../common');
// This test ensures that the callback of `OutgoingMessage.prototype.write()` is
-// called also when writing empty chunks.
+// 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');
-const expected = ['a', 'b', '', Buffer.alloc(0), 'c'];
-const results = [];
+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) {
- setImmediate(callback);
- }
-});
+ const writable = new stream.Writable({
+ write(chunk, encoding, callback) {
+ callback();
+ }
+ });
-const res = new http.ServerResponse({
- method: 'GET',
- httpVersionMajor: 1,
- httpVersionMinor: 1
-});
+ const res = new http.ServerResponse({
+ method: method,
+ httpVersionMajor: 1,
+ httpVersionMinor: 1
+ });
-res.assignSocket(writable);
+ res.assignSocket(writable);
-for (const chunk of expected) {
- res.write(chunk, () => {
- results.push(chunk);
- });
-}
+ for (const chunk of expected) {
+ res.write(chunk, () => {
+ results.push(chunk);
+ });
+ }
-res.end(common.mustCall(() => {
- assert.deepStrictEqual(results, expected);
-}));
+ res.end(common.mustCall(() => {
+ assert.deepStrictEqual(results, expected);
+ }));
+}