可以同时使用WireMock播放和录制吗? [英] Can WireMock play and record be used at the same time?

查看:151
本文介绍了可以同时使用WireMock播放和录制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到外部第三方API的应用程序,最近API的测试实例并不是特别可靠。这会停止开发进度并将我们的构建管道变为红色,所以我想添加一个HTTP记录代理来阻止这种情况发生。

I have an application that connect to an external third-party API, and of late the test instance of the API has not been particularly reliable. This stops development progress and turns our build pipelines to red, so I'd like to add an HTTP recording proxy to stop this happening.

我有 WireMock 推荐,并尝试过记录和播放功能,它看起来确实非常好 - 它几乎开箱即用。我们只需更改外部服务的配置URL,然后按以下方式记录:

I've had WireMock recommended, and having tried its record and play features, it does seem to be very good - it pretty much worked out of the box. We simply change the configured URL for the external service, and then record like this:

java -jar wiremock-standalone-2.3.1.jar \
    --port 8080 \
    --proxy-all="https://test-api.example.com/" \
    --record-mappings \
    --verbose

这会在当前工作目录中创建缓存文件夹,然后它可以通过杀死录音机并切换到播放来切换到播放模式:

This creates cache folders in the current working dir, and then it can be switched to playback mode by killing the recorder and switching to playback:

java -jar wiremock-standalone-2.3.1.jar \
    --port 8080 \
    --verbose

但是,要在实践中使用它,我需要设置一个运行测试回归包的应用程序实例来记录大量的API使用情况,然后每天说一次。然后,我需要停止录制器并将缓存文件复制到播放过程,然后重新开始播放过程。

However, to use this in practice I would need to set up an instance of my app running through a test regression pack to record lots of API usage, and to kick that off say one a day. I'd then need to stop the recorder and copy the cache files over to a playback process and then restart the playback process.

这可能会起作用,但感觉就像很多活动部件,理想情况下我想同时进行游戏和录音。如果需要新的API调用(由于自然项目更改),这将允许自动刷新缓存,但默认情况下将在找到匹配项时进行回放。

This probably would work, but it feels like a lot of moving parts, and ideally I would like to run play and record at the same time. This would allow the cache to be automatically refreshed if a new API call becomes necessary (due to natural project changes) but would playback by default where a match is found.

是这可能吗?我不是Java程序员,但假设如果要编写WireMock插件,它可能是可用的。如果可以在控制台上完成这将是很好的,但手册的措辞表明,维护者将游戏和记录视为单独的事物。

Is this possible? I am not a Java programmer, but suppose it might be available if one were to write a WireMock plugin. It would be great if this could be done at the console, but the phrasing of the manual indicates that play and record are thought of by the maintainers as separate things.

I我是否想知道是否可以切换到 Mountebank ,这看起来像是它可能会支持,但事实证明播放和录制这里也是单独的模式。在任何情况下,我都喜欢WireMock的开始是多么容易,所以如果可能的话,我希望坚持下去。

I did wonder whether I might switch to Mountebank, which looked like it might support this, however it turns out that play and record are separate modes here too. In any case, I like how easy WireMock has been to get started, so would like to stick with it if possible.

推荐答案

但是,WireMock不会完全按你所要求的那样做:

WireMock won't quite do what you're asking at the moment, however:

1)你可以使用 - proxy-all 参数而不是 - 记录映射播放时。这将导致现有存根映射(已记录或以其他方式)无法匹配的任何请求被代理到实际服务。

1) You can use the --proxy-all parameter and not --record-mappings when playing back. This will cause any request unmatched by an existing stub mapping (recorded or otherwise) to be proxied through to the actual service.

2)一种让你漂亮的解决方法接近您所追求的是在您之后将 POST 发送到 / __ admin / mappings / reset 端点d收集了一些新记录的映射。这会导致扫描文件系统并加载所有映射(重新)。

2) A workaround that'd get you pretty close to what you're after would be to send a POST to /__admin/mappings/reset endpoint after you'd collected some new recorded mappings. This causes the filesystem to be scanned and all the mappings (re)loaded.

这篇关于可以同时使用WireMock播放和录制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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