Setting up Mock Servers in Postman: A Step-by-Step Guide
Step-by-step instructions for setting up mock servers in Postman
Photo by Rubaitul Azad on Unsplash
Click on the "Mock Servers" button in the left sidebar, then click the "Create Mock Server" button.
Fill in the 'Request URL' for the API you will be testing. The API architecture or development team can provide this information. You can change this later, so feel free to enter any value you believe is appropriate.
a. For now, let's assume that the response code returned will be 200 OK. This can be changed later.
b. The response body can be left empty, as we will add this later.
c. The request method will depend on the API being tested, but it can also be changed later. Click "Next." In this example, we will examine a mock API that returns a shipping fee based on the shipping method selected by the user and the cart amount. I have named this mock server "Shipping Fee," but you can name it according to your needs. The rest of the settings can be left as default for now.
You will see a mock server created with the specified specifications. The URL provided below is the URL that will be used to call this mock API. Later, this will be replaced by the actual API URL when it becomes available for testing in the testing environment.
Click on 'Collections' in the sidebar. A collection should have been automatically generated with the same name as the mock server. This collection will contain one mock example. Within this example, you can define the request payload, the corresponding response payload, and the response code. I have included additional examples for further exploration, but let's first examine the initial example. The left panel displays the expected request payload, while the right panel presents the anticipated response code and response body. If the chosen shipping method is standard shipping, the cart amount is less than $35, and the customer possesses an active membership, no shipping fee will be applied. In essence, we are instructing the server to return the response on the right side when it receives the request payload on the left side.
In the second example, the left side illustrates the incoming request payload and the right side displays the predicted response code and response body. When the selected shipping method is standard shipping, the cart total is below $35, and the customer holds an active membership, a shipping fee of $6.99 will be imposed on the customer.
If you submit the payload in this example, the response will indicate a shipping fee of $6.99. It is essential to ensure that Postman automatically generates the response from a mock example based on the request payload.
Similarly, we can establish other examples according to our API guidelines. Great job! We have successfully set up the mock server. I hope this article proves helpful if you're facing difficulties.
See you soon in another blog post. Until then, stay connected, stay healthy, and keep learning. Thank you for visiting, and cheers!