【问题标题】:Dynamically return a file based on query argument根据查询参数动态返回文件
【发布时间】:2019-01-24 17:22:47
【问题描述】:

我在我的映射目录中定义了这个存根。我正在尝试根据我的查询参数的值动态返回一个文件(查询参数和文件名将具有相同的名称)

请看下面的例子。

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/some/url/locales/en_GB/products",
    "queryParameters": {
      "typeName": {
        "matches": "([A-Za-z]*)"
      }
    }
  },
  "response": {
    "status": 200,
    "bodyFileName" : "{{request.pathSegments.[5]}}",
    "transformers": [
      "response-template"
    ]
  }
}

GET 请求看起来像

.../some/url/locales/en_GB/products?typeName=blah

在我的 __files 目录中,我将有一个名为 blah.json 的文件,我希望响应匹配该文件。

我不确定bodyFileName 是否正确。 5 代表查询参数的定位,零索引。

【问题讨论】:

    标签: wiremock


    【解决方案1】:

    弄清楚不同的响应模板变量代表什么值并不困难。以下是g的示例

    {
      "request": {
        "method": "GET",
        "urlPathPattern": "/some/url/locales/en_GB/products",
        "queryParameters": {
          "typeName": {
            "matches": "([A-Za-z]*)"
          }
        }
      },
      "response": {
        "status": 200,
        "body" : "request.requestline.pathSegments:\n[0]: {{request.requestLine.pathSegments.[0]}}\n[1]: {{request.requestLine.pathSegments.[1]}}\n[2]: {{request.requestLine.pathSegments.[2]}}\n[3]: {{request.requestLine.pathSegments.[3]}}\n[4]: {{request.requestLine.pathSegments.[4]}}\n[5]: {{request.requestLine.pathSegments.[5]}}\n\nrequest.query.typeName: {{request.query.typeName}}",
        "transformers": [
          "response-template"
        ]
      }
    }
    

    一个 GET 请求 http://localhost:8080/some/url/locales/en_GB/products?typeName=test 会产生这个响应:

    request.requestline.pathSegments:
    [0]: some
    [1]: url
    [2]: locales
    [3]: en_GB
    [4]: products
    [5]: 
    
    request.query.typeName: test
    

    考虑到上述内容,您可以看到您搜索的值不是使用request.pathSegments[x] 检索的,而是使用request.query.typeName 检索的。这会导致:

    {
      "request": {
        "method": "GET",
        "urlPathPattern": "/some/url/locales/en_GB/products",
        "queryParameters": {
          "typeName": {
            "matches": "([A-Za-z]*)"
          }
        }
      },
      "response": {
        "status": 200,
        "bodyFileName": "{{request.query.typeName}}.json",
        "transformers": [
          "response-template"
        ]
      }
    }
    

    【讨论】:

    • 非常感谢您。不幸的是,仍然没有运气使用 request.query.typeName。不确定它是否相关,但我在 docker 容器内运行wiremock。这是我从浏览器调用wiremock 服务时看到的异常。 java.lang.RuntimeException: java.io.FileNotFoundException: /home/wiremock/./__files/{{request.query.typeName}}.json(没有这样的文件或目录)
    • 您是否将 response-template 转换器应用于该 WireMock 规则?对我来说,它似乎正在寻找字符串:./__files/{{request.query.typeName}}.json,并且没有用您在 URL 中提供的任何内容替换 {{request.query.typeName}}
    • 感谢 A.Kootstra。与文档有点混淆。我以为我通过在响应中传递它来启用变压器,如上所示。但它实际上需要作为 docker-compose.yml 中的命令参数传递给容器。一旦通过 --global-response-templating 它工作正常。感谢您的帮助。
    • 图像:rodolpheche/wiremock 命令:-“--verbose”-“--global-response-template”端口:-“8090:8080”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2015-11-26
    • 2012-02-08
    相关资源
    最近更新 更多