Skip to content

Commit 9eb76c4

Browse files
author
Gamze Oguz
committed
Added custom envelope key option for server
1 parent 4660fa2 commit 9eb76c4

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
@@ -184,6 +184,7 @@ Note: for versions of node >0.10.X, you may need to specify `{connection: 'keep-
184184
- `crl` (*string* | *string[]*: PEM encoded CRLs (Certificate Revocation List)
185185
- `ciphers` (*string*): A description of the ciphers to use or exclude, separated by `:`. The default cipher suite is:
186186
- `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`)
187+
- `envelopeKey` (*string*): Set a custom envelope key. (**Default:** `'soap'`)
187188
- `services` (*Object*)
188189
- `wsdl` (*string*): An XML string that defines the service.
189190
- `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
@@ -154,6 +154,7 @@ export interface IServerOptions extends IWsdlBaseOptions {
154154
oneWay?: IOneWayOptions;
155155
/** 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. */
156156
enableChunkedEncoding?: boolean;
157+
envelopeKey?: string;
157158
}
158159

159160
export interface IMTOMAttachments {

test/server-options-test.js

+70
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,74 @@ describe('SOAP Server with Options', function () {
592592
});
593593
});
594594

595+
it('should return soapenv as envelope key when it is set to soapenv', function(done) {
596+
test.server.listen(15099, null, null, function() {
597+
test.soapServer = soap.listen(test.server, {
598+
path: '/stockquote',
599+
services: test.service,
600+
xml: test.wsdl,
601+
uri: __dirname + '/wsdl/strict/',
602+
envelopeKey: 'soapenv'
603+
}, test.service, test.wsdl);
604+
test.baseUrl = 'http://' + test.server.address().address + ":" + test.server.address().port;
605+
606+
//windows return 0.0.0.0 as address and that is not
607+
//valid to use in a request
608+
if (test.server.address().address === '0.0.0.0' || test.server.address().address === '::') {
609+
test.baseUrl = 'http://127.0.0.1:' + test.server.address().port;
610+
}
611+
// console.log(test.baseUrl);
612+
request.post({
613+
url: test.baseUrl + '/stockquote',
614+
body: '<soapenv:Envelope' +
615+
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
616+
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
617+
' <soapenv:Header/>' +
618+
' <soapenv:Body>' +
619+
'</soapenv:Envelope>'
620+
}, function(err, res, body) {
621+
assert.ifError(err);
622+
assert.ok(
623+
body.indexOf('soapenv:Envelope') > -1
624+
)
625+
done();
626+
});
627+
});
628+
});
629+
630+
it('should return soap as envelope key by default', function(done) {
631+
test.server.listen(15099, null, null, function() {
632+
test.soapServer = soap.listen(test.server, {
633+
path: '/stockquote',
634+
services: test.service,
635+
xml: test.wsdl,
636+
uri: __dirname + '/wsdl/strict/',
637+
forceSoap12Headers : true
638+
}, test.service, test.wsdl);
639+
test.baseUrl = 'http://' + test.server.address().address + ":" + test.server.address().port;
640+
641+
//windows return 0.0.0.0 as address and that is not
642+
//valid to use in a request
643+
if (test.server.address().address === '0.0.0.0' || test.server.address().address === '::') {
644+
test.baseUrl = 'http://127.0.0.1:' + test.server.address().port;
645+
}
646+
// console.log(test.baseUrl);
647+
request.post({
648+
url: test.baseUrl + '/stockquote',
649+
body: '<soapenv:Envelope' +
650+
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
651+
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
652+
' <soapenv:Header/>' +
653+
' <soapenv:Body>' +
654+
'</soapenv:Envelope>'
655+
}, function(err, res, body) {
656+
assert.ifError(err);
657+
assert.ok(
658+
body.indexOf('soap:Envelope') > -1
659+
)
660+
done();
661+
});
662+
});
663+
});
664+
595665
});

0 commit comments

Comments
 (0)