对同一个补丁请求端点使用存根多重响应

using stubby multiple responses for the same patch request endpoint

这是我在data.yaml

中的补丁请求映射
request:
    url: ^/api/test
    method: PATCH
    headers:
      Content-Type: application/json 

  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/test-1.json

api 路径 api/test 是一个 PATCH 请求,它在其主体中接受单个请求参数 {testVar: "1111"}

我需要实现的是 当请求参数为 {testVar: "1111"} -> 调用 response/test-1.json 当请求参数为 {testVar: "2222"} -> 调用 response/test-2.json

如何实现?

我尝试了查询参数、请求参数等,但没有成功

阅读:

  1. https://stubby4j.com/docs/http_endpoint_configuration_howto.html#dynamic-token-replacement-in-stubbed-response。更具体的是以下部分在哪里指定模板
  2. 此外,请查看以下 YAML,它是我的功能测试套件的一部分:https://github.com/azagniotov/stubby4j/blob/master/src/functional-test/resources/yaml/include-regex-dynamic-tokens-templated-stubs.yaml#L13

这里的想法是:

在您的 POST/PATCH 请求负载中,您可以将其中一个参数指定为正则表达式,例如:{"testVar": "(.*)"}。匹配时正则表达式的标记(即:(.*) 的值)可用作 response 配置的替换标记。换句话说,您应该能够根据需要加载相应的 JSON 文件。

但是,为了使这对您来说更容易一些,请尝试以下 YAML 配置:

- request:
    method: PATCH
    url: ^/api/test
    headers:
      content-type: application/json
    post: >
      {"testVar": "(.*)"}

  response:
    headers:
      content-type: application/json
    status: 200
    file: response/test-<% post.1 %>.json

让我知道以上是否适合您。作为参考,我在以下 PR 中测试了上述配置:https://github.com/azagniotov/stubby4j/pull/280