Skip to content

Commit

Permalink
feat add(client) accepts soapHeaders property upon method invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
marioestradarosa committed Jun 5, 2020
1 parent 8f468d9 commit 46aaf84
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/client.js
Expand Up @@ -122,7 +122,9 @@ class Client extends Base {
soapAction,
headers = {
'Content-Type': 'text/xml; charset=utf-8'
};
},
extraSoapHeaderProperty = ['soapHeaders'],
extraSoapHeaders = {};

debug('client request. operation: %s args: %j options: %j extraHeaders: %j', operation.name, args, options, extraHeaders);

Expand Down Expand Up @@ -155,14 +157,27 @@ class Client extends Base {
options = options || {};
debugSensitive('client request. options: %j', options);

//Add extra headers
extraSoapHeaders = _.merge(_.pick(extraHeaders, extraSoapHeaderProperty),
_.pick(options, extraSoapHeaderProperty));

debug('Soap xml payload extra headers: %j', extraSoapHeaders);

extraHeaders = _.omit(extraHeaders, extraSoapHeaderProperty);
options = _.omit(options, extraSoapHeaderProperty);

//Add extra headers to http request
for (var header in this.httpHeaders) {
headers[header] = this.httpHeaders[header];
}
for (var attr in extraHeaders) {
headers[attr] = extraHeaders[attr];
}

// Add extra headers to SOAP xml payload
for (var attr in extraSoapHeaders) {
this.addSoapHeader(extraSoapHeaders[attr]);
}

debug('client request. headers: %j', headers);

//Unlike other security objects, NTLMSecurity is passed in through client options rather than client.setSecurity(ntlmSecurity) as some
Expand Down
56 changes: 56 additions & 0 deletions test/client-test.js
Expand Up @@ -314,6 +314,62 @@ describe('SOAP Client', function() {
}, 'http://' + hostname + ':' + server.address().port );
});

it('should add soap headers from options', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
assert.ok(client);
assert.ok(client.getSoapHeaders().length === 0);
var soapheader = {
'esnext': false,
'moz': true,
'boss': true,
'node': true,
'validthis': true,
'globals': {
'EventEmitter': true,
'Promise': true
}
};
var lastRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Header>\n"
+ " <esnext>false</esnext>\n <moz>true</moz>\n <boss>true</boss>\n <node>true</node>\n <validthis>true</validthis>\n <globals>\n <EventEmitter>true</EventEmitter>\n <Promise>true</Promise>\n </globals>\n"
+ " </soap:Header>\n <soap:Body/>\n</soap:Envelope>";
client.MyOperation({}, function(err, result) {
//using lastRequest instead of lastRequestHeaders() since this doesn't contain soap header which this test case needs to test.
assert.equal(client.lastRequest, lastRequest);
done();
}, {
soapHeaders: soapheader
});
}, 'http://' + hostname + ':' + server.address().port );
});

it('should add soap headers from extraHeaders', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
assert.ok(client);
assert.ok(client.getSoapHeaders().length === 0);
var soapheader = {
'esnext': false,
'moz': true,
'boss': true,
'node': true,
'validthis': true,
'globals': {
'EventEmitter': true,
'Promise': true
}
};
var lastRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Header>\n"
+ " <esnext>false</esnext>\n <moz>true</moz>\n <boss>true</boss>\n <node>true</node>\n <validthis>true</validthis>\n <globals>\n <EventEmitter>true</EventEmitter>\n <Promise>true</Promise>\n </globals>\n"
+ " </soap:Header>\n <soap:Body/>\n</soap:Envelope>";
client.MyOperation({}, function(err, result) {
//using lastRequest instead of lastRequestHeaders() since this doesn't contain soap header which this test case needs to test.
assert.equal(client.lastRequest, lastRequest);
done();
}, undefined, {
soapHeaders: soapheader
});
}, 'http://' + hostname + ':' + server.address().port );
});

it('should add soap headers', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
assert.ok(client);
Expand Down
20 changes: 20 additions & 0 deletions test/server-test.js
Expand Up @@ -347,6 +347,26 @@ describe('SOAP Server', function() {
});
});

it('should emit \'headers\' event from options', function(done) {
test.soapServer.on('headers', function headersManager(headers, methodName) {
assert.equal(methodName, 'GetLastTradePrice');
headers.SomeToken = 0;
});
soap.createClient(test.baseUrl + '/stockquote?wsdl', function(err, client) {
assert.ok(!err);
// client.addSoapHeader('<SomeToken>123.45</SomeToken>');
client.GetLastTradePrice({TradePriceRequest: { tickerSymbol: 'AAPL'}}, function(err, result) {
assert.ok(!err);
assert.equal(0, parseFloat(result.price));
done();
}, {
soapHeaders: {
SomeToken: 123.45
}
});
});
});

it('should not emit the \'headers\' event when there are no headers', function(done) {
test.soapServer.on('headers', function headersManager(headers, methodName) {
assert.ok(false);
Expand Down

0 comments on commit 46aaf84

Please sign in to comment.