【问题标题】:stubby4node comma separated query string implementation issuestubby4node 逗号分隔的查询字符串实现问题
【发布时间】:2022-01-21 19:14:40
【问题描述】:

我有一个以下模式的 URL (GET REQUEST)

  • ^/testpath/1/test?pathid=1
  • ^/testpath/1/test?pathid=1,2
  • ^/testpath/1/test?pathid=1,2,5

其中 pathid 查询字符串参数以逗号分隔

我有以下粗短的映射来匹配这些 url 模式

- request:
    url: ^/testpath/(.*)/test
    query:
      pathid: '1'
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-1.json

- request:
    url: ^/testpath/(.*)/test
    query:
      pathid: '1,2'
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-2.json

- request:
    url: ^/testpath/(.*)/test
    query:
      pathid: '1,2,5'
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-3.json

但我无法让此 URL 映射根据不同的参数组合正确传递不同的有效负载。

  • 1 -> 有效载荷1
  • 1,2 -> 有效载荷2
  • 1,2,5 -> 有效载荷3

如何做到这一点?

【问题讨论】:

  • 您可能希望在您的正则表达式末尾结束 $,否则它们可能会产生意想不到的结果...
  • 也试过了,但没有成功。

标签: javascript node.js stubbing stubby4j stubby4node


【解决方案1】:

这里的技巧是改变 yaml 文件中请求/响应映射的顺序。

- request:
    url: ^/testpath/(.*)/test
    query:
      **pathid: '1,2,5'**
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-3.json

- request:
    url: ^/testpath/(.*)/test
    query:
      **pathid: '1,2'**
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-2.json

- request:
    url: ^/testpath/(.*)/test
    query:
      **pathid: '1'**
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-1.json

注意按降序添加的路径ID,以便首先执行更深的匹配。

通过这种方式,我能够实现以下要求,即传递以逗号分隔的不同路径 ID

  • 1 -> 有效载荷1
  • 1,2 -> 有效载荷2
  • 1,2,5 -> 有效载荷3

【讨论】:

    【解决方案2】:

    @Sanath,简短回答 - 是的,stubby4j 匹配不同的存根查询参数组合。

    几个问题:

    1. 您运行的是哪个版本的 stubby4j?
    2. 另外,您是将 stubby4j 作为独立 JAR 还是作为 pre-built stubby4j Docker containers 之一运行?

    我编写了一个测试来验证您在问题中提供的 YAML 配置:https://github.com/azagniotov/stubby4j/pull/434/files(请注意,我确实更改了一点 url,但它仍然是一个类似于您发布的 YAML 的正则表达式)。

    测试发出的请求与存根匹配。 (我确实尝试过没有$$,就像@code 建议的那样)。此外,我还针对正在运行的独立 JAR 使用 cURL 进行了测试:

    curl -X GET  http://localhost:8882/stackoverflow/70417269/1/test?pathid=1,2,5
    

    curl -X GET  http://localhost:8882/stackoverflow/70417269/1/test?pathid=1,2
    

    再次,我从服务器得到了预期的响应。

    如果以上内容对您没有帮助,请随时提出错误报告https://github.com/azagniotov/stubby4j/issues/new/choose

    【讨论】:

    • 我使用的是短版本“5.0.0”并将其用作独立的 npm 参考(在 Web 应用程序中)
    • 我正在使用 stubby4node npmjs.com/package/stubby - 最新的是我升级到的 5.1,但仍然是同样的问题
    • @Sanath 我明白了,谢谢你分享这个,我想现在我对正在发生的事情有了更好的了解。只是为了可见性和清晰性:stubby4j.com(a.k.a.L stubby4j)实际上是一个基于 Java 的存根服务器,它是一个单独的库。您使用的是它对应的基于 NodeJS 的 github.com/mrak/stubby4node,我不维护它。这两个库的功能很可能随着时间的推移而相互分歧。换句话说,在 stubby4j 中可能工作和支持的东西可能在 NodeJS stubby 中不受支持。
    • @Sanath 显然有人在 25 天前在 stubby4node 中提出了一个问题,与您遇到“查询参数匹配问题”github.com/mrak/stubby4node/issues/92 的行为相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 2017-12-24
    • 1970-01-01
    相关资源
    最近更新 更多