Skip to content

Commit 629110b

Browse files
Gamze Oguzw666
Gamze Oguz
authored andcommitted
Added custom envelope key option for server
1 parent 08cecb5 commit 629110b

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ Note: for versions of node >0.10.X, you may need to specify `{connection: 'keep-
229229
- `crl` (*string* | *string[]*: PEM encoded CRLs (Certificate Revocation List)
230230
- `ciphers` (*string*): A description of the ciphers to use or exclude, separated by `:`. The default cipher suite is:
231231
- `enableChunkedEncoding` (*boolean*): Controls chunked transfer encoding in response. Some clients (such as Windows 10's MDM enrollment SOAP client) are sensitive to transfer-encoding mode and can't accept chunked response. This option lets users disable chunked transfer encoding for such clients. (**Default:** `true`)
232+
- `envelopeKey` (*string*): Set a custom envelope key. (**Default:** `'soap'`)
232233
- `services` (*Object*)
233234
- `wsdl` (*string*): An XML string that defines the service.
234235
- `callback` (*Function*): A function to run after the server has been initialized.

src/server.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export class Server extends EventEmitter {
199199
this.wsdl.options.attributesKey = options.attributesKey || 'attributes';
200200
this.onewayOptions.statusCode = this.onewayOptions.responseCode || 200;
201201
this.onewayOptions.emptyBody = !!this.onewayOptions.emptyBody;
202+
this.wsdl.options.envelopeKey = options.envelopeKey || 'soap';
202203
}
203204

204205
private _processRequestXml(req: Request, res: Response, xml) {
@@ -599,13 +600,14 @@ export class Server extends EventEmitter {
599600
const ns = defs.$targetNamespace;
600601
const encoding = '';
601602
const alias = findPrefix(defs.xmlns, ns);
603+
const envelopeKey = this.wsdl.options.envelopeKey;
602604

603605
const envelopeDefinition = this.wsdl.options.forceSoap12Headers
604606
? 'http://www.w3.org/2003/05/soap-envelope'
605607
: 'http://schemas.xmlsoap.org/soap/envelope/';
606608

607609
let xml = '<?xml version="1.0" encoding="utf-8"?>' +
608-
'<soap:Envelope xmlns:soap="' + envelopeDefinition + '" ' +
610+
'<' + envelopeKey + ':Envelope' + ' xmlns:' + envelopeKey + '=' + ' "' + envelopeDefinition + '" ' +
609611
encoding +
610612
this.wsdl.xmlnsInEnvelope + '>';
611613

@@ -627,12 +629,10 @@ export class Server extends EventEmitter {
627629
}
628630

629631
if (headers !== '') {
630-
xml += '<soap:Header>' + headers + '</soap:Header>';
632+
xml += '<' + envelopeKey + ':Header>' + headers + '</' + envelopeKey + ':Header>';
631633
}
632-
633-
xml += body ? '<soap:Body>' + body + '</soap:Body>' : '<soap:Body/>';
634-
635-
xml += '</soap:Envelope>';
634+
xml += body ? '<' + envelopeKey + ':Body>' + body + '</' + envelopeKey + ':Body>' : '<' + envelopeKey + ':Body/>';
635+
xml += '</' + envelopeKey + ':Envelope>';
636636
return xml;
637637
}
638638

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export interface IServerOptions extends IWsdlBaseOptions {
156156
oneWay?: IOneWayOptions;
157157
/** A boolean for controlling chunked transfer encoding in response. Some client (such as Windows 10's MDM enrollment SOAP client) is sensitive to transfer-encoding mode and can't accept chunked response. This option let user disable chunked transfer encoding for such a client. Default to true for backward compatibility. */
158158
enableChunkedEncoding?: boolean;
159+
envelopeKey?: string;
159160
}
160161

161162
export interface IMTOMAttachments {

test/server-options-test.js

+70
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,74 @@ describe('SOAP Server with Options', function () {
625625
});
626626
});
627627

628+
it('should return soapenv as envelope key when it is set to soapenv', function(done) {
629+
test.server.listen(15099, null, null, function() {
630+
test.soapServer = soap.listen(test.server, {
631+
path: '/stockquote',
632+
services: test.service,
633+
xml: test.wsdl,
634+
uri: __dirname + '/wsdl/strict/',
635+
envelopeKey: 'soapenv'
636+
}, test.service, test.wsdl);
637+
test.baseUrl = 'http://' + test.server.address().address + ":" + test.server.address().port;
638+
639+
//windows return 0.0.0.0 as address and that is not
640+
//valid to use in a request
641+
if (test.server.address().address === '0.0.0.0' || test.server.address().address === '::') {
642+
test.baseUrl = 'http://127.0.0.1:' + test.server.address().port;
643+
}
644+
// console.log(test.baseUrl);
645+
request.post({
646+
url: test.baseUrl + '/stockquote',
647+
body: '<soapenv:Envelope' +
648+
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
649+
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
650+
' <soapenv:Header/>' +
651+
' <soapenv:Body>' +
652+
'</soapenv:Envelope>'
653+
}, function(err, res, body) {
654+
assert.ifError(err);
655+
assert.ok(
656+
body.indexOf('soapenv:Envelope') > -1
657+
)
658+
done();
659+
});
660+
});
661+
});
662+
663+
it('should return soap as envelope key by default', function(done) {
664+
test.server.listen(15099, null, null, function() {
665+
test.soapServer = soap.listen(test.server, {
666+
path: '/stockquote',
667+
services: test.service,
668+
xml: test.wsdl,
669+
uri: __dirname + '/wsdl/strict/',
670+
forceSoap12Headers : true
671+
}, test.service, test.wsdl);
672+
test.baseUrl = 'http://' + test.server.address().address + ":" + test.server.address().port;
673+
674+
//windows return 0.0.0.0 as address and that is not
675+
//valid to use in a request
676+
if (test.server.address().address === '0.0.0.0' || test.server.address().address === '::') {
677+
test.baseUrl = 'http://127.0.0.1:' + test.server.address().port;
678+
}
679+
// console.log(test.baseUrl);
680+
request.post({
681+
url: test.baseUrl + '/stockquote',
682+
body: '<soapenv:Envelope' +
683+
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
684+
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
685+
' <soapenv:Header/>' +
686+
' <soapenv:Body>' +
687+
'</soapenv:Envelope>'
688+
}, function(err, res, body) {
689+
assert.ifError(err);
690+
assert.ok(
691+
body.indexOf('soap:Envelope') > -1
692+
)
693+
done();
694+
});
695+
});
696+
});
697+
628698
});

0 commit comments

Comments
 (0)