You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于配置文件为WireMock配置带额外条件的请求代理?

WireMock 基于外部配置文件的条件代理转发方案

原生配置可行性说明

你设想的configFile字段不属于WireMock原生映射配置的一部分,原生WireMock不支持这种直接在规则中引入外部配置文件作为匹配条件的写法,当前的配置无法生效。

可行解决方案

方案1:自定义请求匹配器(Custom Request Matcher)

WireMock支持扩展机制实现自定义匹配逻辑,你可以开发一个同时校验请求体和外部配置文件的匹配器:

  1. 实现自定义匹配器代码
    创建类实现RequestMatcher接口,在匹配逻辑中完成双重校验:
  • 校验请求体中$.criteria.code是否等于ABC
  • 读取外部config.json文件,校验$.proxyForwardRequest是否等于yes

示例Java代码:

import com.github.tomakehurst.wiremock.matching.RequestMatcher;
import com.github.tomakehurst.wiremock.matching.RequestMatchResult;
import com.github.tomakehurst.wiremock.http.Request;
import org.json.JSONArray;
import org.json.JSONObject;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ConfigAwareMatcher implements RequestMatcher {
    @Override
    public RequestMatchResult match(Request request) {
        // 校验请求体条件
        boolean bodyMatches = false;
        try {
            JSONObject requestBody = new JSONObject(request.getBodyAsString());
            bodyMatches = "ABC".equals(requestBody.getJSONObject("criteria").getString("code"));
        } catch (Exception e) {
            bodyMatches = false;
        }

        // 校验配置文件条件
        boolean configMatches = false;
        try {
            String configContent = new String(Files.readAllBytes(Paths.get("config.json")));
            JSONArray configArray = new JSONArray(configContent);
            for (int i = 0; i < configArray.length(); i++) {
                JSONObject configItem = configArray.getJSONObject(i);
                if ("proxyForwardRequest".equals(configItem.getString("key")) && "yes".equals(configItem.getString("value"))) {
                    configMatches = true;
                    break;
                }
            }
        } catch (Exception e) {
            configMatches = false;
        }

        return RequestMatchResult.from(bodyMatches && configMatches);
    }
}
  1. 打包与加载扩展
  • 将代码打包成jar文件,放到WireMock的extensions目录下
  • 启动WireMock时指定加载扩展:java -jar wiremock-jre8-standalone.jar --extensions com.yourpackage.ConfigAwareMatcher
  1. 编写映射规则
    在mapping中使用自定义匹配器,替换原有的bodyPatterns和configFile字段:
{
    "mappings": [
        {
            "priority": 1,
            "request": {
                "method": "ANY",
                "urlPathPattern": "/.*",
                "customMatcher": {
                    "name": "config-aware-matcher"
                }
            },
            "response": {
                "proxyBaseUrl": "https://destinationURL.com"
            }
        }
    ]
}

注意:自定义匹配器的name需与WireMock注册的名称对应,通常为类名的驼峰式小写,也可通过注解指定。

方案2:使用脚本式匹配器(无需编译代码)

如果不想编写Java代码,可使用WireMock的脚本支持(Groovy/JavaScript),直接在映射规则中完成双重校验:

示例mapping配置:

{
    "mappings": [
        {
            "priority": 1,
            "request": {
                "method": "ANY",
                "urlPathPattern": "/.*",
                "scriptMatcher": {
                    "script": "groovy",
                    "source": "import groovy.json.JsonSlurper; def body = new JsonSlurper().parseText(request.bodyAsString); def bodyMatch = body.criteria.code == 'ABC'; def config = new JsonSlurper().parse(new File('config.json')); def configMatch = config.any { it.key == 'proxyForwardRequest' && it.value == 'yes' }; return bodyMatch && configMatch;"
                }
            },
            "response": {
                "proxyBaseUrl": "https://destinationURL.com"
            }
        }
    ]
}

这种方式无需编译代码,直接通过Groovy脚本实现逻辑,WireMock standalone版本通常已内置Groovy支持。

自定义开发说明

WireMock作为开源项目,扩展机制就是为了支持这类自定义需求,你完全可以基于现有接口开发符合业务逻辑的扩展,除了自定义匹配器,还可结合全局变量、响应转换器等实现更复杂的逻辑。

内容的提问来源于stack exchange,提问作者Martin P.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 20:22:48