我正在尝试使用wiremock JSON存根文件模拟查询参数。
当我这样做时它会起作用:
{
"request": {
"method": "GET",
"url": "/posts?id=1",
},
//...
}
但是,当我将查询参数更改为使用这样的专用字段时,它将不再起作用:
{
"request": {
"method": "GET",
"urlPath": "/posts",
"queryParameters": {
"id": {
"equalTo": "1"
}
}
},
//...
}
知道为什么吗?
测试请求看起来像http://some-host/posts?id=1
答案 0 :(得分:2)
您可以尝试使用 urlPathPattern
代替 urlPath
。
here urlPath表示完全匹配,而urlPathPattern表示 regex
。
因此,在QueryParameter中使用 urlPathPattern
可以使查询得到解决。
{
"request": {
"method": "GET",
"urlPathPattern": "/posts",
"queryParameters": {
"id": {
"equalTo": "1"
}
}
},
//...
}
尝试并理解以下Wiremock的概念。
答案 1 :(得分:0)
这对我有用,将您的"urlPath"
更改为"urlPathPattern"
,但在构造此JSON
时要小心。因此urlPath
是完全匹配的模式,而urlPathPattern
是路径和查询参数上的正则表达式匹配
{
"request": {
"urlPathPattern": "/posts",
"method": "GET",
"queryParameters": {
"id": {
"equalTo": "1"
}
}
},
"response": {
"status": 200,
"body":"This is successful"
}
}
答案 2 :(得分:0)
问题在于urlPath
与queryParameters
不兼容,这只是预期的行为。 :-/我在Wiremock Github存储库中发现了this Q&A on the topic。根据{{3}}的回答,这是预期的行为,如果您使用queryParameters
,则urlPathPattern
将匹配。