Warm tip: This article is reproduced from serverfault.com, please click

mocking-动态wiremock捕获路径参数并返回响应

(mocking - Dynamic wiremock to capture path parameter and return in response)

发布于 2020-12-17 05:01:26

我正在尝试使用WireMock创建动态模拟。我有一种情况,如果我指定类似的网址

http://localhost:8989/api/account/121

那么我应该收到这样的响应:

   "cycleSpecification": {
       "id": "121"
        }
  }

简而言之,路径参数在响应主体中返回,但是我不确定如何捕获121并使用Wiremock在响应中返回它。

对于这种要求

/myManagement/v1/source?filters=myParty.id==539&&myParty.role==individual

或者

/myManagement/v1/source?filters=myParty.id%3D%3D539%26myParty.role%3D%individual

我可以使用什么使响应将过滤出ID和角色并放入响应中。

我正在使用独立的Wiremock jar 2.27.2创建Wiremock服务器。

Questioner
sagar verma
Viewed
0
agoff 2020-12-19 02:21:25

你可以通过使用响应模板来实现。你需要添加--global-response-templating如何启动独立服务器,然后才能使用响应模板。

我想你想要类似的东西:

{
  "request" : {
    "urlPathPattern" : "/api/account/.*",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "body" : "{ \"cycleSpecification\": { \"id\": \"{{request.path.[2]}}\" } }"
  }
}

查看有关请求模型的文档以获取更多信息

对于第二个问题,如果查询参数是作为单独的查询参数发送的,则可以在请求模型中引用它们。

{
  "request" : {
    "urlPathPattern" : "/api/account/.*",
    "method" : "GET",
    "queryParameters": {
        "myParty.id" : {
            "matches": ".*"
        },
        "myParty.role": {
            "matches": ".*"
        }
    }
  },
  "response" : {
    "status" : 200,
    "body" : "{ \"cycleSpecification\": { \"id\": \"{{request.query.party.id}}\" } }"
  }
}

不幸的是,由于看起来好像你的查询参数不是单独发送的,所以我认为你需要创建一个转换器来修改你的响应。