Mock Server in Postman
Introduction
Postman's mock server feature revolutionizes API testing by simulating responses from backend services. Learn how to create and utilize mock servers to iterate rapidly and validate designs in isolation from actual endpoints.
What is Mock Server ?
A mock server is a server that is not a real server. It is just a fake server that is simulated to work as a real server so that we can test our APIs and check the response or errors.
Why Use Mock Servers?
Early Testing: Mock Servers enable developers to test their applications early in the development process, even before the actual API is implemented. This accelerates the development cycle.
Parallel Development: Backend and frontend teams can work in parallel. While the backend team is developing the actual API, the frontend team can start building and testing against the Mock Server.
Isolation Testing: Developers can isolate and test specific scenarios, error responses, or edge cases without relying on the real API, making it easier to ensure robust error handling.
Collaboration: Mock Servers facilitate collaboration among team members. They provide a shared environment where developers can collaborate on APIs without waiting for all components to be ready.
Creating a Mock Server
1.Creating a mock from scratch
Select Mock Servers in the sidebar, then select +.
Select an existing collection, or create a new collection and add an initial request.
Configure your mock server details.
2.Creating a mock from a collection
Select Collections in the sidebar. Select the more actions icon
next to the collection you want to mock and select Mock collection.
Configure your mock server details.
3.Creating a mock from history
You can build a mock server based on requests from your Postman history. Select History in the sidebar, then select the more actions icon
next to a request and select Mock Request. (You can also mock all the requests for particular date.)
Configure your mock server details.
Configuring mock server details
To configure your new mock server, give it a name. You can select an optional environment to have your mock sever use environment variables. You can also configure a delay before the mock server sends responses to simulate network delays.
If you choose to make your mock server private, you need to add a Postman API key in the request header when sending requests to the mock server: x-api-key:<Your-Postman-API-key>
. If you share the collection, others can use their Postman API keys to make calls to the mock server.
Not all configuration options will be available, depending on the method you used to create the mock server.
After you finish selecting configuration options, select Create Mock Server. Postman displays the details you need to use the mock server. (You can get these details at any time by selecting Mock Servers in the sidebar and selecting the mock server.)
Select Copy Mock URL to begin making requests to your mock server.
To delete a mock server, select Mock Servers in the sidebar. Select the more actions icon next to the mock server's name and select Delete.
Editing the mock server configuration
You can change the configuration for a mock server at any time. Select Mock Servers in the sidebar, select a mock server, and select
Edit Configuration.
You can change the mock server's name, environment, network delay, and privacy setting. You can also specify options for response matching. When you are done changing the configuration settings, select Update Mock Server.
Making requests to mock servers
Use the mock server's URL to make calls to the mock server. Select Mock Servers in the sidebar, select a mock server, and select Copy Mock URL.
Make sure the request you want to mock has at least one saved example. You can send a request and save the response as an example. You can also define a custom example. Learn more about adding examples to a request.
The mock URL includes the mock server's ID and the path for the request you want to mock. Select Send to send the request to the mock server.
When you send a request to the mock server, the mock server sends back a response based on an example with the same path and method. You can provide multiple examples, and Postman will return the one that best matches your request.
If you configured a delay for your mock server, Postman waits the specified period of time before sending the response.
Conclusion
In conclusion, a mock server is a valuable tool in the realm of API development and testing. By providing a simulated environment for testing APIs, it streamlines the development process, accelerates feedback loops, and facilitates efficient collaboration between developers and QA engineers. Embrace the power of mock servers in your workflow to enhance efficiency, minimize bugs, and ultimately deliver robust APIs to your users.