From 1249cbcbbbccb3b679bafdb5ea19c58d0d8ae586 Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Tue, 26 Mar 2019 13:37:59 +0800 Subject: [PATCH] Handle response with error status code and without response body --- src/client.ts | 7 +++++++ test/client-test.js | 48 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 06c775f61..56fb4749b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -275,6 +275,13 @@ export class Client extends EventEmitter { // If it's not HTML and Soap Body is empty if (!obj.html && !obj.Body) { + if (response.statusCode >= 400) { + const error: ISoapError = new Error('Error http status codes'); + error.response = response; + error.body = body; + this.emit('soapError', error, eid); + return callback(error, obj, body, obj.Header); + } return callback(null, obj, body, obj.Header); } diff --git a/test/client-test.js b/test/client-test.js index 609e99e0d..2e7926baf 100644 --- a/test/client-test.js +++ b/test/client-test.js @@ -579,7 +579,10 @@ var fs = require('fs'), }); }); - describe('Handle non-success http status codes', function () { + // TODO: + // It seems to be an invalid test case that should be removed. + // It verifies that error should be returned when receiving incorrect response and it's irrelevant to http status code. + describe('Handle invalid http response', function () { var server = null; var hostname = '127.0.0.1'; var port = 15099; @@ -587,7 +590,7 @@ var fs = require('fs'), before(function (done) { server = http.createServer(function (req, res) { - res.statusCode = 401; + res.statusCode = 401; // This test case is nothing to do with status code. Set to 200 doesn't break test. res.write(JSON.stringify({ tempResponse: 'temp' }), 'utf8'); res.end(); }).listen(port, hostname, done); @@ -622,6 +625,47 @@ var fs = require('fs'), }); }); + describe('Handle non-success http status codes without response body', function () { + var server = null; + var hostname = '127.0.0.1'; + var port = 15099; + var baseUrl = 'http://' + hostname + ':' + port; + + before(function (done) { + server = http.createServer(function (req, res) { + res.statusCode = 404; + res.end(); + }).listen(port, hostname, done); + }); + + after(function (done) { + server.close(); + server = null; + done(); + }); + + it('should return an error', function (done) { + soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function (err, client) { + client.MyOperation({}, function (err, result) { + assert.ok(err); + assert.ok(err.response); + done(); + }); + }, baseUrl); + }); + + it('should emit a \'soapError\' event', function (done) { + soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function (err, client) { + client.on('soapError', function (err) { + assert.ok(err); + }); + client.MyOperation({}, function (err, result) { + done(); + }); + }, baseUrl); + }); + }); + describe('Handle HTML answer from non-SOAP server', function () { var server = null; var hostname = '127.0.0.1';