apimocker icon indicating copy to clipboard operation
apimocker copied to clipboard

Is it possible to negate value in switchResponses part of config.json?

Open notbear opened this issue 10 years ago • 5 comments

Let's assume the code:

"switch": ["name", "age"],
"responses": {
  "post": {"mockFile": "default.json"}
},
"switchResponses": {
  "nameJohnage[NOT]20": {"mockFile": "John.json"},
  "nameJohnage20": {"mockFile": "John20.json"}
}

I expect that providing parameters name=John and age=99 would give a response from anyOtherJohn.json

I also thought about skipping the second parameter:

"switch": ["name", "age"],
"responses": {
  "post": {"mockFile": "default.json"}
},
"switchResponses": {
  "nameJohn": {"mockFile": "John.json"},
  "nameJohnage20": {"mockFile": "John20.json"}
}

But, by the documentation apimocker will look for the filename that matches ALL the request parameters. If one does not match, then the base file will be returned. So I guess I would get default.json instead of John.json for parameters name=John and age=99. And I'm not sure what I would get for name=John and age=20

Please advice.

notbear avatar Oct 05 '15 14:10 notbear

Hi, are you working on a mock responses for a JSON POST request? It looks like that's what you're doing, so you should look at JSONPath. There's some info on the README, and more details here: https://www.npmjs.com/package/JSONPath You should be able to do something like this: '$..[?(@.age!=20)]' @priyagampa added the JSONPath support, and may be able to help you. Does that work for you?

gstroup avatar Oct 05 '15 19:10 gstroup

Yes, I'm working on JSON files, the examples above were simplified for better readibility. I'll check JSONPath solution that you provided and will let you know if it works. Thanks,

notbear avatar Oct 07 '15 13:10 notbear

any luck with the JSON path solution?

gstroup avatar Oct 16 '15 22:10 gstroup

Sorry for not updating. I had no chance to try your suggestion yet.

notbear avatar Oct 18 '15 09:10 notbear

Hi, I failed with that. I'm not sure about the proper syntax. See my (failing) approach below. It's reduced to one parameter for clarity reason.

"switch": "$..details.name",
"responses": {
  "post": {"mockFile": "default.json"}
},
"switchResponses": {
  "$..details.nameJohn": {"mockFile": "John.json"},
  "$..details[?(@.name!=$['John'])]": {"mockFile": "whoever.json"}
}

notbear avatar Oct 20 '15 07:10 notbear