Mountebank 配置详解

自己看官网整理的关于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;
        }
        
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值