Testing HTTP Requests with Postman
Tags: postman, testing, apitesting, api
Published: 2023-02-15 06:20:00

## Introduction

In [Postman Premier - The Minimal Postman you need to Learn](/posts/2022/postman-premier-the-minimal-postman-you-need-to-learn/), I posted the minimum of Postman you need to learn. In this article, I'm going to expand on that knowledge and dig more into the response. In this post, I'll be asserting the response on certain values and response codes and more. 

## What are assertions and how to use it

Assertions are nothing but test cases we give to a particular request. In the previous post, we already have seen that each of the requests has a **Tests** tab. There we write code in JavaScript, judging specific parameters of the response.

Let's first test for a specific status code. Just for context, while testing, I'm going to use my gingo server.

## Looking for a specific status code

Testing for a specific status code is one of the easiest tasks to do within Postman. I'm going to use my `GET {{baseUrl}}/books` endpoint for this purpose.

When you open the **Tests** tab, you'll see snippets on the right side column. Click on **Status code: Code is 200**.

The code area will be populated with the following:

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

When you send the request, you'll see **Test Results (1/1)** under the response section. That area shows how many of your tests have passed or failed.

I've recorded a gif for you.

<img src="https://d2kbulfv42d43j.cloudfront.net/2022/Q4/postman-looking-for-specific-status-code.gif" caption="Testing for specific status code" alt="Testing for specific status code" position="center" />

This was for the positive assertions, you can send wrong data and do a negative assertion as well.

## Looking for a specific string in the body

For this test, I'll be using the `GET {{baseUrl}}/books/:isbn` endpoint, with `isbn`'s value to be `9781612680194`.

Looking for a specific string in the body is not much different from checking for a status code. You'll find a snippet for it as well. Snippets are a great way to explore assertion in Postman.

Look for an assertion called **Response body: Contains string**. It will spit this source code into the test panel.

```javascript
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
```

I'm going to modify the body:

```javascript
pm.test("Body includes Rich Dad Poor Dad", function () {
    pm.expect(pm.response.text()).to.include("Rich Dad Poor Dad");
});
```

Now I'm not going to include a gif for this as this would be very identical to the previous one.

## Looking for a specific header

Another thing we can test in a REST API is headers. Some use cases to look for a header are:

1. When you are expecting a specific content type
2. When you are expecting an authentication token as a response to a request.

For a header, the testing code would look like this:

```javascript
pm.test("Content-Type is application/json", function () {
    pm.response.to.have.header("Content-Type", "application/json");
});
```

## Adding tests at the collection level

Response time is another thing to test if your API is time critical. This time we don't add this test on a request basis. We are programmers and we DRY. Checking for responses is going to be for each and every request, it's better to add it at the collection level.

To add a test at the collection level, click on the collection name on the left panel. As already discussed, you'd see **Authorization**, **Pre-request Script**, **Tests** and **Variables**.

Navigate to **Tests**, and put this in the code area:

```javascript
pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});
```

500ms is good enough for local testing. If your server is over the internet, you might need to modify it to suit your need.

This test will run for every request which is there in the collection and for each new request, you add to the collection.

## Run all tests from a collection

When you click on a collection, you'll see a Run button at the top right corner. When you click that, you'll be asked which tests you want to run.

This is the Collection Runner UI. When you are done with all the config. Click on the **Run Postman Premier** to run all the tests and see the results.

## Conclusion

We have learned enough Postman to get our wheels rolling. There's much more to Postman than we have discussed in 2 articles on Fullstack with Santosh. Lots of features keep getting added in each release. You can keep yourself updated by following [Postman Docs](https://learning.postman.com/docs/getting-started/introduction/).