自己看官网整理的关于Mountebank 的几个常用的命令:
启动时加载上次保存的服务
mb start --configfile saved.json
创建一个服务
curl -X POST -d @test.json http://localhost:2525/imposters
test.json
{
"protocol": "http",
"port": 3000
}
添加代理
curl -X POST -d @test.json http://localhost:2525/imposters/3000/stubs
test.json
{
"stub":
{
"responses": [
{
"proxy": {
"to": "http://localhost:8080",
"mode": "proxyAlways"
}
}
]
}
}
在当前端口上新增接口:
curl -X POST -d @test.json http://localhost:2525/imposters/3000/stubs
test.json
{
"index": 0,
"stub":
{
"predicates":[{
"equals":{
"method":"GET",
"path":"/test2"
}
}],
"responses": [
{
"is": {
"statusCode": 200,
"headers": { "Content-Type": "application/json" },
"body": "{ \"message\": \"Hello, world12!\" }"
}
}
]
}
}
删除某个接口
curl -X DELETE http://localhost:2525/imposters/3000/stubs/0
修改某个接口
curl -X PUT -d @test.json http://localhost:2525/imposters/3000/stubs/0
test.json
{
"responses": [
{
"is": {
"statusCode": 200,
"headers": { "Content-Type": "application/json" },
"body": "{ \"message\": \"Hello, world1333!\" }"
}
}
]
}
获取所有服务
curl -X GET http://localhost:2525/imposters
获取某个服务所有的接口信息
curl -X GET http://localhost:2525/imposters/3000
要使mountebank能支持xml格式请求报文的匹配,这时要去修改源码,使其在接收请求参数时,将所有的换行符替换为空,同时在写入xml数据时也要使所有的换行符为空,以下为mountebank源码修改部分:
async function getResponseFor (request, requestDetails) {
if (!isAllowedConnection(request.ip, logger)) {
metrics.blockedIPCount.inc({ imposter: logger.scopePrefix });
return { blocked: true, code: 'unauthorized ip address' };
}
const start = new Date();
metrics.requestCount.inc({ imposter: logger.scopePrefix });
numberOfRequests += 1;
if (recordRequests) {
await stubs.addRequest(request);
}
console.log("origin request is ",request);
const request1 = JSON.parse(JSON.stringify(request));
let jsonflag = false;
try{
JSON.parse(request1.body);
jsonflag = true;
}catch(e){
jsonflag = false;
}
if(jsonflag==false){
request1.body = request1.body.replaceAll(/\r?\n/g,"");
request1.body = request1.body.replaceAll(/[\s\t]+/g,"");
}
console.log("request1 is ",request1);
const match = await findFirstMatch(request1),
observeResponseGenerationDuration = metrics.responseGenerationDuration.startTimer(),
responseConfig = await match.stub.nextResponse();
if (responseConfig.proxy) {
console.log("proxy exits");
console.log("request is ",request);
const match1 = await findFirstMatch(request),
observeResponseGenerationDuration1 = metrics.responseGenerationDuration.startTimer(),
responseConfig1 = await match1.stub.nextResponse();
logger.debug(`generating response from ${JSON.stringify(responseConfig)}`);
const response = await resolver.resolve(responseConfig1, request, logger, imposterState, requestDetails);
observeResponseGenerationDuration1({ imposter: logger.scopePrefix });
if (config.recordMatches) {
await recordMatch(match1.stub, request, response, responseConfig, start);
}
return response;
}else{
console.log("proxy not exits");
logger.debug(`generating response from ${JSON.stringify(responseConfig)}`);
const response = await resolver.resolve(responseConfig, request, logger, imposterState, requestDetails);
observeResponseGenerationDuration({ imposter: logger.scopePrefix });
if (config.recordMatches) {
await recordMatch(match.stub, request, response, responseConfig, start);
}
return response;
}
}