make internals private (not directly accessible via public interface)

This commit is contained in:
Sefa Ilkimen 2018-02-09 10:01:48 +01:00
parent 5186b375a4
commit a9bdca2d6d
2 changed files with 45 additions and 34 deletions

View File

@ -36,17 +36,17 @@ describe('Advanced HTTP www interface', function() {
it('sets global headers correctly with two args (old interface)', () => { it('sets global headers correctly with two args (old interface)', () => {
http.setHeader('myKey', 'myValue'); http.setHeader('myKey', 'myValue');
http.headers['*'].myKey.should.equal('myValue'); http.getHeaders('*').myKey.should.equal('myValue');
}); });
it('sets global headers correctly with three args (new interface) #24', () => { it('sets global headers correctly with three args (new interface) #24', () => {
http.setHeader('*', 'myKey', 'myValue'); http.setHeader('*', 'myKey', 'myValue');
http.headers['*'].myKey.should.equal('myValue'); http.getHeaders('*').myKey.should.equal('myValue');
}); });
it('sets host headers correctly #24', () => { it('sets host headers correctly #24', () => {
http.setHeader('www.google.de', 'myKey', 'myValue'); http.setHeader('www.google.de', 'myKey', 'myValue');
http.headers['www.google.de'].myKey.should.equal('myValue'); http.getHeaders('www.google.de').myKey.should.equal('myValue');
}); });
it('resolves global headers correctly #24', () => { it('resolves global headers correctly #24', () => {
@ -126,7 +126,7 @@ describe('Advanced HTTP www interface', function() {
it('sets basic authentication header correctly #36', () => { it('sets basic authentication header correctly #36', () => {
http.useBasicAuth('name', 'pass'); http.useBasicAuth('name', 'pass');
http.headers['*'].Authorization.should.equal('Basic bmFtZTpwYXNz'); http.getHeaders('*').Authorization.should.equal('Basic bmFtZTpwYXNz');
}); });
it('throws an Error when you try to add a cookie by using "setHeader" #46', () => { it('throws an Error when you try to add a cookie by using "setHeader" #46', () => {

View File

@ -35,17 +35,22 @@ var cookieHandler = require(pluginId + '.cookie-handler');
var helpers = require(pluginId + '.helpers'); var helpers = require(pluginId + '.helpers');
var messages = require(pluginId + '.messages'); var messages = require(pluginId + '.messages');
var http = { var internals = {
headers: {}, headers: {},
dataSerializer: 'urlencoded', dataSerializer: 'urlencoded',
sslPinning: false, timeoutInSeconds: 60.0,
timeoutInSeconds: 60.0, };
var publicInterface = {
getBasicAuthHeader: function (username, password) { getBasicAuthHeader: function (username, password) {
return {'Authorization': 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password)}; return {'Authorization': 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password)};
}, },
useBasicAuth: function (username, password) { useBasicAuth: function (username, password) {
this.setHeader('*', 'Authorization', 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password)); this.setHeader('*', 'Authorization', 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password));
}, },
getHeaders: function (host) {
return internals.headers[host || '*'] || null;
},
setHeader: function () { setHeader: function () {
// this one is for being backward compatible // this one is for being backward compatible
var host = '*'; var host = '*';
@ -66,11 +71,14 @@ var http = {
throw new Error(messages.HEADER_VALUE_MUST_BE_STRING); throw new Error(messages.HEADER_VALUE_MUST_BE_STRING);
} }
this.headers[host] = this.headers[host] || {}; internals.headers[host] = internals.headers[host] || {};
this.headers[host][header] = value; internals.headers[host][header] = value;
},
getDataSerializer: function () {
return internals.dataSerializer;
}, },
setDataSerializer: function (serializer) { setDataSerializer: function (serializer) {
this.dataSerializer = helpers.checkSerializer(serializer); internals.dataSerializer = helpers.checkSerializer(serializer);
}, },
setCookie: function (url, cookie, options) { setCookie: function (url, cookie, options) {
cookieHandler.setCookie(url, cookie, options); cookieHandler.setCookie(url, cookie, options);
@ -84,8 +92,11 @@ var http = {
getCookieString: function (url) { getCookieString: function (url) {
return cookieHandler.getCookieString(url); return cookieHandler.getCookieString(url);
}, },
getRequestTimeout: function () {
return internals.timeoutInSeconds;
},
setRequestTimeout: function (timeout) { setRequestTimeout: function (timeout) {
this.timeoutInSeconds = timeout; internals.timeoutInSeconds = timeout;
}, },
enableSSLPinning: function (enable, success, failure) { enableSSLPinning: function (enable, success, failure) {
return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]); return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]);
@ -102,8 +113,8 @@ var http = {
post: function (url, data, headers, success, failure) { post: function (url, data, headers, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
data = helpers.getProcessedData(data, this.dataSerializer); data = helpers.getProcessedData(data, internals.dataSerializer);
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -112,13 +123,13 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, success); var onSuccess = helpers.injectCookieHandler(url, success);
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'post', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'post', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
}, },
get: function (url, params, headers, success, failure) { get: function (url, params, headers, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
params = params || {}; params = params || {};
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -127,13 +138,13 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, success); var onSuccess = helpers.injectCookieHandler(url, success);
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'get', [url, params, headers, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'get', [url, params, headers, internals.timeoutInSeconds]);
}, },
put: function (url, data, headers, success, failure) { put: function (url, data, headers, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
data = helpers.getProcessedData(data, this.dataSerializer); data = helpers.getProcessedData(data, internals.dataSerializer);
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -142,14 +153,14 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, success); var onSuccess = helpers.injectCookieHandler(url, success);
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
}, },
patch: function (url, data, headers, success, failure) { patch: function (url, data, headers, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
data = helpers.getProcessedData(data, this.dataSerializer); data = helpers.getProcessedData(data, internals.dataSerializer);
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -158,14 +169,14 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, success); var onSuccess = helpers.injectCookieHandler(url, success);
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
}, },
delete: function (url, params, headers, success, failure) { delete: function (url, params, headers, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
params = params || {}; params = params || {};
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -174,13 +185,13 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, success); var onSuccess = helpers.injectCookieHandler(url, success);
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'delete', [url, params, headers, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'delete', [url, params, headers, internals.timeoutInSeconds]);
}, },
head: function (url, params, headers, success, failure) { head: function (url, params, headers, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
params = params || {}; params = params || {};
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -189,13 +200,13 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, success); var onSuccess = helpers.injectCookieHandler(url, success);
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'head', [url, params, headers, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'head', [url, params, headers, internals.timeoutInSeconds]);
}, },
uploadFile: function (url, params, headers, filePath, name, success, failure) { uploadFile: function (url, params, headers, filePath, name, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
params = params || {}; params = params || {};
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -204,13 +215,13 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, success); var onSuccess = helpers.injectCookieHandler(url, success);
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name, internals.timeoutInSeconds]);
}, },
downloadFile: function (url, params, headers, filePath, success, failure) { downloadFile: function (url, params, headers, filePath, success, failure) {
helpers.handleMissingCallbacks(success, failure); helpers.handleMissingCallbacks(success, failure);
params = params || {}; params = params || {};
headers = helpers.getMergedHeaders(url, headers, this.headers); headers = helpers.getMergedHeaders(url, headers, internals.headers);
if (!helpers.checkHeaders(headers)) { if (!helpers.checkHeaders(headers)) {
return helpers.onInvalidHeader(failure); return helpers.onInvalidHeader(failure);
@ -219,9 +230,9 @@ var http = {
var onSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success)); var onSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success));
var onFail = helpers.injectCookieHandler(url, failure); var onFail = helpers.injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath, this.timeoutInSeconds]); return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath, internals.timeoutInSeconds]);
} }
}; };
angularIntegration.registerService(http); angularIntegration.registerService(publicInterface);
module.exports = http; module.exports = publicInterface;