Skip to content

Commit

Permalink
added possibility to add action to content-type header (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonanzakrak authored and jsdevel committed Jun 5, 2019
1 parent 7c832ed commit 80f4157
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 + '"';
}

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions src/wsdl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 80f4157

Please sign in to comment.