在 wiremock 中添加自定义字符串助手

add custom string helpers in wiremock

我需要在 wiremock 中添加自定义辅助函数,它根据查询字符串中的输入值生成一个字符串。 需要添加多个功能。 正在寻找这方面的工作样本。

//I am able to get the sample code working
            Helper<String> serialNumberHelper = new Helper<String>() {
                // @Override    Helper<String> stringLengthHelper = new Helper<String>() {
                    // @Override
                    public Object apply(String context, Options options)
                            throws IOException {
                        return context.length();
                    }
                };
                public Object apply(String context, Options options)
                        throws IOException {
                    return context.substring(2);
                }
            };

            Map<String, Helper> helpers = new HashMap<String, Helper>();
            helpers.put("GetLength", stringLengthHelper);
            helpers.put("GetSerialNumber", serialNumberHelper);

            wireMockServer = new WireMockServer(
                    wireMockConfig().port(8080).options().extensions(
                            new ResponseTemplateTransformer(false, helpers))); 
// and able to use the same in response.

            wireMockServer
                    .stubFor(get(urlMatching("/vis/([a-z]*)\?unitid=([0-9]*)"))
                            .willReturn(aResponse()
                                    .withHeader("Content-Type", "text/plain")
                                    .withBody(
                                            "{{GetSerialNumber request.query.unitid.[0]}}")
                                    .withTransformers("response-template")));