Skip to content

Commit

Permalink
feat: support shortcut configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbin committed Nov 15, 2018
1 parent 21f580f commit bcf85b1
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 23,025 deletions.
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mock service base on [Anyproxy](https://www.npmjs.com/package/anyproxy).
[![npm package](https://img.shields.io/npm/v/@tiandatong/anymock.svg?style=flat-square)](https://www.npmjs.org/package/@tiandatong/anymock)
[![NPM downloads](http://img.shields.io/npm/dm/@tiandatong/anymock.svg?style=flat-square)](http://npmjs.com/@tiandatong/anymock)
[![Percentage of issues still open](http://isitmaintained.com/badge/open/temberature/anymock.svg)](http://isitmaintained.com/project/temberature/anymock "Percentage of issues still open")

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
</div>

English | [简体中文](./docs/README-zh_CN.md)
Expand All @@ -38,6 +38,9 @@ create a directory contains RESTAPI.config.json and RESTAPI.json
"URL": "//api.github.com/repos/temberature/anymock/branches",
"OPTIONS": ["normal", "protected"],
"choices": ["normal"]
},{
"URL": "https://suggest.taobao.com/sug",
"choices": 1
}]
}
```
Expand Down Expand Up @@ -74,17 +77,46 @@ create a directory contains RESTAPI.config.json and RESTAPI.json
}
}
}
}, {
"url": "https://suggest.taobao.com/sug",
"default": {
"Content-Type": "text/html",
"fileHead": "",
"fileFooter": "",
"body": {
"result": [
[
"apple watch4",
"14770"
],
[
"apple pencil",
"12500"
]
],
"shop": "apple",
"tmall": "apple"
}
}
}]
```

run command below in the root dir

```bash
anymock
λ anymock
[AnyProxy Log][2018-11-15 12:35:52]: throttle :10000kb/s
[AnyProxy Log][2018-11-15 12:35:52]: clearing cache file...
[AnyProxy Log][2018-11-15 12:35:52]: ==>>> clearing cache
[AnyProxy Log][2018-11-15 12:35:52]: closing webserver...
[AnyProxy Log][2018-11-15 12:35:52]: Http proxy started on port 8001
[AnyProxy Log][2018-11-15 12:35:52]: Active rule is: a rule to hack response
```

then you can request https://api.github.com/repos/temberature/anymock/branches?protected=0
and see the corresponding result
configure chrome's proxy to http://127.0.0.1:8001 by [Proxy SwitchyOmega](https://chrome.google.com/webstore/detail/proxy-switchyomega/padekgcemlokbadohgkifijomclgjgif)

then you can browse https://api.github.com/repos/temberature/anymock/branches?protected=0 or https://suggest.taobao.com/sug?code=utf-8&callback=KISSY.Suggest.callback&q=apple
and see the corresponding mock result

```json
[{
Expand All @@ -96,6 +128,25 @@ and see the corresponding result
}]
```

```json
KISSY.Suggest.callback(
{
"result": [
[
"apple watch4",
"14770"
],
[
"apple pencil",
"12500"
]
],
"shop": "apple",
"tmall": "apple"
}
)
```

## Running the tests

Explain how to run the automated tests for this system
Expand Down
5 changes: 5 additions & 0 deletions lib/RESTAPIMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ function RESTAPIMock(myURL, searchParams, callbackName) {
}

} else {
const option = RESTAPI.default
if (option) {
response = createRes(callbackName, option)
return true
}
if (!RESTAPI.needPars || searchParams.get(RESTAPI.needPars)) {
response = createRes(callbackName, RESTAPI);
return true;
Expand Down
7 changes: 2 additions & 5 deletions lib/createRes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ function createRes(callbackName, mockMap) {
type = mockMap["Content-Type"];
var rt, jsonp;
if (callbackName) {
jsonp = callbackName + "(" + JSON.stringify(body) + ");";
jsonp = callbackName + "(" + JSON.stringify(body) + ")";
if (type === "text/html") {
rt =
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript"> document.domain="iqiyi.com"; window.parent.' +
jsonp +
"</script></head><body></body></html>";
rt = mockMap.fileHead + jsonp + mockMap.fileFooter;
} else {
rt = jsonp;
}
Expand Down
58 changes: 7 additions & 51 deletions mock/RESTAPI.config.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,11 @@
{
"disabled": false,
"configs": [{
"URL": "//api.github.com/repos/temberature/anymock/branches",
"OPTIONS": ["normal", "protected"],
"choices": ["normal"]
}, {
"URL": "//ip.if.iqiyi.com/cityjson",
"OPTIONS": ["mainland", "international"],
"choices": 0
},
{
"URL": "//i.vip.iqiyi.com/client/store/pc/checkout.action",
"OPTIONS": ["gold", "diamondNotLogin", "diamond"],
"choices": ["gold", "diamondNotLogin", "diamond"]
},
{
"URL": "//i.vip.iqiyi.com/order/queryOrderStat.action",
"OPTIONS": ["qrcode", "mobile"],
"choices": ["mobile"]
},
{
"URL": "//i.vip.iqiyi.com/pay/dopay.action",
"choices": 0
},
{
"URL": "//i.vip.iqiyi.com/pay/mobile-pay-vd.action",
"choices": 0
},
{
"URL": "//i.vip.iqiyi.com/pc/payconfirm/confirmOrderStat.action",
"choices": 0
},
{
"URL": "//i.vip.iqiyi.com/pay/readQd.action",
"choices": 0
},
{
"URL": "//passport.iqiyi.com/apis/user/info/queryWithVerify.action",
"choices": 0
},
{
"URL": "//vip.iqiyi.com/security/info/get",
"choices": 1
},
{
"URL": "//vip.iqiyi.com/card/info.action",
"choices": 1
},
{
"URL": "//vip.iqiyi.com/card/verifyWalletPassword.action",
"choices": 1
}
]
"URL": "//api.github.com/repos/temberature/anymock/branches",
"OPTIONS": ["normal", "protected"],
"choices": ["normal"]
},{
"URL": "https://suggest.taobao.com/sug",
"choices": 1
}]
}
Loading

0 comments on commit bcf85b1

Please sign in to comment.