我有一个像下面的示例一样运行的wiremock实例:
java -jar wiremock-standalone-2.19.0.jar --port=8080 --root-dir="/data" --proxy-all="https://api.github.com" --verbose
现在,当我开始在 localhost:8080/__admin/recorder/上为域 https://api.github.com
进行录制并请求像这样的代理 URL
https://api.github.com/repos/MyOrganisationName/MyRepoName/contents/
我收到以下回复:
[
{
"name": "a-file-in-my-repo.md",
"path": "content/articles/a-file-in-my-repo.md",
"sha": "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"size": 0,
"url": "https://api.github.com/repos/MyOrganisationName/MyRepoName/contents/content/articles/a-file-in-my-repo.md?ref=develop",
"html_url": "https://github.com/MyOrganisationName/MyRepoName/blob/develop/content/articles/a-file-in-my-repo.md",
"git_url": "https://api.github.com/repos/MyOrganisationName/MyRepoName/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"download_url": "https://raw.githubusercontent.com/MyOrganisationName/MyRepoName/develop/content/articles/a-file-in-my-repo.md",
"type": "file",
"_links": {
"self": "https://api.github.com/repos/MyOrganisationName/MyRepoName/contents/content/articles/a-file-in-my-repo.md?ref=develop",
"git": "https://api.github.com/repos/MyOrganisationName/MyRepoName/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"html": "https://github.com/MyOrganisationName/MyRepoName/blob/develop/content/articles/a-file-in-my-repo.md"
}
}
]
现在的“问题”是我也想关注这些网址,例如
https://api.github.com/repos/MyOrganizationName/MyRepoName/contents/content/articles/a-file-in-my-repo.md?ref=develop
— 但随后请求是直接针对 github api 执行的,而不是通过我配置的wiremock 代理执行。
所以我需要一些重写/替换功能(在wiremock级别)来重写url的匹配https://api.github.com至http://localhost:8080在保留录音之前,我会得到如下响应:
[
{
"name": "a-file-in-my-repo.md",
"path": "content/articles/a-file-in-my-repo.md",
"sha": "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"size": 0,
"url": "http://localhost:8080/repos/MyOrganisationName/MyRepoName/contents/content/articles/a-file-in-my-repo.md?ref=develop",
"html_url": "https://github.com/MyOrganisationName/MyRepoName/blob/develop/content/articles/a-file-in-my-repo.md",
"git_url": "http://localhost:8080/repos/MyOrganisationName/MyRepoName/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"download_url": "https://raw.githubusercontent.com/MyOrganisationName/MyRepoName/develop/content/articles/a-file-in-my-repo.md",
"type": "file",
"_links": {
"self": "http://localhost:8080/repos/MyOrganisationName/MyRepoName/contents/content/articles/a-file-in-my-repo.md?ref=develop",
"git": "http://localhost:8080/repos/MyOrganisationName/MyRepoName/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"html": "https://github.com/MyOrganisationName/MyRepoName/blob/develop/content/articles/a-file-in-my-repo.md"
}
}
]
是否可以以某种方式无需手动更换并每次重新启动wiremock?
提前致谢
最佳答案
Wiremock不会关心http://localhost:8080与 http://github.com因为如果端点匹配,它已经重写了这些。
如果您按照预期方式进行设置,您仍然需要为 Wiremock 提供自定义映射才能提供正确的响应。
您能否详细说明为什么要替换网址?
为了尝试回答您的部分问题,我不知道在初始代理期间转换映射响应的方法,但是您可以使用类似 OpenTable's Wiremock Body Transformer 的方法。清理录音,以便它们在将来播放时按照您希望的方式工作。
关于java - Wiremock 记录 — 在记录模式下重写响应 'on the fly',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52890629/