如何在Mountebank中使用代理记录请求和响应? [英] How to record request and reponse using a proxy in Mountebank?

查看:210
本文介绍了如何在Mountebank中使用代理记录请求和响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mountebank 创建冒名顶替者程序,并希望记录请求和响应.为了创建http冒名顶替者,我按照其文档中的说明使用了以下CURL命令.

I am creating an imposter process using Mountebank and want to record the request and response. To create a http imposter I used the following CURL command as described in their documentation.

curl -i -X POST -H 'Content-Type: application/json' http://127.0.0.1:2525/imposters --data '{
  "port": 6568,
  "protocol": "http",
  "name": "proxyAlways",
  "stubs": [
    {
      "responses": [
        {
          "proxy": {
            "to": "http://localhost:8000",
            "mode": "proxyAlways",
            "predicateGenerators": [
              {
                "matches": {
                  "method": true,
                  "path": true,
                  "query": true
                }
              }
            ]
          }
        }
      ]
    }
  ]
}'

我在http://localhost:8000处运行着另一台服务器,该服务器正在侦听端口6568发出的所有请求.

I have another server running at http://localhost:8000 which is listening to all the request coming to port 6568.

现在我的服务器输出:

mb
info: [mb:2525] mountebank v1.6.0-beta.1102 now taking orders - point your browser to http://localhost:2525 for help
info: [mb:2525] POST /imposters
info: [http:6568 proxyAlways] Open for business...
info: [http:6568 proxyAlways] ::ffff:127.0.0.1:55488 => GET /

我想记录所有的请求和响应,并且现在无法执行.当我输入curl -i -X GET -H 'Content-Type: application/json' http://127.0.0.1:6568/时,它会给我一个响应,但是我如何存储它呢?

I want to record all the request and response going around, and unable to do right now. When I enter curl -i -X GET -H 'Content-Type: application/json' http://127.0.0.1:6568/ it is giving me a response but how I do store it?

任何人都可以向我解释

在代理响应前面的新存根中保存响应:

save off the response in a new stub in front of the proxy response:

(来自此 Mountebank文档)

推荐答案

如何存储代理结果

简短的答案是mountebank已经正在存储它.您可以通过查看curl http://localhost:2525/imposters/6568的输出来验证.真正的问题是如何重播存储的响应?

The short answer is that mountebank already is storing it. You can verify that by looking at the output of curl http://localhost:2525/imposters/6568. The real question is how do you replay the stored response?

mountebank代理的常见用法是在一个正在运行的mb实例上记录代理响应,保存结果,然后使用保存的响应启动下一个mb实例.您这样做的方法是让被测系统与您要尝试在任何需要的条件下通过mountebank代理进行存根的服务进行对话,然后通过发送响应来保存响应(及其请求谓词). HTTP GETDELETEhttp://localhost:2525/imposters/6568?removeProxies=true&replayable=true.您可以通过REST API,或者通过将其保存在磁盘上并使用诸如mb --configfile savedProxyResults.json之类的命令启动mountebank,将该响应的JSON主体输入到下一个mb实例中.届时,mountebank将提供与请求完全相同的响应,而无需连接到下游服务.

The common usage scenario with mountebank proxies is that you record the proxy responses on one running instance of mb, save off the results, and then start the next instance of mb with those saved responses. The way you would do that is to have the system under test talk to service you're trying to stub out via the mountebank proxy under whatever conditions you need it to, and then save off the responses (and their request predicates) by sending an HTTP GET or DELETE to http://localhost:2525/imposters/6568?removeProxies=true&replayable=true. You feed the JSON body of that response into the next mb instance, either through the REST API, or by saving it on disk and starting mountebank with a command of something like mb --configfile savedProxyResults.json. At that point, mountebank is providing the exact same responses to the requests without connecting to the downstream service.

代理创建新的存根

您的最后一个问题是围绕了解proxyAlways模式的工作原理.默认的proxyOnce模式意味着mountebank代理第一次看到唯一满足谓词的请求时,它将查询下游服务并保存响应.下次看来是一个满足完全相同谓词的请求时,它将避免进行下游调用,而仅返回保存的结果.它仅对相同请求的下游一次进行代理.另一方面,proxyAlways模式始终将请求发送到下游,并保存同一请求的响应列表.

Your last question revolves around understanding how the proxyAlways mode works. The default proxyOnce mode means that the first time a mountebank proxy sees a request that uniquely satisfies a predicate, it queries the downstream service and saves the response. The next time it seems a request that satisfies the exact same predicates, it avoids the downstream call and simply returns the saved result. It only proxies downstream once for the same request. The proxyAlways mode, on the other hand, always sends the requests downstream, and saves a list of responses for the same request.

为清楚起见,在您复制的示例中,我们关心请求上的methodpathquery字段,因此,如果我们看到两个请求,这三个字段的组合完全相同,我们需要知道我们应该将保存的响应发回还是继续代理.想象一下我们第一次发送:

To make this clear, in the example you copied we care about the method, path, and query fields on the request, so if we see two requests with exactly the same combination of those three fields, we need to know whether we should send the saved response back or continue to proxy. Imagine we first sent:

GET /test?q=elephants

methodGETpath/test,而queryq=elephants.由于这是第一个请求,因此我们将其发送到下游服务器,该服务器返回以下内容:

The method is GET, the path is /test, and the query is q=elephants. Since this is the first request, we send it to the downstream server, which returns a body of:

No results

无论您将mountebank设置为哪种代理模式,这都是正确的,因为它必须至少向下游查询一次.现在假设,我们正在考虑时,下游服务添加了一个大象,然后我们的受测系统进行了相同的调用:

That will be true regardless of which proxy mode you set mountebank to, since it has to query downstream at least once. Now suppose, while we're thinking about it, the downstream service added an elephant, and then our system under test makes the same call:

GET /test?q=elephants

如果我们处于proxyOnce模式,那么将大象添加到真实服务中这一事实将无关紧要,我们将继续返回保存的响应:

If we're in proxyOnce mode, the fact that the elephant was added to the real service simply won't matter, we'll continue to return our saved response:

No results

如果如上所述关闭mountebank进程并重新启动它,您将看到相同的行为.在您保存的配置文件中,您会看到类似以下内容(简化):

You'd see the same behavior if you shut the mountebank process down and restarted it as described above. In the config file you saved, you'd see something like this (simplifying a bit):

"stubs": [
  {
    "predicates": [{
      "deepEquals': {
        "method": "GET",
        "path": "/test",
        "query": { "q": "elephants" }
      }
    }],
    "responses": [
      {
        "is": {
          "body": "No results"
        }
      }
    ]
  }
]

只有一个存根.另一方面,如果我们使用proxyAlways,则对GET /test?q=elephants的第二次调用将产生新的大象:

There's only the one stub. If, on the other hand, we use proxyAlways, then the second call to the GET /test?q=elephants would yield the new elephant:

1. Jumbo reporting for duty!

这很重要,因为如果我们关闭mountebank进程并重新启动它,那么我们的测试现在可以依靠这样的事实:我们将循环遍历这两个响应:

This is important, because if we shut down the mountebank process and restart it, now our tests can rely on the fact that we'll cycle through both responses:

"stubs": [
  {
    "predicates": [{
      "deepEquals': {
        "method": "GET",
        "path": "/test",
        "query": { "q": "elephants" }
      }
    }],
    "responses": [
      {
        "is": {
          "body": "No results"
        }
      },
      {
        "is": {
          "body": "1. Jumbo reporting for duty!"
        }
      }
    ]
  }
]

这篇关于如何在Mountebank中使用代理记录请求和响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