如何在wiremock中使用json创建动态响应

问题描述 投票:0回答:1
{   "result": {     {{#each (jsonPath request.body '$.ids') as |id|}}"{{id}}": {       "favorite_0": 1,       "search_0": 1,       "visit_0": 1,       "order_0": 1,       "basket_0": 1,       "jfyimpression_0": 1     }{{#unless @last}}     ,{{/unless}}{{/each}}   },   "messages": [     {       "type": "success",       "position": "top",       "content": "Request processed successfully.",       "title": "All is well!",       "code": "SUCCESS_1001"     }   ] }

我创建此响应是为了动态复制每个 ID 的结果。 ID 列表可以有一项或多项。取决于 ids 结果 > ids 会改变。

我尝试了这个 json 但没有帮助,如何创建 json 使其动态

{
  "mappings": [
    {
      "request": {
        "method": "POST",
        "url": "/api/v1/feature-store/features",
        "bodyPatterns": [
          {
            "matchesJsonPath": "$.feature_name",
            "equalTo": "customer-product_clickstream_activities_precomputed"
          },
          {
            "matchesJsonPath": "$.identifier",
            "equalTo": "123972627"
          },
          {
            "matchesJsonPath": "$.features",
            "contains": "visit_0"
          },
          {
            "matchesJsonPath": "$.features",
            "contains": "search_0"
          },
          {
            "matchesJsonPath": "$.features",
            "contains": "favorite_0"
          },
          {
            "matchesJsonPath": "$.ids",
            "contains": "\\d{9}"
          }
        ]
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "jsonBody": {
          "result": "{{{#each (jsonPath request.body ',$.ids') as |id index|}}  {{id}}: {\n    \"favorite_{{index}}\": 1,\n    \"search_{{index}}\": 1,\n    \"visit_{{index}}\": 1,\n    \"order_{{index}}\": 1,\n    \"basket_{{index}}\": 1,\n    \"jfyimpression_{{index}}\": 1\n  }{{#unless @last}},{{/unless}}{{/each}}}",
          "messages": [
            {
              "type": "success",
              "position": "top",
              "content": "Request processed successfully.",
              "title": "All is well!",
              "code": "SUCCESS_1001"
            }
          ]
        }
      }
    }
  ]
}
wiremock wiremock-standalone wiremock-record
1个回答
0
投票

我设置了一个简单的映射来帮助找出响应模板。这是我正在使用的映射:

{
  "name": "ids",
  "request": {
    "url": "/api/v1/feature-store/features",
    "method": "POST"
  },
  "response": {
    "status": 200,
    "bodyFileName": "features.txt",
    "headers": {
      "Content-Type": "application/json"
    },
    "transformers": [
      "response-template"
    ]
  }
}

此文件位于我的

mappings
文件夹中,其中有几个要点。首先,主体存储在一个名为
features.txt
的单独文件中,该文件保存在
__files
目录中。第二件事是它启用了响应模板转换器。

features.txt
文件如下所示:

{
  "result": {     
  {{#each (jsonPath request.body '$.ids') as |id index|}}
    "{{id}}": {
      "favorite_{{index}}": 1, 
      "search_{{index}}": 1, 
      "visit_{{index}}": 1, 
      "order_{{index}}": 1, 
      "basket_{{index}}": 1, 
      "jfyimpression_{{index}}": 1     
    }{{#unless @last}}, {{/unless}}
  {{/each}}
}, 
"messages": [
    {
      "type": "success", 
      "position": "top", 
      "content": "Request processed successfully.", 
      "title": "All is well!", 
      "code": "SUCCESS_1001"
    }
  ]
}

它循环遍历请求中的

ids
并相应地更新响应。有这样的要求:

{
  "ids": [1,2]
}

它将产生如下响应:

{
  "result": {
    "1": {
      "favorite_0": 1,
      "search_0": 1,
      "visit_0": 1,
      "order_0": 1,
      "basket_0": 1,
      "jfyimpression_0": 1
    },
    "2": {
      "favorite_1": 1,
      "search_1": 1,
      "visit_1": 1,
      "order_1": 1,
      "basket_1": 1,
      "jfyimpression_1": 1
    }
  },
  "messages": [
    {
      "type": "success",
      "position": "top",
      "content": "Request processed successfully.",
      "title": "All is well!",
      "code": "SUCCESS_1001"
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.