Mock Server: Query Params drive responses

Archived topic — some details may be out of date.
Mock servers built from saved collection examples are still supported. Postman now also has code-based mocks: JavaScript request handlers plus a config.yaml, stored as files in your Git repository, which you can run locally or deploy. Create one in the app, or use the Postman CLI to generate one from an OpenAPI spec, collection or dataset (postman mock generate) and run it (postman mock run). See About mocks and Create a mock.
Working with an AI coding agent? The Postman plugin can build and run mocks for you.
For current help, see the Postman docs, try the free Postman tools (no account needed), or join us on Discord.

Hi:

Mock Server is a very powerful feature. It allows for the testing of APIs as they are being developed. It allows for exercising the APIs once they are developed.

In my case, what I actually want to do is simulate failures in a third party API I have zero control over. My primary goal while testing using Postman, is to avoid any code changes to be required for testing (eg: don’t introduce different headers or parameters because that requires code changes and would be an invalid test.) Data changes are fine.

My thinking is:

  1. The paths to be tested remain constant
  2. The parameters determine behavior.

For example: path is /w/get/someInfo

Keeping it simple, the call takes one parameter: ID

I can then set up several examples to simulate the conditions I want to test for. eg:
/w/get/someInfo?id=1 : returns 200, well formed data
/w/get/someInfo?id=2 : returns 200, badly formed data
/w/get/someInfo?id=3 : returns 404, no data //yes, this third party does that (!!!)
/w/get/someInfo?id=4 : returns 500, no data

What I’m proposing is that the first parameter is the discriminator. ie: Postman doesn’t have to have knowledge of the parameters passed, only that the first parameter is the one to match responses on.

If this is already possible, and I’m completely misunderstanding path matching please let me know.

If this is not clear, please let me know.

Thanks in advance.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.