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

Add name to event data #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,20 @@ Client.prototype._invoke = function(method, args, location, callback, options, e
self.lastRequest = xml;

self.emit('message', message);
self.emit('request', xml);
self.emit('request', xml, name);

req = self.httpClient.request(location, xml, function(err, response, body) {
var result;
var obj;
self.lastResponse = body;
self.lastResponseHeaders = response && response.headers;
self.emit('response', body);
self.emit('response', body, name);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also combine this and above into soapSuccess, so the request and response are correlated into one entry?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new event soapSuccess? We could. Then we only need to handle 2 events. But I assume we don't want to remove any existing events for backward compatibility.


if (err) {
err.methodName = name;
err.request = xml;
err.response = body;
self.emit('soapError', err);
callback(err);
} else {
try {
Expand All @@ -235,6 +239,8 @@ Client.prototype._invoke = function(method, args, location, callback, options, e
}
error.response = response;
error.body = body;
error.request = xml;
error.methodName = name;
self.emit('soapError', error);
return callback(error, response, body, null);
}
Expand Down
31 changes: 25 additions & 6 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ describe('SOAP Client', function() {
});
}, baseUrl);
});

it('should emit a \'soapError\' event with methodName', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
var didEmitEvent = false;
client.on('soapError', function(err) {
didEmitEvent = true;
assert.equal(err.methodName, 'MyOperation');
});
client.MyOperation({}, function(err, result) {
assert.ok(didEmitEvent);
done();
});
}, baseUrl);
});
});

describe('Handle non-success http status codes', function() {
Expand Down Expand Up @@ -384,10 +398,12 @@ describe('SOAP Client', function() {
}, baseUrl);
});

it('should emit a \'soapError\' event', function (done) {
it('should emit a \'soapError\' event with methodName', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
client.on('soapError', function(err) {
assert.ok(err);
console.log({err:err});
assert.equal(err.methodName, 'MyOperation');
});
client.MyOperation({}, function(err, result) {
done();
Expand Down Expand Up @@ -465,14 +481,15 @@ describe('SOAP Client', function() {
}, baseUrl);
});

it('Should emit the "request" event with entire XML message', function (done) {
it('Should emit the "request" event with entire XML message and name', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
var didEmitEvent = false;
client.on('request', function (xml) {
client.on('request', function (xml, name) {
didEmitEvent = true;
// Should contain entire soap message
assert.equal(typeof xml, 'string');
assert.notEqual(xml.indexOf('soap:Envelope'), -1);
assert.equal(name, 'MyOperation');
});

client.MyOperation({}, function() {
Expand All @@ -482,14 +499,15 @@ describe('SOAP Client', function() {
}, baseUrl);
});

it('Should emit the "response" event with Soap Body string', function (done) {
it('Should emit the "response" event with Soap Body string and name', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
var didEmitEvent = false;
client.on('response', function (xml) {
client.on('response', function (xml, name) {
didEmitEvent = true;
// Should contain entire soap message
assert.equal(typeof xml, 'string');
assert.equal(xml.indexOf('soap:Envelope'), -1);
assert.equal(name, 'MyOperation');
});

client.MyOperation({}, function() {
Expand All @@ -499,12 +517,13 @@ describe('SOAP Client', function() {
}, baseUrl);
});

it('should emit a \'soapError\' event', function (done) {
it('should emit a \'soapError\' event with methodName', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
var didEmitEvent = false;
client.on('soapError', function(err) {
didEmitEvent = true;
assert.ok(err.root.Envelope.Body.Fault);
assert.equal(err.methodName, 'MyOperation');
});
client.MyOperation({}, function(err, result) {
assert.ok(didEmitEvent);
Expand Down