Skip to content

Commit 699b984

Browse files
authored
Clear http client header (#1103)
* fix: clear client http headers * test on clear httpHeaders
1 parent cd8a852 commit 699b984

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class Client extends EventEmitter {
120120
}
121121

122122
public clearHttpHeaders(): void {
123-
this.httpHeaders = {};
123+
this.httpHeaders = null;
124124
}
125125

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

371371
// Add extra headers
372-
for (const header in this.httpHeaders ) { headers[header] = this.httpHeaders[header]; }
373-
for (const attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
372+
if (this.httpHeaders === null) {
373+
headers = {};
374+
} else {
375+
for (const header in this.httpHeaders) { headers[header] = this.httpHeaders[header]; }
376+
for (const attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
377+
}
374378

375379
// Allow the security object to add headers
376380
if (this.security && this.security.addHeaders) {

test/client-test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,26 @@ var fs = require('fs'),
455455
}, baseUrl);
456456
});
457457

458+
it ('should remove add httpHeaders after the call', function (done) {
459+
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function (err, client) {
460+
assert.ok(client);
461+
assert.ifError(err);
462+
463+
client.addHttpHeader('foo', 'bar');
464+
assert.equal(client.getHttpHeaders().foo, 'bar');
465+
466+
client.clearHttpHeaders();
467+
assert.equal(client.getHttpHeaders(), null);
468+
469+
client.MyOperation({}, function (err, result) {
470+
assert.ok(result);
471+
assert.equal(client.lastRequestHeaders.foo, undefined);
472+
473+
done();
474+
});
475+
}, baseUrl);
476+
});
477+
458478
it('should have rawRequest available in the callback', function (done) {
459479
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function (err, client) {
460480
assert.ok(client);
@@ -674,7 +694,7 @@ var fs = require('fs'),
674694
assert.equal(client.getHttpHeaders().foo, 'bar');
675695

676696
client.clearHttpHeaders();
677-
assert.equal(Object.keys(client.getHttpHeaders()).length, 0);
697+
assert.equal(client.getHttpHeaders(), null);
678698
done();
679699
});
680700
});
@@ -1453,7 +1473,7 @@ var fs = require('fs'),
14531473
assert.equal(client.getHttpHeaders().foo, 'bar');
14541474

14551475
client.clearHttpHeaders();
1456-
assert.equal(Object.keys(client.getHttpHeaders()).length, 0);
1476+
assert.equal(client.getHttpHeaders(), null);
14571477
done();
14581478
});
14591479
});

0 commit comments

Comments
 (0)