diff --git a/lib/RESTAPIMock.js b/lib/RESTAPIMock.js index e20598b..1db48f7 100644 --- a/lib/RESTAPIMock.js +++ b/lib/RESTAPIMock.js @@ -1,7 +1,7 @@ const getJSON = require('./getJSON') const createRes = require('./createRes') const startsWith = require('./startsWith') -const removeProtocal = require('./removeProtocal') +const removeProtocol = require('./removeProtocol') function RESTAPIMock(myURL, searchParams, callbackName) { let match, response; @@ -15,10 +15,10 @@ function RESTAPIMock(myURL, searchParams, callbackName) { if (!matchConfig || matchConfig.choices === 0) { return; } - const matchURL = removeProtocal(matchConfig.URL); + const matchURL = removeProtocol(matchConfig.URL); const RESTAPIS = getJSON("./mock/RESTAPI.json"); match = RESTAPIS.some(RESTAPI => { - if (matchURL === removeProtocal(RESTAPI.url)) { + if (matchURL === removeProtocol(RESTAPI.url)) { // console.log(RESTAPI); if (matchConfig.OPTIONS) { const choice = matchConfig.choices.filter(choice => { diff --git a/lib/anyproxy.config.js b/lib/anyproxy.config.js index 51e4f35..19e94a8 100644 --- a/lib/anyproxy.config.js +++ b/lib/anyproxy.config.js @@ -3,7 +3,7 @@ const { URLSearchParams } = require('url'); const R = require('ramda'); -const removeProtocal = require('./removeProtocal') +const removeProtocol = require('./removeProtocol') const RESTAPIMock = require('./RESTAPIMock'); const fileMock = require('./fileMock'); @@ -12,10 +12,10 @@ module.exports = function () { summary: "a rule to hack response", * beforeSendRequest(requestDetail) { - const URLO = new URL(requestDetail.url); - const myURL = removeProtocal(URLO.href); - // console.log(URLO.searchParams); - const searchParams = URLO.searchParams; + const url = new URL(requestDetail.url); + const myURL = removeProtocol(url.href); + // console.log(url.searchParams); + const searchParams = url.searchParams; const callback = searchParams.get('callback') || new URLSearchParams(requestDetail.requestData.toString()).get('callback'); const chain = [RESTAPIMock, fileMock]; diff --git a/lib/fileMock.js b/lib/fileMock.js index 935d2a3..359633f 100644 --- a/lib/fileMock.js +++ b/lib/fileMock.js @@ -21,12 +21,12 @@ function objectToMap(o) { function fileMock(myURL, searchParams, callback) { let match, response; - const filemap = objectToMap(getJSON("./mock/filemap.json")); - if (filemap.get('disabled')) { + const fileMap = objectToMap(getJSON("./mock/filemap.json")); + if (fileMap.get('disabled')) { return } - filemap.get('map').forEach((local, online) => { - // console.log(online, url.startsWith(removeProtocal(online))); + fileMap.get('map').forEach((local, online) => { + // console.log(online, url.startsWith(removeProtocol(online))); if (startsWith(myURL, online)) { if (fs.existsSync(local)) { match = true; diff --git a/lib/removeProtocal.js b/lib/removeProtocal.js deleted file mode 100644 index e054adb..0000000 --- a/lib/removeProtocal.js +++ /dev/null @@ -1,5 +0,0 @@ -function removeProtocal(url) { - return url.replace(/(^\w+:|^)\/\//, ''); -} - -module.exports = removeProtocal \ No newline at end of file diff --git a/lib/removeProtocol.js b/lib/removeProtocol.js new file mode 100644 index 0000000..d49e349 --- /dev/null +++ b/lib/removeProtocol.js @@ -0,0 +1,5 @@ +function removeProtocol(url) { + return url.replace(/(^\w+:|^)\/\//, ''); +} + +module.exports = removeProtocol \ No newline at end of file diff --git a/lib/startsWith.js b/lib/startsWith.js index 322c60b..c96e27f 100644 --- a/lib/startsWith.js +++ b/lib/startsWith.js @@ -1,8 +1,8 @@ const fs = require("fs"); -const removeProtocal = require('./removeProtocal') +const removeProtocol = require('./removeProtocol') function startsWith(url, pattern) { - return removeProtocal(url).startsWith(removeProtocal(pattern)); + return removeProtocol(url).startsWith(removeProtocol(pattern)); } module.exports = startsWith \ No newline at end of file diff --git a/test/fileMock.unittest.js b/test/fileMock.unittest.js index aa67ddd..c968318 100644 --- a/test/fileMock.unittest.js +++ b/test/fileMock.unittest.js @@ -4,20 +4,20 @@ const { URLSearchParams } = require('url'); const fs = require("fs"); -const removeProtocal = require('../lib/removeProtocal') +const removeProtocol = require('../lib/removeProtocol') const fileMock = require("../lib/fileMock"); describe("fileMock", () => { it("js file", () => { const URLO = new URL('http://static.iqiyi.com/js/lib/sea1.2.js'); - const myURL = removeProtocal(URLO.href); + const myURL = removeProtocol(URLO.href); const searchParams = URLO.searchParams; const callbackName = searchParams.get('callback'); expect(fileMock(myURL, searchParams, callbackName)).toBeUndefined(); }); it("css file", () => { const URLO = new URL('http://static.qiyi.com/css/common/v3-vipCash/vip_cash1.0.css'); - const myURL = removeProtocal(URLO.href); + const myURL = removeProtocol(URLO.href); const searchParams = URLO.searchParams; const callbackName = searchParams.get('callback'); expect(fileMock(myURL, searchParams, callbackName)).toBeUndefined(); diff --git a/test/getParas.js b/test/getParas.js index 4cece2a..691521f 100644 --- a/test/getParas.js +++ b/test/getParas.js @@ -1,4 +1,4 @@ -const removeProtocal = require('../lib/removeProtocal') +const removeProtocol = require('../lib/removeProtocol') const URLS = { goldNotLogin: 'http://i.vip.iqiyi.com/client/store/pc/checkout.action?platform=b6c13e26323c537d&pid=a0226bd958843452&fs=&fsSign=&fc=a03a512fefb9eaf7&fv=&qc005=f1c1ee7ebee4b600f12d153351a38e48&vipType=1&aid=&P00001=&callback=window.jsonp_07077807149559778', goldLogin: 'http://i.vip.iqiyi.com/client/store/pc/checkout.action?platform=b6c13e26323c537d&pid=a0226bd958843452&fs=&fsSign=&fc=a03a512fefb9eaf7&fv=&qc005=f1c1ee7ebee4b600f12d153351a38e48&vipType=1&aid=&P00001=1el6tTVC2n4q6CVz3zbaLTBr7l0fnDm10IjRLWyNOQGSIT89QqcC1tbYnNlqeXlb5m2gd9&callback=window.jsonp_06267915858114745', @@ -9,7 +9,7 @@ const URLS = { module.exports = (type) => { const URLO = new URL(URLS[type]); - const myURL = removeProtocal(URLO.href); + const myURL = removeProtocol(URLO.href); const searchParams = URLO.searchParams; const callbackName = searchParams.get('callback'); return [ diff --git a/test/removeProtocal.unittest.js b/test/removeProtocal.unittest.js index b7ceb92..b7dd0f0 100644 --- a/test/removeProtocal.unittest.js +++ b/test/removeProtocal.unittest.js @@ -1,18 +1,18 @@ "use strict"; -const removeProtocal = require("../lib/removeProtocal"); +const removeProtocol = require("../lib/removeProtocol"); -describe("removeProtocal", () => { +describe("removeProtocol", () => { it("remove http url", () => { - expect(removeProtocal('http://www.abc.com/')).toMatch("www.abc.com"); + expect(removeProtocol('http://www.abc.com/')).toMatch("www.abc.com"); }); it("remove https url", () => { - expect(removeProtocal('https://www.abc.com/def')).toMatch("www.abc.com/def"); + expect(removeProtocol('https://www.abc.com/def')).toMatch("www.abc.com/def"); }); it("remove removed url", () => { - expect(removeProtocal('//www.abc.com/def?query=string')).toMatch("www.abc.com/def?query=string"); + expect(removeProtocol('//www.abc.com/def?query=string')).toMatch("www.abc.com/def?query=string"); }); it("invalid url", () => { - expect(removeProtocal('abc')).toMatch(""); + expect(removeProtocol('abc')).toMatch(""); }); }); \ No newline at end of file