From 80f4157ccdfe5c820e6453cb2e65f9a068561750 Mon Sep 17 00:00:00 2001 From: bonanzakrak Date: Wed, 5 Jun 2019 18:31:03 +0200 Subject: [PATCH] added possibility to add action to content-type header (#1073) --- src/client.ts | 14 ++++++++------ src/types.ts | 2 ++ src/wsdl/index.ts | 1 + test/client-test.js | 17 +++++++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/client.ts b/src/client.ts index e5356994f..4486fb893 100644 --- a/src/client.ts +++ b/src/client.ts @@ -194,6 +194,7 @@ export class Client extends EventEmitter { this.wsdl.options.overrideRootElement = options.overrideRootElement; } this.wsdl.options.forceSoap12Headers = !!options.forceSoap12Headers; + this.wsdl.options.addHeadersAction = !!options.addHeadersAction; } private _defineService(service: ServiceElement, endpoint?: string) { @@ -351,11 +352,6 @@ export class Client extends EventEmitter { return finish(obj, body, response); }; - if (this.wsdl.options.forceSoap12Headers) { - headers['Content-Type'] = 'application/soap+xml; charset=utf-8'; - xmlnsSoap = 'xmlns:' + envelopeKey + '="http://www.w3.org/2003/05/soap-envelope"'; - } - if (this.SOAPAction) { soapAction = this.SOAPAction; } else if (method.soapAction !== undefined && method.soapAction !== null) { @@ -364,7 +360,13 @@ export class Client extends EventEmitter { soapAction = ((ns.lastIndexOf('/') !== ns.length - 1) ? ns + '/' : ns) + name; } - if (!this.wsdl.options.forceSoap12Headers) { + if (this.wsdl.options.forceSoap12Headers) { + headers['Content-Type'] = 'application/soap+xml; charset=utf-8'; + if (this.wsdl.options.addHeadersAction) { + headers['Content-Type'] += '; action=' + soapAction; + } + xmlnsSoap = 'xmlns:' + envelopeKey + '="http://www.w3.org/2003/05/soap-envelope"'; + } else { headers.SOAPAction = '"' + soapAction + '"'; } diff --git a/src/types.ts b/src/types.ts index d3ed2debc..d026d7aea 100644 --- a/src/types.ts +++ b/src/types.ts @@ -94,6 +94,8 @@ export interface IWsdlBaseOptions { wsdl_options?: { [key: string]: any }; /** set proper headers for SOAP v1.2. */ forceSoap12Headers?: boolean; + /** set content type header action for SOAP v1.2 */ + addHeadersAction?: boolean; } /** @deprecated use IOptions */ diff --git a/src/wsdl/index.ts b/src/wsdl/index.ts index ff1c2ccd7..83c34ccb7 100644 --- a/src/wsdl/index.ts +++ b/src/wsdl/index.ts @@ -1142,6 +1142,7 @@ export class WSDL { // Works only in client this.options.forceSoap12Headers = options.forceSoap12Headers; + this.options.addHeadersAction = options.addHeadersAction; this.options.customDeserializer = options.customDeserializer; if (options.overrideRootElement !== undefined) { diff --git a/test/client-test.js b/test/client-test.js index 08e245670..ad349d4aa 100644 --- a/test/client-test.js +++ b/test/client-test.js @@ -430,6 +430,23 @@ var fs = require('fs'), }, baseUrl); }); + it('should add proper headers for soap12 with action in content type', function (done) { + soap.createClient(__dirname + '/wsdl/default_namespace_soap12.wsdl', _.assign({ forceSoap12Headers: true, addHeadersAction: true}, meta.options), function (err, client) { + assert.ok(client); + assert.ifError(err); + + client.MyOperation({}, function (err, result) { + assert.ok(result); + assert.ok(client.lastRequestHeaders); + assert.ok(client.lastRequest); + assert.equal(client.lastRequestHeaders['Content-Type'], 'application/soap+xml; charset=utf-8; action=MyOperation'); + assert.notEqual(client.lastRequest.indexOf('xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"'), -1); + assert(!client.lastRequestHeaders.SOAPAction); + done(); + }, null, { 'test-header': 'test' }); + }, baseUrl); + }); + it('should allow calling the method with args, callback, options and extra headers', function (done) { soap.createClient(__dirname + '/wsdl/json_response.wsdl', meta.options, function (err, client) { assert.ok(client);