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

Adding custom envelope key option for server #1208

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ Note: for versions of node >0.10.X, you may need to specify `{connection: 'keep-
- `crl` (*string* | *string[]*: PEM encoded CRLs (Certificate Revocation List)
- `ciphers` (*string*): A description of the ciphers to use or exclude, separated by `:`. The default cipher suite is:
- `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`)
- `envelopeKey` (*string*): Set a custom envelope key. (**Default:** `'soap'`)
- `services` (*Object*)
- `wsdl` (*string*): An XML string that defines the service.
- `callback` (*Function*): A function to run after the server has been initialized.
Expand Down
12 changes: 6 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export class Server extends EventEmitter {
this.wsdl.options.attributesKey = options.attributesKey || 'attributes';
this.onewayOptions.statusCode = this.onewayOptions.responseCode || 200;
this.onewayOptions.emptyBody = !!this.onewayOptions.emptyBody;
this.wsdl.options.envelopeKey = options.envelopeKey || 'soap';
}

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

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

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

Expand All @@ -627,12 +629,10 @@ export class Server extends EventEmitter {
}

if (headers !== '') {
xml += '<soap:Header>' + headers + '</soap:Header>';
xml += '<' + envelopeKey + ':Header>' + headers + '</' + envelopeKey + ':Header>';
}

xml += body ? '<soap:Body>' + body + '</soap:Body>' : '<soap:Body/>';

xml += '</soap:Envelope>';
xml += body ? '<' + envelopeKey + ':Body>' + body + '</' + envelopeKey + ':Body>' : '<' + envelopeKey + ':Body/>';
xml += '</' + envelopeKey + ':Envelope>';
return xml;
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export interface IServerOptions extends IWsdlBaseOptions {
oneWay?: IOneWayOptions;
/** 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. */
enableChunkedEncoding?: boolean;
envelopeKey?: string;
}

export interface IMTOMAttachments {
Expand Down
68 changes: 68 additions & 0 deletions test/server-options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,72 @@ describe('SOAP Server with Options', function () {
});
});

it('should return soapenv as envelope key when it is set to soapenv', function (done) {
test.server.listen(15099, null, null, function () {
test.soapServer = soap.listen(test.server, {
path: '/stockquote',
services: test.service,
xml: test.wsdl,
uri: __dirname + '/wsdl/strict/',
envelopeKey: 'soapenv'
}, test.service, test.wsdl);
test.baseUrl = 'http://' + test.server.address().address + ":" + test.server.address().port;

//windows return 0.0.0.0 as address and that is not
//valid to use in a request
if (test.server.address().address === '0.0.0.0' || test.server.address().address === '::') {
test.baseUrl = 'http://127.0.0.1:' + test.server.address().port;
}
// console.log(test.baseUrl);
axios.post(
test.baseUrl + '/stockquote',
'<soapenv:Envelope' +
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
' <soapenv:Header/>' +
' <soapenv:Body>' +
'</soapenv:Envelope>'
).then(res => {
assert.ok(res.data.indexOf('soapenv:Envelope') > -1);
done();
}).catch(err => {
throw err;
});
});
});

it('should return soap as envelope key by default', function (done) {
test.server.listen(15099, null, null, function () {
test.soapServer = soap.listen(test.server, {
path: '/stockquote',
services: test.service,
xml: test.wsdl,
uri: __dirname + '/wsdl/strict/',
forceSoap12Headers: true
}, test.service, test.wsdl);
test.baseUrl = 'http://' + test.server.address().address + ":" + test.server.address().port;

//windows return 0.0.0.0 as address and that is not
//valid to use in a request
if (test.server.address().address === '0.0.0.0' || test.server.address().address === '::') {
test.baseUrl = 'http://127.0.0.1:' + test.server.address().port;
}
// console.log(test.baseUrl);
axios.post(
test.baseUrl + '/stockquote',
'<soapenv:Envelope' +
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
' <soapenv:Header/>' +
' <soapenv:Body>' +
'</soapenv:Envelope>'
).then(res => {
assert.ok(res.data.indexOf('soap:Envelope') > -1);
done();
}).catch(err => {
throw err;
});
});
});

});
Loading