Skip to content

Commit a9bdca2

Browse files
author
Sefa Ilkimen
committed
make internals private (not directly accessible via public interface)
1 parent 5186b37 commit a9bdca2

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

test/js-mocha-specs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ describe('Advanced HTTP www interface', function() {
3636

3737
it('sets global headers correctly with two args (old interface)', () => {
3838
http.setHeader('myKey', 'myValue');
39-
http.headers['*'].myKey.should.equal('myValue');
39+
http.getHeaders('*').myKey.should.equal('myValue');
4040
});
4141

4242
it('sets global headers correctly with three args (new interface) #24', () => {
4343
http.setHeader('*', 'myKey', 'myValue');
44-
http.headers['*'].myKey.should.equal('myValue');
44+
http.getHeaders('*').myKey.should.equal('myValue');
4545
});
4646

4747
it('sets host headers correctly #24', () => {
4848
http.setHeader('www.google.de', 'myKey', 'myValue');
49-
http.headers['www.google.de'].myKey.should.equal('myValue');
49+
http.getHeaders('www.google.de').myKey.should.equal('myValue');
5050
});
5151

5252
it('resolves global headers correctly #24', () => {
@@ -126,7 +126,7 @@ describe('Advanced HTTP www interface', function() {
126126

127127
it('sets basic authentication header correctly #36', () => {
128128
http.useBasicAuth('name', 'pass');
129-
http.headers['*'].Authorization.should.equal('Basic bmFtZTpwYXNz');
129+
http.getHeaders('*').Authorization.should.equal('Basic bmFtZTpwYXNz');
130130
});
131131

132132
it('throws an Error when you try to add a cookie by using "setHeader" #46', () => {

www/advanced-http.js

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,22 @@ var cookieHandler = require(pluginId + '.cookie-handler');
3535
var helpers = require(pluginId + '.helpers');
3636
var messages = require(pluginId + '.messages');
3737

38-
var http = {
39-
headers: {},
40-
dataSerializer: 'urlencoded',
41-
sslPinning: false,
42-
timeoutInSeconds: 60.0,
38+
var internals = {
39+
headers: {},
40+
dataSerializer: 'urlencoded',
41+
timeoutInSeconds: 60.0,
42+
};
43+
44+
var publicInterface = {
4345
getBasicAuthHeader: function (username, password) {
4446
return {'Authorization': 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password)};
4547
},
4648
useBasicAuth: function (username, password) {
4749
this.setHeader('*', 'Authorization', 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password));
4850
},
51+
getHeaders: function (host) {
52+
return internals.headers[host || '*'] || null;
53+
},
4954
setHeader: function () {
5055
// this one is for being backward compatible
5156
var host = '*';
@@ -66,11 +71,14 @@ var http = {
6671
throw new Error(messages.HEADER_VALUE_MUST_BE_STRING);
6772
}
6873

69-
this.headers[host] = this.headers[host] || {};
70-
this.headers[host][header] = value;
74+
internals.headers[host] = internals.headers[host] || {};
75+
internals.headers[host][header] = value;
76+
},
77+
getDataSerializer: function () {
78+
return internals.dataSerializer;
7179
},
7280
setDataSerializer: function (serializer) {
73-
this.dataSerializer = helpers.checkSerializer(serializer);
81+
internals.dataSerializer = helpers.checkSerializer(serializer);
7482
},
7583
setCookie: function (url, cookie, options) {
7684
cookieHandler.setCookie(url, cookie, options);
@@ -84,8 +92,11 @@ var http = {
8492
getCookieString: function (url) {
8593
return cookieHandler.getCookieString(url);
8694
},
95+
getRequestTimeout: function () {
96+
return internals.timeoutInSeconds;
97+
},
8798
setRequestTimeout: function (timeout) {
88-
this.timeoutInSeconds = timeout;
99+
internals.timeoutInSeconds = timeout;
89100
},
90101
enableSSLPinning: function (enable, success, failure) {
91102
return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]);
@@ -102,8 +113,8 @@ var http = {
102113
post: function (url, data, headers, success, failure) {
103114
helpers.handleMissingCallbacks(success, failure);
104115

105-
data = helpers.getProcessedData(data, this.dataSerializer);
106-
headers = helpers.getMergedHeaders(url, headers, this.headers);
116+
data = helpers.getProcessedData(data, internals.dataSerializer);
117+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
107118

108119
if (!helpers.checkHeaders(headers)) {
109120
return helpers.onInvalidHeader(failure);
@@ -112,13 +123,13 @@ var http = {
112123
var onSuccess = helpers.injectCookieHandler(url, success);
113124
var onFail = helpers.injectCookieHandler(url, failure);
114125

115-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'post', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]);
126+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'post', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
116127
},
117128
get: function (url, params, headers, success, failure) {
118129
helpers.handleMissingCallbacks(success, failure);
119130

120131
params = params || {};
121-
headers = helpers.getMergedHeaders(url, headers, this.headers);
132+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
122133

123134
if (!helpers.checkHeaders(headers)) {
124135
return helpers.onInvalidHeader(failure);
@@ -127,13 +138,13 @@ var http = {
127138
var onSuccess = helpers.injectCookieHandler(url, success);
128139
var onFail = helpers.injectCookieHandler(url, failure);
129140

130-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'get', [url, params, headers, this.timeoutInSeconds]);
141+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'get', [url, params, headers, internals.timeoutInSeconds]);
131142
},
132143
put: function (url, data, headers, success, failure) {
133144
helpers.handleMissingCallbacks(success, failure);
134145

135-
data = helpers.getProcessedData(data, this.dataSerializer);
136-
headers = helpers.getMergedHeaders(url, headers, this.headers);
146+
data = helpers.getProcessedData(data, internals.dataSerializer);
147+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
137148

138149
if (!helpers.checkHeaders(headers)) {
139150
return helpers.onInvalidHeader(failure);
@@ -142,14 +153,14 @@ var http = {
142153
var onSuccess = helpers.injectCookieHandler(url, success);
143154
var onFail = helpers.injectCookieHandler(url, failure);
144155

145-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]);
156+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
146157
},
147158

148159
patch: function (url, data, headers, success, failure) {
149160
helpers.handleMissingCallbacks(success, failure);
150161

151-
data = helpers.getProcessedData(data, this.dataSerializer);
152-
headers = helpers.getMergedHeaders(url, headers, this.headers);
162+
data = helpers.getProcessedData(data, internals.dataSerializer);
163+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
153164

154165
if (!helpers.checkHeaders(headers)) {
155166
return helpers.onInvalidHeader(failure);
@@ -158,14 +169,14 @@ var http = {
158169
var onSuccess = helpers.injectCookieHandler(url, success);
159170
var onFail = helpers.injectCookieHandler(url, failure);
160171

161-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]);
172+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
162173
},
163174

164175
delete: function (url, params, headers, success, failure) {
165176
helpers.handleMissingCallbacks(success, failure);
166177

167178
params = params || {};
168-
headers = helpers.getMergedHeaders(url, headers, this.headers);
179+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
169180

170181
if (!helpers.checkHeaders(headers)) {
171182
return helpers.onInvalidHeader(failure);
@@ -174,13 +185,13 @@ var http = {
174185
var onSuccess = helpers.injectCookieHandler(url, success);
175186
var onFail = helpers.injectCookieHandler(url, failure);
176187

177-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'delete', [url, params, headers, this.timeoutInSeconds]);
188+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'delete', [url, params, headers, internals.timeoutInSeconds]);
178189
},
179190
head: function (url, params, headers, success, failure) {
180191
helpers.handleMissingCallbacks(success, failure);
181192

182193
params = params || {};
183-
headers = helpers.getMergedHeaders(url, headers, this.headers);
194+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
184195

185196
if (!helpers.checkHeaders(headers)) {
186197
return helpers.onInvalidHeader(failure);
@@ -189,13 +200,13 @@ var http = {
189200
var onSuccess = helpers.injectCookieHandler(url, success);
190201
var onFail = helpers.injectCookieHandler(url, failure);
191202

192-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'head', [url, params, headers, this.timeoutInSeconds]);
203+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'head', [url, params, headers, internals.timeoutInSeconds]);
193204
},
194205
uploadFile: function (url, params, headers, filePath, name, success, failure) {
195206
helpers.handleMissingCallbacks(success, failure);
196207

197208
params = params || {};
198-
headers = helpers.getMergedHeaders(url, headers, this.headers);
209+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
199210

200211
if (!helpers.checkHeaders(headers)) {
201212
return helpers.onInvalidHeader(failure);
@@ -204,13 +215,13 @@ var http = {
204215
var onSuccess = helpers.injectCookieHandler(url, success);
205216
var onFail = helpers.injectCookieHandler(url, failure);
206217

207-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name, this.timeoutInSeconds]);
218+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name, internals.timeoutInSeconds]);
208219
},
209220
downloadFile: function (url, params, headers, filePath, success, failure) {
210221
helpers.handleMissingCallbacks(success, failure);
211222

212223
params = params || {};
213-
headers = helpers.getMergedHeaders(url, headers, this.headers);
224+
headers = helpers.getMergedHeaders(url, headers, internals.headers);
214225

215226
if (!helpers.checkHeaders(headers)) {
216227
return helpers.onInvalidHeader(failure);
@@ -219,9 +230,9 @@ var http = {
219230
var onSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success));
220231
var onFail = helpers.injectCookieHandler(url, failure);
221232

222-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath, this.timeoutInSeconds]);
233+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath, internals.timeoutInSeconds]);
223234
}
224235
};
225236

226-
angularIntegration.registerService(http);
227-
module.exports = http;
237+
angularIntegration.registerService(publicInterface);
238+
module.exports = publicInterface;

0 commit comments

Comments
 (0)