Mock Server in POSTMAN

A Mock Server is a virtual server that can be your own computer or any software which can be used to act like a real server. A Mock server is used during different API testing when various responses have to be received which are not readily available on the web servers or for some testing that cannot be performed on real servers for security issues.

POSTMAN provides a great facility letting you create a Mock Server inside it.

Below are some necessity for creating Mock Servers:

  • Any kind of request can be made into the server without any authentication.
  • Troubleshooting during API testing becomes much faster and easier.
  • Some critical APIs can be developed and checked which is not possible through real servers.
  • It helps to create a proto-type for APIs.
  • While developing the User Interface, of the website, one must be to some extent aware of the size and the property of the response that will be provided by the real server, after the request is made. In this case, the Mock server is the only way to make it possible.
  • The list of uses and advantages of using the Mock server will go on.

Creating the MOCK Server in POSTMAN

For creating Mock Server in POSTMAN, you don't really need any external extensions or applications, but the Mock server is inbuilt in POSTMAN. Let us see how to use it.

Go to the New button in POSTMAN and you will see the option for Mock Server.

postman-mock-server-option

Select the Mock Server.

postman-mock-server-set-up

As can you see in the above image, the first column is for the method type. (We selected GET). In the next column, the Request Path has to be inserted. ({{url}}/demo). The third column is for the response code(which is 200, means OK). The fourth is the Response body, this is where we can write our response.

We can write the response text as plain or HTML document, JSON document, etc.

Now hit the Next button.

postman-mock-server-second-step

Then click into the Create Mock Server, and your Mock server is created.

postman-mock-server-final-step

Now, on the left side of the POSTMAN interface, your Mock Collection should be displayed along with all other existing collections.

postman-mock-server-default-request

As shown in the above request, you can see that a request has been automatically created by POSTMAN for making requests into the Mock server.

Now, open the request and click into the send button to send the request.

Now, go the Response Section, and you must be able to see the custom response that you created at the time of creating the Mock Server.

postman-mock-server-response

Creating a JSON Response in the Mock Server

So far we have only seen how to create a response in the Mock server and how a request can be made to get the response. Now, we shall see how to create a JSON response and how a request can be made to get the response from the Mock Server.

Let us create a JSON response into the Mock Server. To edit the response, just open the request and then go to the Example in the top right corner of the POSTMAN page.

postman-mock-server-edit-response

As you click Examples, you will get a drop-down option called Default. Click into that option and you will be able to edit your response.

postman-example-default-option

As you click into Default option, you will get an area to edit your response. Click into the drop-down menu right to the Preview section in the body section and then select JSON(you can select anyone, but now we are concerned about creating JSON response, so we shall choose JSON response).

postman-response-json

After selecting the JSON file, in the body section, write your JSON Response.

