diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e238d2..d74ee29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 3.0.0 +- Feature #158: support removing headers which were previously set via "setHeader" + - :warning: **Breaking Change**: Dropped support for Android < 5.1 - :warning: **Breaking Change**: Removed "disableRedirect", use "setFollowRedirect" instead - :warning: **Breaking Change**: Removed "setSSLCertMode", use "setServerTrustMode" instead diff --git a/test/js-specs.js b/test/js-specs.js index b949be7..1cd139c 100644 --- a/test/js-specs.js +++ b/test/js-specs.js @@ -49,6 +49,13 @@ describe('Advanced HTTP public interface', function () { http.getHeaders('*').myKey.should.equal('myValue'); }); + it('clears global headers correctly when value is undefined', () => { + http.setHeader('*', 'myKey', 'myValue'); + http.setHeader('*', 'myKey', null); + should.equal(undefined, http.getHeaders('*').myKey); + Object.keys(http.getHeaders('*')).length.should.be.equal(0); + }); + it('sets host headers correctly #24', () => { http.setHeader('www.google.de', 'myKey', 'myValue'); http.getHeaders('www.google.de').myKey.should.equal('myValue'); diff --git a/www/helpers.js b/www/helpers.js index a578211..805fcf7 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -187,7 +187,9 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, } function checkForInvalidHeaderValue(value) { - if (jsUtil.getTypeOf(value) !== 'String') { + var type = jsUtil.getTypeOf(value); + + if (type !== 'String' && type !== 'Null') { throw new Error(messages.INVALID_HEADER_VALUE); } diff --git a/www/messages.js b/www/messages.js index 3a4f6b8..9fefa06 100644 --- a/www/messages.js +++ b/www/messages.js @@ -11,7 +11,7 @@ module.exports = { INVALID_DATA_SERIALIZER: 'advanced-http: invalid serializer, supported serializers are:', INVALID_DOWNLOAD_FILE_PATH: 'advanced-http: invalid "filePath" value, needs to be a string, ', INVALID_FOLLOW_REDIRECT_VALUE: 'advanced-http: invalid follow redirect value, needs to be a boolean value, ', - INVALID_HEADER_VALUE: 'advanced-http: invalid header value, needs to be a string, ', + INVALID_HEADER_VALUE: 'advanced-http: invalid header value, needs to be a string or null, ', INVALID_HTTP_METHOD: 'advanced-http: invalid HTTP method, supported methods are:', INVALID_RESPONSE_TYPE: 'advanced-http: invalid response type, supported types are:', INVALID_SSL_CERT_MODE: 'advanced-http: invalid SSL cert mode, supported modes are:', diff --git a/www/public-interface.js b/www/public-interface.js index eb0c86b..73f75a9 100644 --- a/www/public-interface.js +++ b/www/public-interface.js @@ -58,7 +58,12 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf helpers.checkForInvalidHeaderValue(value); globalConfigs.headers[host] = globalConfigs.headers[host] || {}; - globalConfigs.headers[host][header] = value; + + if (value === null) { + delete globalConfigs.headers[host][header]; + } else { + globalConfigs.headers[host][header] = value; + } } function getDataSerializer() {