如何从 Wiremock 响应正文发送带有正文的模拟 Webhook

问题描述 投票:0回答:1

我尝试创建一个模拟 webhook 请求,该请求将在调用请求

/transform
时触发 在 webhook 请求正文中将从响应正文 id 中获取 id。我怎样才能做到这一点?

{
  "request": {
    "method": "POST",
    "urlPath": "/transform"
  },
  "response": {
    "status": 200,
    "body": "{\"message\": \"success\", \"id\": \"{{randomValue length=10 type='ALPHANUMERIC'}}\"}",
    "headers": {
      "Content-Type": "application/json"
    }
  },
  "serveEventListeners": [
    {
      "name": "webhook",
      "parameters": {
        "method": "POST",
        "url": "https://webhook.site/9c32fadb-b542-4edf-a530-85a012064566",
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\"id\": \"{{jsonPath response.body '$.id'}}\"}"
      }
    }
  ]
}

我尝试使用 body-transformer,但仍然无法从响应中获取 id 值

"transformers":[  
            "body-transformer"
         ]
wiremock wiremock-standalone
1个回答
0
投票

此对话是在 Wiremock 社区 slack 上进行的 - https://wiremock-community.slack.com/archives/C03NAEH5LVA/p1705654286090779

为了完整性,此处复制了答案:

响应对象在 Webhooks 模板系统中不可用。从查看文档看来,您只能在 webhook 有效负载的模板中使用原始请求:

{
  "request": {
    "method": "POST",
    "urlPath": "/webhooks"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "message": "success",
      "transactionId": "{{jsonPath request.body '$.id'}}"
    },
    "headers": {
      "Content-Type": "application/json"
    },
    "transformers": ["response-template"]
  },
  "serveEventListeners": [
    {
      "name": "webhook",
      "parameters": {
        "method": "POST",
        "url": "http://host.docker.internal:7070/callback-logger/api/callback",
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\"systemId\": \"{{jsonPath originalRequest.body '$.id'}}\"}"
      }
    }
  ]
}

请求正文:

{
  "id": "1234567890"
}

目前模拟您正在寻找的内容的唯一方法是将值作为请求正文中的值(如上所述)或作为请求发送 测试中的标题。这样,该值将在响应和 Webhook 中可用。

这并不理想,我们确实提出了一个问题来解决这个问题 - https://github.com/wiremock/wiremock/issues/2359

© www.soinside.com 2019 - 2024. All rights reserved.