Skip to content

Commit

Permalink
Handle response with error status code and without response body (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleung authored and jsdevel committed Mar 26, 2019
1 parent 58c0b8a commit 1f4af8a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/client.ts
Expand Up @@ -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);
}

Expand Down
48 changes: 46 additions & 2 deletions test/client-test.js
Expand Up @@ -579,15 +579,18 @@ 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;
var baseUrl = 'http://' + hostname + ':' + port;

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);
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 1f4af8a

Please sign in to comment.