Skip to content

Commit

Permalink
fix: typo.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbin committed Nov 8, 2018
1 parent c218d36 commit 2d7480a
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 30 deletions.
6 changes: 3 additions & 3 deletions lib/RESTAPIMock.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 => {
Expand Down
10 changes: 5 additions & 5 deletions lib/anyproxy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions lib/fileMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions lib/removeProtocal.js

This file was deleted.

5 changes: 5 additions & 0 deletions lib/removeProtocol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function removeProtocol(url) {
return url.replace(/(^\w+:|^)\/\//, '');
}

module.exports = removeProtocol
4 changes: 2 additions & 2 deletions lib/startsWith.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions test/fileMock.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/getParas.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 [
Expand Down
12 changes: 6 additions & 6 deletions test/removeProtocal.unittest.js
Original file line number Diff line number Diff line change
@@ -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("");
});
});

0 comments on commit 2d7480a

Please sign in to comment.