From d9ed4b526b90cf53eecce07d2ee85e4f1115723e Mon Sep 17 00:00:00 2001 From: Ratchanan Srirattanamet Date: Fri, 8 Feb 2019 18:10:48 +0700 Subject: [PATCH] server: send response to client with res.end() Using res.end() to write result instead of res.write() followed by res.end() will cause node not to use chunked encoding and include Content-Length header. (See nodejs/node#26005) This small distinction is important with some client. For example, Windows 10's MDM enrollment system will not accept chunked response (https://docs.microsoft.com/en-us/windows/client-management/mdm/on-premise-authentication-device-enrollment). This might improve compatibility with some other clients, too. However, there's a small chance that some client may expect the response to be chunked. --- lib/server.js | 9 +++------ test/server-test.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/server.js b/lib/server.js index a5098d073..aa4eab059 100644 --- a/lib/server.js +++ b/lib/server.js @@ -147,8 +147,7 @@ Server.prototype._processRequestXml = function (req, res, xml) { if (statusCode) { res.statusCode = statusCode; } - res.write(result); - res.end(); + res.end(result); if (typeof self.log === 'function') { self.log("replied", result); } @@ -157,8 +156,7 @@ Server.prototype._processRequestXml = function (req, res, xml) { if (err.Fault !== undefined) { return self._sendError(err.Fault, function (result, statusCode) { res.statusCode = statusCode || 500; - res.write(result); - res.end(); + res.end(result); if (typeof self.log === 'function') { self.log("error", err); } @@ -166,8 +164,7 @@ Server.prototype._processRequestXml = function (req, res, xml) { } else { error = err.stack ? (self.suppressStack === true ? err.message : err.stack) : err; res.statusCode = 500; - res.write(error); - res.end(); + res.end(error); if (typeof self.log === 'function') { self.log("error", error); } diff --git a/test/server-test.js b/test/server-test.js index 23023260b..a24f7693e 100644 --- a/test/server-test.js +++ b/test/server-test.js @@ -483,6 +483,25 @@ describe('SOAP Server', function() { }); }); + it('should not use chunked encoding and return Content-Length header', function(done) { + soap.createClient(test.baseUrl + '/stockquote?wsdl', function(err, client) { + var clientArgs = { tickerSymbol: 'AAPL'}; + + assert.ifError(err); + + client.on('response', function(body, response, eid) { + var headers = response.headers; + assert.notEqual(headers['content-length'], undefined); + assert.equal(headers['transfer-encoding'], undefined); + }) + + client.GetLastTradePrice(clientArgs, function(err, result, raw, headers) { + assert.ifError(err); + done(); + }); + }); + }); + // NOTE: this is actually a -client- test /* it('should return a valid error if the server stops responding': function(done) {