mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-01-31 00:00:03 +08:00
chore: add JS linter
This commit is contained in:
6
.eslintignore
Normal file
6
.eslintignore
Normal file
@@ -0,0 +1,6 @@
|
||||
node_modules
|
||||
temp
|
||||
platforms
|
||||
**/*.min.js
|
||||
www/umd-tough-cookie.js
|
||||
www/lodash.js
|
||||
25
.eslintrc.json
Normal file
25
.eslintrc.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"env": {
|
||||
"browser": true,
|
||||
"node": true,
|
||||
"es2020": true
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended"
|
||||
],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2020,
|
||||
"sourceType": "script"
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"files": [
|
||||
"test/**/*.js",
|
||||
"www/**/*.js"
|
||||
],
|
||||
"env": {
|
||||
"mocha": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
15
.github/workflows/ci.yml
vendored
15
.github/workflows/ci.yml
vendored
@@ -17,6 +17,21 @@ env:
|
||||
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Install node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ env.node_version }}
|
||||
cache: npm
|
||||
cache-dependency-path: package-lock.json
|
||||
- name: Install node modules
|
||||
run: npm ci
|
||||
- name: Run ESLint
|
||||
run: npm run lint
|
||||
|
||||
test-www-interface:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
1125
package-lock.json
generated
1125
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@
|
||||
"build:browser": "./scripts/build-test-app.sh --browser",
|
||||
"build:android": "./scripts/build-test-app.sh --android --emulator",
|
||||
"build:ios": "./scripts/build-test-app.sh --ios --emulator",
|
||||
"lint": "eslint . --ext .js",
|
||||
"test:android": "npm run update:cert && npm run build:android && ./scripts/test-app.sh --android --emulator",
|
||||
"test:ios": "npm run update:cert && npm run build:ios && ./scripts/test-app.sh --ios --emulator",
|
||||
"test:app": "npm run test:android && npm run test:ios",
|
||||
@@ -61,6 +62,7 @@
|
||||
"chai": "4.3.6",
|
||||
"colors": "1.4.0",
|
||||
"cordova": "11.0.0",
|
||||
"eslint": "^8.57.0",
|
||||
"mocha": "9.2.2",
|
||||
"umd-tough-cookie": "3.0.0",
|
||||
"wd": "1.14.0",
|
||||
|
||||
27
src/browser/cordova-http-plugin.js
vendored
27
src/browser/cordova-http-plugin.js
vendored
@@ -193,7 +193,7 @@ function sendRequest(method, withData, opts, success, failure) {
|
||||
}
|
||||
|
||||
switch (serializer) {
|
||||
case 'json':
|
||||
case 'json': {
|
||||
setDefaultContentType(headers, 'application/json; charset=utf8');
|
||||
processedData = serializeJsonData(data);
|
||||
|
||||
@@ -202,18 +202,21 @@ function sendRequest(method, withData, opts, success, failure) {
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'utf8':
|
||||
case 'utf8': {
|
||||
setDefaultContentType(headers, 'text/plain; charset=utf8');
|
||||
processedData = data.text;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'urlencoded':
|
||||
case 'urlencoded': {
|
||||
setDefaultContentType(headers, 'application/x-www-form-urlencoded');
|
||||
processedData = serializeParams(data);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'multipart':
|
||||
case 'multipart': {
|
||||
const contentType = getHeaderValue(headers, 'Content-Type');
|
||||
|
||||
// intentionally don't set a default content type
|
||||
@@ -224,11 +227,13 @@ function sendRequest(method, withData, opts, success, failure) {
|
||||
|
||||
processedData = processMultipartData(data);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'raw':
|
||||
case 'raw': {
|
||||
setDefaultContentType(headers, 'application/octet-stream');
|
||||
processedData = data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// requesting text instead of JSON because it's parsed in the response handler
|
||||
@@ -283,7 +288,7 @@ function sendRequest(method, withData, opts, success, failure) {
|
||||
xhr.send(processedData);
|
||||
}
|
||||
|
||||
function abort(opts, success, failure) {
|
||||
function abort(opts, success) {
|
||||
var reqId = opts[0];
|
||||
var result = false;
|
||||
|
||||
@@ -318,19 +323,19 @@ var browserInterface = {
|
||||
abort: function (success, failure, opts) {
|
||||
return abort(opts, success, failure);
|
||||
},
|
||||
uploadFile: function (success, failure, opts) {
|
||||
uploadFile: function (success, failure) {
|
||||
return failure('advanced-http: function "uploadFile" not supported on browser platform');
|
||||
},
|
||||
uploadFiles: function (success, failure, opts) {
|
||||
uploadFiles: function (success, failure) {
|
||||
return failure('advanced-http: function "uploadFiles" not supported on browser platform');
|
||||
},
|
||||
downloadFile: function (success, failure, opts) {
|
||||
downloadFile: function (success, failure) {
|
||||
return failure('advanced-http: function "downloadFile" not supported on browser platform');
|
||||
},
|
||||
setServerTrustMode: function (success, failure, opts) {
|
||||
setServerTrustMode: function (success, failure) {
|
||||
return failure('advanced-http: function "setServerTrustMode" not supported on browser platform');
|
||||
},
|
||||
setClientAuthMode: function (success, failure, opts) {
|
||||
setClientAuthMode: function (success, failure) {
|
||||
return failure('advanced-http: function "setClientAuthMode" not supported on browser platform');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* global hooks, tests */
|
||||
|
||||
const app = {
|
||||
testIndex: -1,
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* global cordova, resolveLocalFileSystemURL, should */
|
||||
|
||||
const hooks = {
|
||||
onBeforeEachTest: function (resolve, reject) {
|
||||
cordova.plugin.http.clearCookies();
|
||||
@@ -92,12 +94,12 @@ const helpers = {
|
||||
// abort is not working reliably; will be documented in known issues
|
||||
return false;
|
||||
|
||||
if (window.cordova && window.cordova.platformId === 'android') {
|
||||
var version = device.version; // NOTE will throw error if cordova is present without cordova-plugin-device
|
||||
var major = parseInt(/^(\d+)(\.|$)/.exec(version)[1], 10);
|
||||
return isFinite(major) && major >= 6;
|
||||
}
|
||||
return true;
|
||||
// if (window.cordova && window.cordova.platformId === 'android') {
|
||||
// var version = device.version; // NOTE will throw error if cordova is present without cordova-plugin-device
|
||||
// var major = parseInt(/^(\d+)(\.|$)/.exec(version)[1], 10);
|
||||
// return isFinite(major) && major >= 6;
|
||||
// }
|
||||
// return true;
|
||||
},
|
||||
getAbortDelay: function () { return 0; },
|
||||
getDemoArrayBuffer: function(size) {
|
||||
@@ -229,7 +231,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send JSON object correctly (POST)',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}" ...',
|
||||
before: helpers.setJsonSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -239,7 +241,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send JSON object correctly (PUT)',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}" ...',
|
||||
before: helpers.setJsonSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -249,7 +251,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send JSON object correctly (PATCH)',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}" ...',
|
||||
before: helpers.setJsonSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -259,7 +261,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send JSON array correctly (POST) #26',
|
||||
expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]" ...',
|
||||
before: helpers.setJsonSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', [1, 2, 3], {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -269,7 +271,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send JSON array correctly (PUT) #26',
|
||||
expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]" ...',
|
||||
before: helpers.setJsonSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', [1, 2, 3], {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -279,7 +281,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send JSON array correctly (PATCH) #26',
|
||||
expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]" ...',
|
||||
before: helpers.setJsonSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', [1, 2, 3], {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -290,7 +292,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send url encoded data correctly (POST) #41',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}" ...',
|
||||
before: helpers.setUrlEncodedSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -300,7 +302,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send url encoded data correctly (PUT)',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}" ...',
|
||||
before: helpers.setUrlEncodedSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -310,7 +312,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should send url encoded data correctly (PATCH)',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}" ...',
|
||||
before: helpers.setUrlEncodedSerializer,
|
||||
func: function (resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
@@ -330,7 +332,10 @@ const tests = [
|
||||
{
|
||||
description: 'should not follow 302 redirect when following redirects is disabled',
|
||||
expected: 'rejected: {"status": 302, ...',
|
||||
before: function (resolve, reject) { cordova.plugin.http.setFollowRedirect(false); resolve(); },
|
||||
before: function (resolve) {
|
||||
cordova.plugin.http.setFollowRedirect(false);
|
||||
resolve();
|
||||
},
|
||||
func: function (resolve, reject) { cordova.plugin.http.get('http://httpbingo.org/redirect-to?url=http://httpbingo.org/anything', {}, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('rejected');
|
||||
@@ -427,7 +432,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should encode HTTP array params correctly (GET) #45',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"url\\":\\"http://httpbin.org/get?myArray[]=val1&myArray[]=val2&myArray[]=val3\\"}\" ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"url\\":\\"http://httpbin.org/get?myArray[]=val1&myArray[]=val2&myArray[]=val3\\"}" ...',
|
||||
func: function (resolve, reject) {
|
||||
cordova.plugin.http.get('http://httpbin.org/get', { myArray: ['val1', 'val2', 'val3'], myString: 'testString' }, {}, resolve, reject);
|
||||
},
|
||||
@@ -455,8 +460,9 @@ const tests = [
|
||||
{
|
||||
description: 'should throw an error while setting non-string value as global header #54',
|
||||
expected: 'throwed: "advanced-http: header values must be strings"',
|
||||
func: function (resolve, reject) {
|
||||
func: function (resolve) {
|
||||
cordova.plugin.http.setHeader('myTestHeader', 2);
|
||||
resolve();
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('throwed');
|
||||
@@ -495,7 +501,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should not send programmatically set cookies after running "clearCookies" (GET) #59',
|
||||
expected: 'resolved: {"status": 200, "data": "{\"headers\": {\"Cookie\": \"\"...',
|
||||
expected: 'resolved: {"status": 200, "data": "{"headers": {"Cookie": ""...',
|
||||
func: function (resolve, reject) {
|
||||
cordova.plugin.http.setCookie('http://httpbin.org/get', 'myCookie=myValue');
|
||||
cordova.plugin.http.setCookie('http://httpbin.org/get', 'mySecondCookie=mySecondValue');
|
||||
@@ -1011,7 +1017,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should not send any cookies after running "clearCookies" (GET) #248',
|
||||
expected: 'resolved: {"status": 200, "data": "{\"cookies\":{}} ...',
|
||||
expected: 'resolved: {"status": 200, "data": "{"cookies":{}} ...',
|
||||
before: helpers.disableFollowingRedirect,
|
||||
func: function (resolve, reject) {
|
||||
cordova.plugin.http.get('https://httpbin.org/cookies/set?myCookieKey=myCookieValue', {}, {}, function () {
|
||||
|
||||
@@ -86,7 +86,7 @@ function getCaps(environment, os, runtime) {
|
||||
caps.name = `cordova-plugin-advanced-http (${os})`;
|
||||
|
||||
return caps;
|
||||
};
|
||||
}
|
||||
|
||||
function capitalize(text) {
|
||||
return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
|
||||
|
||||
@@ -98,7 +98,7 @@ async function validateTestIndex(driver, testIndex) {
|
||||
|
||||
async function validateTestTitle(driver, testTitle) {
|
||||
const description = await driver.elementById('descriptionLbl').text();
|
||||
const title = description.match(/\d+:\ (.*)/)[1];
|
||||
const title = description.match(/\d+: (.*)/)[1];
|
||||
|
||||
title.should.be.equal(testTitle, 'Test description is not matching!');
|
||||
}
|
||||
@@ -106,6 +106,7 @@ async function validateTestTitle(driver, testTitle) {
|
||||
async function waitToBeFinished(driver, timeout) {
|
||||
const timeoutTimestamp = Date.now() + timeout;
|
||||
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
while (true) {
|
||||
if (await driver.elementById('statusInput').getValue() === 'finished') {
|
||||
return true;
|
||||
|
||||
@@ -3,7 +3,7 @@ const BlobMock = require('./Blob.mock');
|
||||
module.exports = class FileMock extends BlobMock {
|
||||
constructor(blob, fileName) {
|
||||
super(blob, { type: blob.type });
|
||||
this._fileName = fileName !== undefined ? fileName : 'blob';
|
||||
this._fileName = fileName !== undefined ? fileName : 'blob';
|
||||
this.__lastModifiedDate = new Date();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* global FileSystem */
|
||||
|
||||
module.exports = function init(global, jsUtil, cookieHandler, messages, base64, errorCodes, dependencyValidator, ponyfills) {
|
||||
var validSerializers = ['urlencoded', 'json', 'utf8', 'raw', 'multipart'];
|
||||
var validCertModes = ['default', 'nocheck', 'pinned', 'legacy'];
|
||||
@@ -66,7 +68,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
for (var i = 0; i < globalKeys.length; i++) {
|
||||
key = globalKeys[i];
|
||||
|
||||
if (!localHeaders.hasOwnProperty(key)) {
|
||||
if (!Object.prototype.hasOwnProperty.call(localHeaders, key)) {
|
||||
localHeaders[key] = globalHeaders[key];
|
||||
}
|
||||
}
|
||||
@@ -361,7 +363,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
}
|
||||
|
||||
function getMatchingHostHeaders(url, headersList) {
|
||||
var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
|
||||
var matches = url.match(/^https?:\/\/([^/?#]+)(?:[/?#]|$)/i);
|
||||
var domain = matches && matches[1];
|
||||
|
||||
return headersList[domain] || null;
|
||||
|
||||
@@ -164,13 +164,15 @@ module.exports = function init(ToughCookie, _) {
|
||||
WebStorageCookieStore.prototype._readStore = function () {
|
||||
var json = this._storage.getItem(this._storeKey);
|
||||
|
||||
if (json !== null) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch (e) { }
|
||||
if (json === null) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {};
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
WebStorageCookieStore.prototype._writeStore = function (store) {
|
||||
|
||||
@@ -197,7 +197,7 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
|
||||
function post(url, data, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'post', data: data, headers: headers }, success, failure);
|
||||
};
|
||||
}
|
||||
|
||||
function put(url, data, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'put', data: data, headers: headers }, success, failure);
|
||||
@@ -209,7 +209,7 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
|
||||
function get(url, params, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'get', params: params, headers: headers }, success, failure);
|
||||
};
|
||||
}
|
||||
|
||||
function del(url, params, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'delete', params: params, headers: headers }, success, failure);
|
||||
@@ -221,7 +221,7 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
|
||||
function options(url, params, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'options', params: params, headers: headers }, success, failure);
|
||||
};
|
||||
}
|
||||
|
||||
function uploadFile(url, params, headers, filePath, name, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'upload', params: params, headers: headers, filePath: filePath, name: name }, success, failure);
|
||||
|
||||
@@ -6,7 +6,7 @@ module.exports = function init(jsUtil) {
|
||||
}
|
||||
|
||||
function parseUrl(url) {
|
||||
var match = url.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
|
||||
var match = url.match(/^(https?:)\/\/(([^:/?#]*)(?::([0-9]+))?)([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
|
||||
|
||||
return match && {
|
||||
protocol: match[1],
|
||||
@@ -42,7 +42,7 @@ module.exports = function init(jsUtil) {
|
||||
var parts = [];
|
||||
|
||||
for (var key in object) {
|
||||
if (!object.hasOwnProperty(key)) {
|
||||
if (!Object.prototype.hasOwnProperty.call(object, key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user