Skip to content

Commit 15ad277

Browse files
author
zhaoxuxu
committed
feature: bodyArgs
1 parent 212b4f7 commit 15ad277

File tree

7 files changed

+46
-4
lines changed

7 files changed

+46
-4
lines changed

src/fake-services/example/__param1__/map

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
# to match value
1919
# e.g. _pa1={.*}&pa2={.*}
2020
#
21+
# +bodyArgs : optional
22+
# starts with +
23+
# urlencoded, braces to include a regular expression
24+
# to match value
25+
# e.g. +arg1={.*}&arg2={.*}
26+
#
2127
# responsePath : required
2228
# search response file by this field based on current
2329
# directory
@@ -27,8 +33,10 @@
2733
# ], divided with space(s), e.g:
2834
# GET ?qu1={.*}&qu2={.*} _pa1={.*}&pa2={.*} ./response
2935
# @2019-5-29 created by @zhaoxuxu
36+
# @2021-3-17 updated by @zhaoxuxu
3037

3138
GET ?range={.*}&n={.*} _param1={.*} ./response
3239
GET _param1={.*} ./response
3340
GET ?range={.*}&n={.*} ./response
41+
POST +username={xyz}&password={.*} ./response
3442
GET ./response
File renamed without changes.

src/fake-services/proxy404

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# https://nodejs.org
22

33
baidu https://baidu.com
4-
node https://nodejs.org
4+
bing https://bing.com
5+
https://nodejs.org

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ if (fs.existsSync(proxy404CfgFile)
3636
});
3737
}
3838

39+
const REQUEST_MAX_SIZE = '10mb';
40+
app.use(express.json({limit: REQUEST_MAX_SIZE}));
3941
app.use(mapToRes);
4042

4143
function tillListen(tryPort) {

src/middlewares/mapToRes/matchAResponse.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function matchOneLine(line, req){
3939
method: fields[0],
4040
querys: parseQueryStr(fields.find(fld => fld[0] === '?')),
4141
pathParams: parseQueryStr(fields.find(fld => fld[0] === '_')),
42+
bodyArgs: parseQueryStr(fields.find(fld => fld[0] === '+')),
4243
responsePath: fields[fields.length - 1],
4344
};
4445

@@ -86,6 +87,26 @@ function matchOneLine(line, req){
8687
if (!pathParamsOk)
8788
return false;
8889

90+
let bodyArgsOk = false;
91+
if (_.isObject(cfg.bodyArgs)) {
92+
bodyArgsOk = true;
93+
const keys = Object.keys(cfg.bodyArgs);
94+
for (let i = 0; i < keys.length; i++) {
95+
const key = keys[i];
96+
const rule = cfg.bodyArgs[key];
97+
if(!isRuleMatch(rule, req.body[key])) {
98+
bodyArgsOk = false;
99+
break;
100+
}
101+
}
102+
} else if (cfg.bodyArgs === false){
103+
bodyArgsOk = true;
104+
} else {
105+
return new Error('Abnormal condition.');
106+
}
107+
if (!bodyArgsOk)
108+
return false;
109+
89110
return cfg.responsePath;
90111
}
91112

@@ -102,15 +123,19 @@ function walkMapFileLines(mapFilePath, req) {
102123
const tmp = matchOneLine(line, req);
103124
if (tmp) {
104125
responsePath = tmp;
105-
lineReader.close();
106126
resolve(responsePath);
127+
lineReader.close();
107128
}
108129
}
109130
catch (err) {
110131
err.message = `Bad rule for matching: ${line}. ${err.message}`;
111132
reject(err.message);
112133
}
113134
});
135+
lineReader.on('close', () => {
136+
// handle only EOF situation
137+
reject('No match.');
138+
});
114139
});
115140
}
116141

@@ -124,7 +149,10 @@ function match(req) {
124149
if (fs.existsSync(mapFilePath)) {
125150
walkMapFileLines(mapFilePath, req).then(
126151
responsePath => resolve(pathUtil.resolve(resource.path, responsePath)),
127-
reason => reject(reason),
152+
reason => {
153+
console.error(reason);
154+
resolve(null);
155+
},
128156
);
129157
} else if (fs.existsSync(defaultResponsePath)) {
130158
resolve(defaultResponsePath);

src/test/curl

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
curl localhost:3000/example/sdfd?sdsd=223 -v
22
curl localhost:3000/404/sdfd?sdsd=223 -v
3+
curl localhost:3000/example/sdfd?sdsd=223 -v \
4+
-H 'content-type: application/json' \
5+
--data '{"username":"xyz","password":"xyz"}'

src/utils/parseQueryStr.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = function(str) {
22
if (!str) return false;
33
let ret = {};
4-
if (str[0] === '?' || str[0] === '_')
4+
if (str[0] === '?' || str[0] === '_' || str[0] === '+')
55
str = str.slice(1);
66
str.split('&')
77
.filter(one => one.length > 0)

0 commit comments

Comments
 (0)