gpt4 book ai didi

java - Wiremock - 第一次映射错误时重定向到代理

转载 作者:行者123 更新时间:2023-12-02 01:34:10 29 4
gpt4 key购买 nike

我正在尝试使用 Wiremock 2.24.1 根据请求路径动态返回不同的主体文件,如果在本地找不到,我想调用外部资源(作为代理)。

我正在使用这个映射文件:

{
"priority": 1,
"request" : {
"urlPattern" : "/rest/v2/name/.*",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "rest_v2_name_{{request.requestLine.pathSegments.[3]}}_body.json",
"headers" : {
"Content-Type" : "application/json;charset=utf-8"
},
"transformers": ["response-template"]
}
}

我有一个名为 rest_v2_name_springfield_body.json 的文件,其中包含此内容

[
{
"name": "Springfield",
"now": "{{now}}",
"yesterday"; "{{now offset='-1 days' format='yyyy-MM-dd HH:mm:ssZ'}},
"tomorrow"; "{{now offset='1 days' format='yyyy-MM-dd HH:mm:ssZ'}}
}
]

当我打电话curl http://localhost:8099/rest/v2/name/springfield时返回:

[
{
"name": "Springfield",
"now": "2019-08-17T00:23:12Z",
"yesterday"; "2019-08-15 21:23:12-0300,
"tomorrow"; "2019-08-17 21:23:12-0300
}
]

如果我使用另一个路径值调用(例如 http://localhost:8099/rest/v2/name/brasil ),我希望由其他映射处理:

{
"priority": 10,
"request" : {
"urlPattern" : "/rest/v2/name/.*",
"method" : "GET"
},
"response" : {
"proxyBaseUrl" : "http://restcountries.eu"
}
}

但不是回答 http://restcountries.eu/rest/v2/name/brasil 的响应,我收到此消息的本地 HTTP 500 错误(我相信它来自第一个匹配的映射):

java.io.FileNotFoundException: ./__files/rest_v2_name_brasil_body.json

我尝试过这个替代方案

  • 我禁用了第一个映射(只是为了确定),wiremock 按预期充当代理。
  • 我在代理映射文件上尝试了更广泛的 urlPattern“.*”,但没有成功,第一个映射仍然应答了调用。
  • 我切换了优先级(1 为代理,10 为本地映射),但所有调用均由代理应答,包括“springfield”。

有没有办法不用编码就能解决这个场景?

最佳答案

无论代理开/关如何,Wiremock 将始终遵循优先级。

Considering that you have the same request pattern,

Case-I : When you have Proxy(brasil) at priority 1 and stub-mapping(spingfield) at priority 10 : You will be getting all the responses through Proxy only. There is no failover check for other mappings kind of thing.

Case-I : When you have Proxy(brasil) at priority 10 and stub-mapping(spingfield) at priority 1 : You will be getting the same response as stub-mapping you have configured since every request will be going to satisfy the springfield rule.

要解决这个问题,您需要在代理上记录每个请求。

停止录制后,您可以检查映射并根据您的要求添加其他映射!

关于java - Wiremock - 第一次映射错误时重定向到代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57532559/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com