queryStringParameters in mock-server



我有以下网址:

http://10.11.100.163:1080/v1/publish/?category=conf&product=UFED_INFIELD&serial=123&productGUIDs%5B0%5D%5Bproduct%5D=GLOBAL&productGUIDs%5B0%5D%5Bguid%5D=undefinedblabla&productGUIDs%5B1%5D%5Bproduct%5D=UFED_INFIELD&productGUIDs%5B1%5D%5Bguid%5D=undefinedblabla

如您所见,有几个参数由两个名称组成,例如"productGUIDs%5B0%5D%5Bproduct%5D=GLOBAL",这等于"productGUIDs[0][product]=GLOBAL">

现在在模拟服务器上的期望文件中,我正在尝试创建请求,但直到现在都没有成功。

这是我在期望文件中写的:

await mockServerClient(process.env.mockServerAddress , process.env.mockServerPort).setDefaultHeaders(defaultResponseHeaders, []).mockAnyResponse({
"httpRequest":{
"method": "GET",
"path": "/v1/publish",
"queryStringParameters": {
"category":["conf"],
"product":["UFED_INFIELD"],
"serial":["123"],
"productGUIDs%5B0%5D%5Bproduct%5D" : ["GLOBAL"],
"productGUIDs%5B0%5D%5Bguid%5D" : ["undefinedblabla"],
"productGUIDs%5B1%5D%5Bproduct%5D" : ["UFED_INFIELD"],
"productGUIDs%5B1%5D%5Bguid%5D" : ["undefinedblabla"],
}
},

使用 POSTMAN 发送请求 (GET( 时,我得到 404,这意味着模拟服务器无法识别请求。

有关如何在期望文件中编写查询字符串参数的任何建议都将得到真正的理解

正确的queryStringParameters语法是具有name/values键的对象数组,如下所示:

"queryStringParameters": [
{
"name": "category",
"values": ["conf"]
},
{
"name": "product",
"values": ["UFED_INFIELD"]
},
{
"name": "productGUIDs%5B0%5D%5Bproduct%5D",
"values": ["GLOBAL"]
}
]

下面是一个 file.yaml 中对带有 queryStringParameters 的 POST 请求的期望示例。要使其适应GET方法,只需删除正文并将"POST"更改为"GET":

times:
remainingTimes: 1
unlimited: false
httpRequest:
method: POST
path: /accounts/login
queryStringParameters:
urlparameter1: 'value1'
body:
type: PARAMETERS
parameters:
username: myusername
password: mypassword
httpResponse:
body: {
"access_token": "e55etg9c-167e-4841-b0r3-y8fe5d1t7568",
"token_type": "bearer",
"redirectUrl": "/menu/clothes"
}
statusCode: 200
reasonPhrase: OK

索引在 .yaml 文件中非常重要,因此请注意每个元素的格式良好,否则它将不起作用。

你可以在这里找到文档做期望: https://www.mock-server.com/mock_server/creating_expectations.html#button_match_request_by_negative_priority

也许您会在上面链接中的">按路径参数和查询参数匹配请求"示例中找到答案。

最新更新