Skip to content

Commit 63d859a

Browse files
author
Sefa Ilkimen
committed
- implement all HTTP operations as shorthand functions of "sendRequest"
- add some more type checking
1 parent 6b9ed72 commit 63d859a

File tree

5 files changed

+153
-285
lines changed

5 files changed

+153
-285
lines changed

test/app-test-definitions.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ const tests = [
288288
.should.be.equal('http://httpbin.org/get?myArray[]=val1&myArray[]=val2&myArray[]=val3&myString=testString');
289289
}
290290
},{
291-
description: 'should reject non-string values in local header object #54',
292-
expected: 'rejected: {"status": 0, "error": "advanced-http: header values must be strings" ...',
291+
description: 'should throw on non-string values in local header object #54',
292+
expected: 'throwed: {"message": "advanced-http: header values must be strings"}',
293293
func: function(resolve, reject) {
294294
cordova.plugin.http.get('http://httpbin.org/get', {}, { myTestHeader: 1 }, resolve, reject);
295295
},
296296
validationFunc: function(driver, result) {
297-
result.type.should.be.equal('rejected');
298-
result.data.error.should.be.equal('advanced-http: header values must be strings');
297+
result.type.should.be.equal('throwed');
298+
result.message.should.be.equal('advanced-http: header values must be strings');
299299
}
300300
},{
301301
description: 'should throw an error while setting non-string value as global header #54',

test/js-mocha-specs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ describe('Advanced HTTP www interface', function() {
2727
mock('cordova/exec', noop);
2828
mock(`${PLUGIN_ID}.cookie-handler`, {});
2929
mock(`${HELPERS_ID}.cookie-handler`, {});
30-
mock(`${PLUGIN_ID}.messages`, require('../www/messages'));
3130
mock(`${HELPERS_ID}.messages`, require('../www/messages'));
3231
mock(`${PLUGIN_ID}.angular-integration`, { registerService: noop });
3332

www/advanced-http.js

Lines changed: 111 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
1-
/*global angular*/
2-
3-
/*
4-
*
5-
* Licensed to the Apache Software Foundation (ASF) under one
6-
* or more contributor license agreements. See the NOTICE file
7-
* distributed with this work for additional information
8-
* regarding copyright ownership. The ASF licenses this file
9-
* to you under the Apache License, Version 2.0 (the
10-
* "License"); you may not use this file except in compliance
11-
* with the License. You may obtain a copy of the License at
12-
*
13-
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
15-
* Unless required by applicable law or agreed to in writing,
16-
* software distributed under the License is distributed on an
17-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18-
* KIND, either express or implied. See the License for the
19-
* specific language governing permissions and limitations
20-
* under the License.
21-
*
22-
* Modified by Andrew Stephan for Sync OnSet
23-
* Modified by Sefa Ilkimen
24-
*/
25-
261
/*
27-
* An HTTP Plugin for PhoneGap.
2+
* A native HTTP Plugin for Cordova / PhoneGap.
283
*/
294

305
var pluginId = module.id.slice(0, module.id.lastIndexOf('.'));
@@ -33,222 +8,124 @@ var exec = require('cordova/exec');
338
var angularIntegration = require(pluginId +'.angular-integration');
349
var cookieHandler = require(pluginId + '.cookie-handler');
3510
var helpers = require(pluginId + '.helpers');
36-
var messages = require(pluginId + '.messages');
3711

38-
var internals = {
12+
var globalConfigs = {
3913
headers: {},
40-
dataSerializer: 'urlencoded',
41-
timeoutInSeconds: 60.0,
14+
serializer: 'urlencoded',
15+
timeout: 60.0,
4216
};
4317

4418
var publicInterface = {
45-
getBasicAuthHeader: function (username, password) {
46-
return {'Authorization': 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password)};
47-
},
48-
useBasicAuth: function (username, password) {
49-
this.setHeader('*', 'Authorization', 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password));
50-
},
51-
getHeaders: function (host) {
52-
return internals.headers[host || '*'] || null;
53-
},
54-
setHeader: function () {
55-
// this one is for being backward compatible
56-
var host = '*';
57-
var header = arguments[0];
58-
var value = arguments[1];
59-
60-
if (arguments.length === 3) {
61-
host = arguments[0];
62-
header = arguments[1];
63-
value = arguments[2];
64-
}
65-
66-
if (header.toLowerCase() === 'cookie') {
67-
throw new Error(messages.ADDING_COOKIES_NOT_SUPPORTED);
68-
}
69-
70-
if (helpers.getTypeOf(value) !== 'String') {
71-
throw new Error(messages.HEADER_VALUE_MUST_BE_STRING);
72-
}
73-
74-
internals.headers[host] = internals.headers[host] || {};
75-
internals.headers[host][header] = value;
76-
},
77-
getDataSerializer: function () {
78-
return internals.dataSerializer;
79-
},
80-
setDataSerializer: function (serializer) {
81-
internals.dataSerializer = helpers.checkSerializer(serializer);
82-
},
83-
setCookie: function (url, cookie, options) {
84-
cookieHandler.setCookie(url, cookie, options);
85-
},
86-
clearCookies: function () {
87-
cookieHandler.clearCookies();
88-
},
89-
removeCookies: function (url, callback) {
90-
cookieHandler.removeCookies(url, callback);
91-
},
92-
getCookieString: function (url) {
93-
return cookieHandler.getCookieString(url);
94-
},
95-
getRequestTimeout: function () {
96-
return internals.timeoutInSeconds;
97-
},
98-
setRequestTimeout: function (timeout) {
99-
internals.timeoutInSeconds = timeout;
100-
},
101-
enableSSLPinning: function (enable, success, failure) {
102-
return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]);
103-
},
104-
acceptAllCerts: function (allow, success, failure) {
105-
return exec(success, failure, 'CordovaHttpPlugin', 'acceptAllCerts', [allow]);
106-
},
107-
disableRedirect: function (disable, success, failure) {
108-
return exec(success, failure, 'CordovaHttpPlugin', 'disableRedirect', [disable]);
109-
},
110-
validateDomainName: function (validate, success, failure) {
111-
failure(messages.DEPRECATED_VDN);
112-
},
113-
sendRequest: function (url, options, success, failure) {
114-
helpers.handleMissingCallbacks(success, failure);
115-
116-
options = helpers.handleMissingOptions(options, internals);
117-
118-
var headers = helpers.getMergedHeaders(url, options.headers, internals.headers);
119-
var onSuccess = helpers.injectCookieHandler(url, success);
120-
var onFail = helpers.injectCookieHandler(url, failure);
121-
var payload;
122-
123-
if ([ 'get', 'delete', 'head' ].indexOf(method) < 0) {
124-
payload = helpers.getProcessedData(options.data, options.serializer);
125-
} else {
126-
payload = params;
127-
}
128-
129-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [ url, payload, options.serializer, headers, options.timeout ]);
130-
},
131-
post: function (url, data, headers, success, failure) {
132-
helpers.handleMissingCallbacks(success, failure);
133-
134-
data = helpers.getProcessedData(data, internals.dataSerializer);
135-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
136-
137-
if (!helpers.checkHeaders(headers)) {
138-
return helpers.onInvalidHeader(failure);
139-
}
140-
141-
var onSuccess = helpers.injectCookieHandler(url, success);
142-
var onFail = helpers.injectCookieHandler(url, failure);
143-
144-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'post', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
145-
},
146-
get: function (url, params, headers, success, failure) {
147-
helpers.handleMissingCallbacks(success, failure);
148-
149-
params = params || {};
150-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
151-
152-
if (!helpers.checkHeaders(headers)) {
153-
return helpers.onInvalidHeader(failure);
154-
}
155-
156-
var onSuccess = helpers.injectCookieHandler(url, success);
157-
var onFail = helpers.injectCookieHandler(url, failure);
158-
159-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'get', [url, params, headers, internals.timeoutInSeconds]);
160-
},
161-
put: function (url, data, headers, success, failure) {
162-
helpers.handleMissingCallbacks(success, failure);
163-
164-
data = helpers.getProcessedData(data, internals.dataSerializer);
165-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
166-
167-
if (!helpers.checkHeaders(headers)) {
168-
return helpers.onInvalidHeader(failure);
169-
}
170-
171-
var onSuccess = helpers.injectCookieHandler(url, success);
172-
var onFail = helpers.injectCookieHandler(url, failure);
173-
174-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
175-
},
176-
patch: function (url, data, headers, success, failure) {
177-
helpers.handleMissingCallbacks(success, failure);
178-
179-
data = helpers.getProcessedData(data, internals.dataSerializer);
180-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
181-
182-
if (!helpers.checkHeaders(headers)) {
183-
return helpers.onInvalidHeader(failure);
184-
}
185-
186-
var onSuccess = helpers.injectCookieHandler(url, success);
187-
var onFail = helpers.injectCookieHandler(url, failure);
188-
189-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
190-
},
191-
delete: function (url, params, headers, success, failure) {
192-
helpers.handleMissingCallbacks(success, failure);
193-
194-
params = params || {};
195-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
196-
197-
if (!helpers.checkHeaders(headers)) {
198-
return helpers.onInvalidHeader(failure);
199-
}
200-
201-
var onSuccess = helpers.injectCookieHandler(url, success);
202-
var onFail = helpers.injectCookieHandler(url, failure);
203-
204-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'delete', [url, params, headers, internals.timeoutInSeconds]);
205-
},
206-
head: function (url, params, headers, success, failure) {
207-
helpers.handleMissingCallbacks(success, failure);
208-
209-
params = params || {};
210-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
211-
212-
if (!helpers.checkHeaders(headers)) {
213-
return helpers.onInvalidHeader(failure);
214-
}
215-
216-
var onSuccess = helpers.injectCookieHandler(url, success);
217-
var onFail = helpers.injectCookieHandler(url, failure);
218-
219-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'head', [url, params, headers, internals.timeoutInSeconds]);
220-
},
221-
uploadFile: function (url, params, headers, filePath, name, success, failure) {
222-
helpers.handleMissingCallbacks(success, failure);
223-
224-
params = params || {};
225-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
226-
227-
if (!helpers.checkHeaders(headers)) {
228-
return helpers.onInvalidHeader(failure);
229-
}
230-
231-
var onSuccess = helpers.injectCookieHandler(url, success);
232-
var onFail = helpers.injectCookieHandler(url, failure);
233-
234-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name, internals.timeoutInSeconds]);
235-
},
236-
downloadFile: function (url, params, headers, filePath, success, failure) {
237-
helpers.handleMissingCallbacks(success, failure);
238-
239-
params = params || {};
240-
headers = helpers.getMergedHeaders(url, headers, internals.headers);
241-
242-
if (!helpers.checkHeaders(headers)) {
243-
return helpers.onInvalidHeader(failure);
244-
}
245-
246-
var onSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success));
247-
var onFail = helpers.injectCookieHandler(url, failure);
19+
getBasicAuthHeader: function (username, password) {
20+
return {'Authorization': 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password)};
21+
},
22+
useBasicAuth: function (username, password) {
23+
this.setHeader('*', 'Authorization', 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password));
24+
},
25+
getHeaders: function (host) {
26+
return globalConfigs.headers[host || '*'] || null;
27+
},
28+
setHeader: function () {
29+
// this one is for being backward compatible
30+
var host = '*';
31+
var header = arguments[0];
32+
var value = arguments[1];
33+
34+
if (arguments.length === 3) {
35+
host = arguments[0];
36+
header = arguments[1];
37+
value = arguments[2];
38+
}
24839

249-
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath, internals.timeoutInSeconds]);
40+
helpers.checkForBlacklistedHeaderKey(header);
41+
helpers.checkForInvalidHeaderValue(value);
42+
43+
globalConfigs.headers[host] = globalConfigs.headers[host] || {};
44+
globalConfigs.headers[host][header] = value;
45+
},
46+
getDataSerializer: function () {
47+
return globalConfigs.serializer;
48+
},
49+
setDataSerializer: function (serializer) {
50+
globalConfigs.serializer = helpers.checkSerializer(serializer);
51+
},
52+
setCookie: function (url, cookie, options) {
53+
cookieHandler.setCookie(url, cookie, options);
54+
},
55+
clearCookies: function () {
56+
cookieHandler.clearCookies();
57+
},
58+
removeCookies: function (url, callback) {
59+
cookieHandler.removeCookies(url, callback);
60+
},
61+
getCookieString: function (url) {
62+
return cookieHandler.getCookieString(url);
63+
},
64+
getRequestTimeout: function () {
65+
return globalConfigs.timeout;
66+
},
67+
setRequestTimeout: function (timeout) {
68+
globalConfigs.timeout = timeout;
69+
},
70+
enableSSLPinning: function (enable, success, failure) {
71+
return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]);
72+
},
73+
acceptAllCerts: function (allow, success, failure) {
74+
return exec(success, failure, 'CordovaHttpPlugin', 'acceptAllCerts', [allow]);
75+
},
76+
disableRedirect: function (disable, success, failure) {
77+
return exec(success, failure, 'CordovaHttpPlugin', 'disableRedirect', [disable]);
78+
},
79+
sendRequest: function (url, options, success, failure) {
80+
helpers.handleMissingCallbacks(success, failure);
81+
82+
options = helpers.handleMissingOptions(options, globalConfigs);
83+
84+
var headers = helpers.getMergedHeaders(url, options.headers, globalConfigs.headers);
85+
var onSuccess = helpers.injectCookieHandler(url, success);
86+
var onFail = helpers.injectCookieHandler(url, failure);
87+
88+
switch(options.method) {
89+
case 'post':
90+
case 'put':
91+
case 'patch':
92+
var data = helpers.getProcessedData(options.data, options.serializer);
93+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [ url, data, options.serializer, headers, options.timeout ]);
94+
case 'upload':
95+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFile', [ url, options.params, headers, options.filePath, options.name, options.timeout ]);
96+
case 'download':
97+
var onDownloadSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success));
98+
return exec(onDownloadSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [ url, options.params, headers, options.filePath, options.timeout ]);
99+
default:
100+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [ url, options.params, headers, options.timeout ]);
250101
}
102+
},
103+
post: function (url, data, headers, success, failure) {
104+
return publicInterface.sendRequest(url, { method: 'post', data: data, headers: headers }, success, failure);
105+
},
106+
get: function (url, params, headers, success, failure) {
107+
return publicInterface.sendRequest(url, { method: 'get', params: params, headers: headers }, success, failure);
108+
},
109+
put: function (url, data, headers, success, failure) {
110+
return publicInterface.sendRequest(url, { method: 'put', data: data, headers: headers }, success, failure);
111+
},
112+
patch: function (url, data, headers, success, failure) {
113+
return publicInterface.sendRequest(url, { method: 'patch', data: data, headers: headers }, success, failure);
114+
},
115+
delete: function (url, params, headers, success, failure) {
116+
return publicInterface.sendRequest(url, { method: 'delete', params: params, headers: headers }, success, failure);
117+
},
118+
head: function (url, params, headers, success, failure) {
119+
return publicInterface.sendRequest(url, { method: 'head', params: params, headers: headers }, success, failure);
120+
},
121+
uploadFile: function (url, params, headers, filePath, name, success, failure) {
122+
return publicInterface.sendRequest(url, { method: 'upload', params: params, headers: headers, filePath: filePath, name: name }, success, failure);
123+
},
124+
downloadFile: function (url, params, headers, filePath, success, failure) {
125+
return publicInterface.sendRequest(url, { method: 'download', params: params, headers: headers, filePath: filePath }, success, failure);
126+
}
251127
};
252128

129+
// angular service is deprecated and will be removed anytime soon
253130
angularIntegration.registerService(publicInterface);
254131
module.exports = publicInterface;

0 commit comments

Comments
 (0)