Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle response with error status code and without response body #1053

Merged
merged 1 commit into from Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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