Limbo
Limbo

Reputation: 663

queryStringParameters in mock-server

I have the following url:

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

As you can see there are several parameters that are formed by two names, like "productGUIDs%5B0%5D%5Bproduct%5D=GLOBAL" and this is equal to "productGUIDs[0][product]=GLOBAL"

now in the expectation file on the mock-server I am trying to create the request but without success until now.

this is what I wrote in the expectation file:

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"],
  }
},

when sending the request (GET) with POSTMAN, I get 404, means, the mock-server does not recognize the request.

any advice of how to write the query string parameters in the expectation file will be really appreaciated

Upvotes: 1

Views: 10101

Answers (2)

carlosraya10
carlosraya10

Reputation: 1

Here an example of an expectation in a file.yaml for a POST request with queryStringParameters. To adapt it to the GET method just delete the body and change "POST" by "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

The indexation is very important in the .yaml file, so be careful to have the good format for each element, otherwise it won't work.

You can find here the documentation to do expectations : https://www.mock-server.com/mock_server/creating_expectations.html#button_match_request_by_negative_priority

Maybe you'll find your answer in the example "match request by path parameter and query parameter" in the link above.

Upvotes: 0

Jimothy
Jimothy

Reputation: 9740

The correct queryStringParameters syntax is an array of objects with name/values keys, like this:

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

Upvotes: 3

Related Questions