Skip to content

Commit

Permalink
Clear http client header (#1103)
Browse files Browse the repository at this point in the history
* fix: clear client http headers

* test on clear httpHeaders
  • Loading branch information
KurtzL authored Feb 25, 2020
1 parent cd8a852 commit 699b984
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class Client extends EventEmitter {
}

public clearHttpHeaders(): void {
this.httpHeaders = {};
this.httpHeaders = null;
}

public addBodyAttribute(bodyAttribute: any, name?: string, namespace?: string, xmlns?: string): void {
Expand Down Expand Up @@ -278,7 +278,7 @@ export class Client extends EventEmitter {
let req: Request;
let soapAction: string;
const alias = findPrefix(defs.xmlns, ns);
const headers: any = {
let headers: any = {
'Content-Type': 'text/xml; charset=utf-8',
};
let xmlnsSoap = 'xmlns:' + envelopeKey + '="http://schemas.xmlsoap.org/soap/envelope/"';
Expand Down Expand Up @@ -369,8 +369,12 @@ export class Client extends EventEmitter {
options = options || {};

// Add extra headers
for (const header in this.httpHeaders ) { headers[header] = this.httpHeaders[header]; }
for (const attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
if (this.httpHeaders === null) {
headers = {};
} else {
for (const header in this.httpHeaders) { headers[header] = this.httpHeaders[header]; }
for (const attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
}

// Allow the security object to add headers
if (this.security && this.security.addHeaders) {
Expand Down
24 changes: 22 additions & 2 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,26 @@ var fs = require('fs'),
}, baseUrl);
});

it ('should remove add httpHeaders after the call', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function (err, client) {
assert.ok(client);
assert.ifError(err);

client.addHttpHeader('foo', 'bar');
assert.equal(client.getHttpHeaders().foo, 'bar');

client.clearHttpHeaders();
assert.equal(client.getHttpHeaders(), null);

client.MyOperation({}, function (err, result) {
assert.ok(result);
assert.equal(client.lastRequestHeaders.foo, undefined);

done();
});
}, baseUrl);
});

it('should have rawRequest available in the callback', function (done) {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function (err, client) {
assert.ok(client);
Expand Down Expand Up @@ -674,7 +694,7 @@ var fs = require('fs'),
assert.equal(client.getHttpHeaders().foo, 'bar');

client.clearHttpHeaders();
assert.equal(Object.keys(client.getHttpHeaders()).length, 0);
assert.equal(client.getHttpHeaders(), null);
done();
});
});
Expand Down Expand Up @@ -1453,7 +1473,7 @@ var fs = require('fs'),
assert.equal(client.getHttpHeaders().foo, 'bar');

client.clearHttpHeaders();
assert.equal(Object.keys(client.getHttpHeaders()).length, 0);
assert.equal(client.getHttpHeaders(), null);
done();
});
});
Expand Down

0 comments on commit 699b984

Please sign in to comment.