wiremock responsedefinitiontransformer不处理转换器参数

sg3maiej  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(487)

其主要思想是首先处理来自请求数据的数据(解密、计算等),然后将一些结果值传递给json响应文件。我试图在wiremock中创建一个响应定义转换器。Map文件如下所示:

"priority": 2,
"request": {
  "method": "GET",
  "urlPattern": "/\\?id=(.*)"
},
"response": {
  "status": 200,
  "transformers": ["id-transformer", "response-template"],
  "headers": {
    "Content-Type": "application/json"
   }
}

json响应文件如下所示:

{
  "value1": "{{ parameters.value1 }}",
  "value2": "{{ parameters.value2 }}"
}

在responsedefinitiontransformer扩展类中,我从请求url获取id,并将其和传递值作为转换器参数进行处理:

public class IdResponseTransformer extends ResponseDefinitionTransformer {

    private static final String NAME = "id-transformer";

    @Override
    public ResponseDefinition transform(
        Request request,
        ResponseDefinition responseDefinition,
        FileSource files,
        Parameters parameters) {

        var id = request.queryParameter("id").firstValue();
        var value1 = //process id for getting value1
        var value2 = //process id for getting value2

        return ResponseDefinitionBuilder
            .withBodyFile("response.json")
            .withTransformerParameter("value1", value1)
            .withTransformerParameter("value2", value2)
            .build(); 
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public boolean applyGlobally() {
        return false;
    } 
}

但是wiremock返回一个json响应文件,其中包含相同的parameters.valuex占位符,并且没有插入值:

{
      "value1": "{{ parameters.value1 }}",
      "value2": "{{ parameters.value2 }}"
}

如何正确传递变压器参数?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题