{
  "data": [
    {
      "type": "articles",
      "id": "1",
      "attributes": {
        "title": "Learning mock server in postman",
        "body": "Tutorial in Chercher.tech for mock server in POSTMAN"
      },
      "relationships": {
        "author": {
          "data": {
            "id": "42",
            "type": "people"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "people",
      "id": "2",
      "attributes": {
        "name": "Rahul"
      }
    }
  ]
}

Now, hit the save example button and then click to return back.

postman-mock-server-json-response

In the image, the left side button demo, you can return back to the request.

Now, send the request. And wait for some time. Even though the response is made from your local machine through POSTMAN itself, it will take some time to process the request and the response.

Response from Mock Server :

postman-json-mock-response

Performing Assertion into Mock Server's Response

As we saw earlier, in the Assertion Chapter, that assertions are nothing but writing test code. The POSTMAN inbuilt code snippets can be used the javascript code can be customized for making different assertions on the Mock Server's Response.

Performing Assertions on the Mock Server's Response is similar to the assertions we make on the Response that we obtain from a distant or remote server(what we call a real server).

Checking the HTTP status code

Go to the Test Tab of the Request and edit the code below:

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Click the send button to send the request again and after the response is received, to the Result tab in the Response section and see the result.

postman-response-status-code

As you can see in the above image, the script for the status code is PASS.

Accessing data from the JSON Response

Any data values from the JSON Response can be obtained by accessing the JSON path for that variable. For example, consider the response below.

{
  "data": [
    {
      "type": "articles",
      "id": "1",
      "attributes": {
        "title": "Learning mock server in postman",
        "body": "Tutorial in Chercher.tech for mock server in POSTMAN"
      },
      "relationships": {
        "author": {
          "data": {
            "id": "2",
            "type": "people"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "people",
      "id": "2",
      "attributes": {
        "name": "Rahul"
      }
    }
  ]
}

Suppose, I have to access the value inside the title, so the access path should be data[0].attrubutes.title.

Edit the following code into the Test tab editor:

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    var mydata = jsonData.data[0].attributes.title;
    console.log(mydata);
});

Now, click the send button to send the request and after the response is received, see the Result.

postman-json-access-data

Now, suppose we want to access the last attribute data, that is name. So, the JSON path for that value will be included[0].attributes.name;. Now edit the following code into the Test Tab:

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    var mydata = jsonData.included[0].attributes.name;
    console.log(mydata);
});

Now, send the request and see the Result:

postman-data-access-in-json

Advanced assertions on the response of a Mock server

Advanced assertions can also be performed on the response from the Mock server. We have already see what are the different types of programming concepts that can be applied to creating assertions. Some of them were conditional statements, some were loops and switch statements.

Assertion using Conditional Statement:

Generally, the conditional statement is a if. When a condition is satisfied or is found to be true, then the if statement gets executed. In this example, we shall that how can we check the existence of a name inside the response. This can be checked by comparing the JSON path of the data with the value we want to check.

Let, the name be Rahul. Edit the following code into the Tests Tab:

pm.test("Your test name", function() {
 var jsonData = pm.response.json();
 var mydata = jsonData.included[0].attributes.name;
 if (mydata == "Rahul")
  console.log("The name is found");
 else
  console.log("The name is not found");
});

In the above example, the line if(mydata == "Rahul") checks the condition for the presence of the name Rahul in the given path.

Console Output :

postman-if-console

Checking for data in two different path

It may be possible, that a data value if not found in one path, then can be found in a different path of the JSON response. To check a value in two different places, either we can use two of the statements or can use an OR operator inside the if statement.

Let us check for the name Rahul in one path and the id =1 in a different path. If anyone (name or the id) is matched, then At least one condition is satisfied will be printed in the console, otherwise, None of the condition is satisfied will be printed in the console.

Now, edit the following code into the Tests Tab of the request:

pm.test("Comparing name and id", function () {
    var jsonData = pm.response.json();
    var mydata = jsonData.included[0].attributes.name;
    var mydata01 = jsonData.data[0].id;
    if(mydata=="Rahul"||mydata01==1)
    {
    console.log("Atleast one condition is satisfied");
    }
    else
    {
    console.log("No of the condition is satisfied");
    }
});

In the above program, you can see that, there are two different JSON paths:

  • For the name = Rahul: jsonData.included[0].attributes.name.
  • For the id = 1: jsonData.data[0].id.
Before sending the request, you are advised to clear the console, so that the new logs can be easily understandable.

Now, send the request and after the response is received, see the console output:

postman-dual-condition-check

Using code snippets on Mock server response

As discussed earlier, code snippets are nothing but ready-made codes that are integrated into POSTMAN to create assertions, check status code, search for a keyword, setting or getting variables.

Code snippets help in creating scripts easily and also the time to write code for a simple test can be reduced significantly. Let us see the applications of some code snippets.

To go to POSTMAN snippets, go to the right side of the interface and select the code snippet you want and it will be automatically pasted into the editor.

postman-test-snippets

You may use code snippets into the Pre-test script or you can use the code snippets in the Tests(which runs after the response is received).

Some of the snippets which are used in Pre-Test scripts are setting variables, getting variables, etc. On the other hand, some of the snippets which are used in the Tests(which gets executed after the response are received) are: checking status code, performing assertions, implementing other filters, etc.

Che
cking the status code:

To check the status code, Select Tests Tab of the request, and once the editor is opened, the snippets must be visible to you.

Click into the snippet: Status code is 200. And the code for the test will be automatically pasted into the editor:

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

postman-snippet-for-status-code

Now, send the request and wait for the response. Once the response is received, go to the Result tab of the Response and see the result:

postman-status-code-check-snippet

Searching for a string in the body

There are functions in POSTMAN for searching a certain keyword or string in the body of the response. Use the code snippet: Response body Contains string. Once you click into the code snippet, the code must be visible into the Tests editor.

I shall search for the string Chercher.tech. Edit the code below:

pm.test("Searching for the string", function () {
    pm.expect(pm.response.text()).to.include("Chercher.tech");
});

postman-string-check-snippet

Now, send the request and check the result after the response is received.

postman-string-check

Matching Status Code

When a response is received, there can be different status codes associated with the response. For example, when a response is received from the server without any interruption or without any error, the status code turns out to be 200.(which means OK).

Now, it is also possible that the server returns some different codes like 201, 202, etc, which are somewhat similar to the Ok status code: 200. Now, let us see how we can check for different status code:

In this example, I shall check for the status code: 200, 201, 202.

Edit the following code into the Tests tab of the request :

pm.test("Matching status code", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202,200]);
});

In the above code, we have checked for the code- 201, 202 and 200 using the statement:

pm.expect(pm.response.code).to.be.oneOf([201,202,200]);

Now, send the request and see the result tab after the response is received from the server:

postman-check-multiple-response-code

As you can see in the above response, its showing PASS.

Check for the Response Time

Response time is the time taken by the server to send the response. All the time the server will not be able to send the response with similar efficiency or speed. And the response time decides how good the website is.

Use the code snippets to see the response time or edit the code below:

pm.test("Checking for the response time less than 200 ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

As shown in the above code, we are checking for the response time to be less than 200 mili-seconds. After it is done, send the request and wait for the response. If the response from the server, has any of the three status codes, the test will PASS.

postman-response-time

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions