javascript - 对同一个补丁请求端点使用 stubby 多个响应
问题描述
这是我在 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"} -> 请求参数为 {testVar: "2222"} 时调用 response/test-1.json -> 调用 response/test-2.json
如何实施?
我尝试了查询参数、请求参数等,但没有运气
解决方案
阅读:
- https://stubby4j.com/docs/http_endpoint_configuration_howto.html#dynamic-token-replacement-in-stubbed-response。更具体地说,以下部分在哪里指定模板。
- 另外,请查看以下 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
推荐阅读
- batch-file - 如何生成 00 和 FF 之间但没有相等字符组合的随机值?
- azure - 是否可以通过 Az Cmdlet 以编程方式路由 Slot 流量?
- python - 消除 CSV 文件中不需要的换行符
- django - pip 找不到满足要求的版本 django==2.2.1
- javascript - 是否有任何等效于 C# 中 JavaScript 的 decodeURIComponent() 的方法?
- wordpress - 按订单获取客户自定义元数据
- reactjs - 从 Ant Design Table 获取当前表数据
- whatsapp - whatsapp 业务 API 中的 v1 是什么?
- reactjs - Grafana 示例 Workshop-panel 不工作 - TypeError: a.ReactPanelPlugin is not a constructor
- kubernetes - 当 vault 部署在专用集群上时,如何从另一个 kubernetes 集群访问 vault secret?