【问题标题】:Wiremock proxying to different Url like Apache ProxyPassWiremock 代理到不同的 Url,如 Apache ProxyPass
【发布时间】:2021-05-22 22:09:00
【问题描述】:

我正在尝试实现一些非常简单的事情:

代理请求

mock.com/foo?paramA=valueA&paramB=valueB

backend.com/bar?paramA=valueA&paramB=valueB

我想使用 json 配置来做到这一点。

问题是 proxyBaseUrl 总是从输入中获取 FULL Url 并附加它,所以

{
    "request": {
        "method": "GET",
        "urlPattern": "/foo/.*"
    },
    "response": {
        "proxyBaseUrl": "http://backend.com/bar"
    }
}

我收到一个请求 http://backend.com/bar/foo?paramA=valueA&paramB=valueB 这显然不是我需要的。

我需要一些方法来使用捕获组来获取部分请求 url,例如

"urlPattern": "/foo/(.*)"

然后是一种将捕获的组插入到目标 url 路径中的方法 - 仅此而已。

如何通过 JSON 配置来做到这一点?

我检查了wiremock文档并浏览了十几个讨论,但我仍然不清楚。

这两个帖子有同样的问题,没有得到任何答案:

https://groups.google.com/g/wiremock-user/c/UPO2vw4Jmhw/m/Rx0e8FtZBQAJ

https://groups.google.com/g/wiremock-user/c/EVw1qK7k8Fo/m/5iYg1SQEBAAJ

所以我想知道这在wiremock中是否可能? (在 Apache 中是 2-liner)

【问题讨论】:

    标签: proxypass wiremock capture-group


    【解决方案1】:

    据我所知,代理无法以这种方式进行配置。查看文档,WireMock 只会通过 proxyBaseUrl 代理相同的请求。

    不幸的是,看起来您最好的选择是编写一个自定义响应转换器来为您执行此重定向。我认为转换器类中给出的请求/响应对象不会自行处理重定向,因此您可能需要设置自己的客户端来转发请求。

    伪代码如:

    class MyCustomTransformer extends ResponseTransformer {
        public String getName() {
             return "MyCustomTransformer";
        }
    
        @Override
        public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
            Pattern pattern = Pattern.compile("/regex/url/to/match/");
            Matcher matcher = pattern.matcher(request.getUrl());
    
            if (matcher.matches()) {
                // Code to modify request and send via your own client
                // For the example, you've saved the returned response as `responseBody`
                return Response.Builder.like(response).but().body(responseBody.toJSONString()).build();
            } else {
                return response
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2021-09-16
      • 2023-04-10
      • 1970-01-01
      • 2015-11-22
      • 2021-05-16
      • 2014-08-08
      相关资源
      最近更新 更多