Wiremock 独立更改日期

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

我正在使用 docker compose 中的 Wiremock

wiremock:
    image: "wiremock/wiremock:latest"
    ports:
        - "8095:8080"
    container_name: wiremock
    volumes:
        - ./wiremock/extensions:/var/wiremock/extensions
        - ./wiremock/__files:/home/wiremock/__files
        - ./wiremock/mappings:/home/wiremock/mappings
    entrypoint: ["/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose"]

我有一个包含日期的请求,我想在响应中返回接下来的 3 小时

我已经可以在 json 响应中返回 3 个节点的列表,但我找不到计算接下来 3 小时的方法。这可行吗?

请求

/nexthours?deliveryDateUtc=2024-05-20T10:00:00Z

预期回应

{
   "next3hours":[
      {"position":"1","timestamp":"2024-05-20T10:00:00Z"},
      {"position":"2","timestamp":"2024-05-20T11:00:00Z",
      {"position":"3","timestamp":"2024-05-20T12:00:00Z"
   ]
}

映射文件夹中的文件 (nexthours.json)

{
  "request": {
    "method": "GET",
    "urlPath": "/nexthours"
  },
  "response": {
    "status": 200, 
    "headers": {
      "Content-Type": "application/json"
    },
    "bodyFileName": "nexthourstemplate.json",
    "transformers": ["response-template"]
  }
}

__files 中的文件 (nexthourstemplate.json)

{
  "next3hours": [
    {{#each (range 1 3)}}
    {
      "position": {{this}},
    "timestamp": "{{request.query.deliveryDateUtc}}"
    }{{#unless @last}},{{/unless}}
    {{/each}}
  ]
}

这给了我

{
   "next3hours":[
      {"position":"1","timestamp":"2024-05-20T10:00:00Z"},
      {"position":"2","timestamp":"2024-05-20T10:00:00Z",
      {"position":"3","timestamp":"2024-05-20T10:00:00Z"
   ]
}

但我没有找到计算时间戳值的方法。这可行吗?

wiremock wiremock-standalone
1个回答
0
投票

这里的技巧是解析日期并提供一个

offset
。 偏移量可以是
3 days
1 years
之类的东西,但在这种情况下我们可以使用
hours
偏移量。

对于您的示例,我们需要将偏移量设置为循环范围的基础。 这是您的

nexthourstemplate.json
的更新版本:

{
  "next3hours": [
    {{#each (range 1 3)}}
    {{#assign 'currentOffset'}}{{this}} hours{{/assign}}
    {
      "position": {{this}},
      "timestamp": "{{date (parseDate request.query.deliveryDateUtc) offset=(lookup currentOffset) }}"
    }{{#unless @last}},{{/unless}}
    {{/each}}
  ]
}

如您所见,我们要做的第一件事是构造偏移量并将其分配给变量

currentOffset
。 然后,我们使用
date
帮助器和
parseDate
帮助器使用我们生成的 offest 来解析作为查询参数传递的日期:

{{date (parseDate request.query.deliveryDateUtc) offset=(lookup currentOffset) }}

使用您的原始请求

/nexthours?deliveryDateUtc=2024-05-20T10:00:00Z
这应该返回以下 json:

{
  "next3hours": [
    {
      "position": 1,
      "timestamp": "2024-05-20T11:00:00Z"
    },
    {
      "position": 2,
      "timestamp": "2024-05-20T12:00:00Z"
    },
    {
      "position": 3,
      "timestamp": "2024-05-20T13:00:00Z"
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.