Skip to content

Commit

Permalink
Fix of client events behavior (#1181)
Browse files Browse the repository at this point in the history
* added ability to add to request http header within "request", "message" events;
added accompanying test which test ability to add http header within "request" event;

* fixed lint error
  • Loading branch information
rlavshchenko committed Mar 18, 2022
1 parent c54e125 commit 52a280e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,6 @@ export class Client extends EventEmitter {

options = options || {};

// Add extra headers
if (this.httpHeaders === null) {
headers = {};
} else {
for (const header in this.httpHeaders) { headers[header] = this.httpHeaders[header]; }
for (const attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
}

// Allow the security object to add headers
if (this.security && this.security.addHeaders) {
this.security.addHeaders(headers);
Expand Down Expand Up @@ -474,6 +466,14 @@ export class Client extends EventEmitter {
this.emit('message', message, eid);
this.emit('request', xml, eid);

// Add extra headers
if (this.httpHeaders === null) {
headers = {};
} else {
for (const header in this.httpHeaders) { headers[header] = this.httpHeaders[header]; }
for (const attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
}

const tryJSONparse = (body) => {
try {
return JSON.parse(body);
Expand Down
59 changes: 59 additions & 0 deletions test/header-rely-on-xml-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

var soap = require('..'),
http = require('http'),
assert = require('assert');


describe('testing adding header rely on completed xml', () => {
let server = null;
let hostname = '127.0.0.1';
let port = 15099;
let baseUrl = 'http://' + hostname + ':' + port;
const envelope = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
+ ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'
+ ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+ '<soap:Body><Response>Hello</Response></soap:Body></soap:Envelope>';

before(function (done) {
server = http.createServer(function (req, res) {
res.statusCode = 200;
res.write(envelope, 'utf8');
res.end();
}).listen(port, hostname, done);
});

after(function (done) {
server.close();
server = null;
done();
});

it('should add header to request, which created from xml before request', function (done) {
soap.createClient(__dirname + '/wsdl/complex/registration-common.wsdl', function (err, client) {
if (err) {
return void done(err);
}
assert.ok(client);

const testHeaderKey = 'testHeader';
let testHeaderValue;

client.on('request', (xml) => {
testHeaderValue = xml;
client.addHttpHeader(testHeaderKey, xml);
});

client.registerUser('', function (err, result) {

const header = result.request._header;
const headerKey = header.includes(testHeaderKey);
const headerValue = header.includes(testHeaderValue);

assert.equal(headerKey, true);
assert.equal(headerValue, true);
done();
});
}, baseUrl);
});
});

0 comments on commit 52a280e

Please sign in to comment.