简体   繁体   English

Stubby:对具有不同正文的相同 URL 的 PUT 请求进行存根

[英]Stubby: stub PUT requests for same URLS with distinct body

I'd like to match distinct bodies for the same PUT URL, but stubby4j always matches the first case, no matter what's the content of the body.我想为相同的 PUT URL 匹配不同的主体,但 stubby4j 总是匹配第一种情况,无论主体的内容是什么。

Example:例子:

- request:
    url: /individuals/.*/address$
    method: PUT
    body: >
      {
        "type": "MOBILE",
        (other input fields)  
      }
  response:
    status: 400
    body: >
      {
        "type": "BAD_REQUEST",
        "message": "Validation error on request."
      }
- request:
    url: /individuals/.*/address$
    method: PUT
    body: >
      {
        "type": "HOME",
        (other input fields)
      }
  response:
    status: 200

In this case, no matter what's the value of the parameter "type" in my request, it always matches with the first stub.在这种情况下,无论我的请求中参数“type”的值是什么,它总是与第一个存根匹配。

Try to change the request body to json data with headers.尝试将请求正文更改为带有标头的 json 数据。

- request:
    url: /individuals/.*/address$
    method: PUT
    headers:
       content-type: application/json
    json: '{"type": "MOBILE"}'
  response: ...

- request:
    url: /individuals/.*/address$
    method: PUT
    headers:
       content-type: application/json
    json: '{"type": "HOME"}'
  response: ...

Apologies for the late response.为迟到的回复道歉。

In the recent & current versions of stubby4j (ie: 7.xx ) it is possible to match distinct bodies for the same PUT / GET / POST / PATCH /etc URLs.在 stubby4j 的最新和当前版本(即: 7.xx )中,可以为相同的PUT / GET / POST / PATCH /etc URL 匹配不同的主体。

For example, the following is a valid YAML config that would work (I simplified a tad the YAML config provided by the OP for the sake of example):例如,以下是一个有效的 YAML 配置(为了举例,我简化了 OP 提供的 YAML 配置):

-  request:
      url: /individuals/.*/address$
      method: PUT
      post: >
        {"type": "MOBILE"}

   response:
      status: 400
      body: >
        {"type": "BAD_REQUEST"}


-  request:
      url: /individuals/.*/address$
      method: PUT
      post: >
        {"type": "HOME"}

   response:
      body: OK
      status: 200

To note:要注意:

  • The request does not get the body key, use post key instead for stubbing your payload for POST/PUT/PATCH (or file if your payload is too big) request没有获得body密钥,使用post密钥代替为 POST/PUT/PATCH 存根您的有效负载(如果您的有效负载太大,则使用file
  • The body key should only be used in response , not request body键应该只用于response ,而不是request
  • The json key is invalid & not supported by stubby4j json密钥无效且不受 stubby4j 支持

Please refer to the stubby4j user manual for more information about the YAML config for request & response : https://github.com/azagniotov/stubby4j#endpoint-configuration-howto有关requestresponse的 YAML 配置的更多信息,请参阅 stubby4j 用户手册: https : //github.com/azagniotov/stubby4j#endpoint-configuration-howto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM