Testing Nodejs Application with Postman

By in Community Stories April 8, 2019

Postman is becoming a standard nowadays for testing APIs. It’s one of the must-have tools for many developers, automation testers and its currently used by millions.

Postman has some awesome features listed below.

1. Design & Mock
Creates API specs directly with the postman collection format. We can also utilize the mock service to work with data

2. Debug
This helps in debugging & examining our APIs before making them production ready

3. Test Automation
Postman helps to run the automated testing scripts for all the requests created into our collection

4. Documentation
Postman can create viewable documents for all your code snippets and tests in various languages

5. API monitoring
Continuously monitor the critical API

6. Publishing
Developers can easily learn your services with postman’s API documentation

In this article, we will test the rest of the APIs of our application which we developed in the last blog  “Handling Authentication with Nodejs

Installation

Kindly refer to the official website for installing Postman.You may see the Postman GUI as below:

Import postman collection of our application

  1. GitHub Link to our collection – Nodejs app. Download it and save it on the desktop
  2. Click on import
  3. Choose the file to import.

To understand the requests created for our collection, we will need to understand more about HTTP methods.

HyperText Transfer Protocol (HTTP) is a stateless protocol, which means that the client and server know how to handle the data for that instance only. Once the browser initiates the request and sends it to the server, the server will send a response back to the client. On every request initialized, a new connection is established between client and server. The widely-used HTTP verbs are GET, POST, PUT, PATCH, DELETE

GET: It is used to retrieve all the information from the server using the URI and does not modify any kind of data

POST: It is used to send data to the server

PATCH: It is used to update and modify the resource partially

PUT: It is used to replace the resource entirely, unlike the PATCH method

DELETE: This will delete the resource

Now, let’s start testing our application with the postman.

You will see all the rest of the APIs of our collection on the left.

Setting the environment variables

This is an optional step, as you may not need to set environment variables if you have already imported the collection in your postman instance.

The steps below are to be followed to set up the environment

  1. Goto settings
  2. Click on add
  3. Insert the environment name, variable, and the current value of the variable

You will see we have also set the authtoken variable here. This is the token which is received from the server after logging in into our app. To automate the token and to generate it automatically without manually inserting it every time we log-in, we will insert the below code to the “Tests” tab

if(pm.response.code === 200 ){
    pm.environment.set('authtoken', pm.response.json().token)
}

After the response is received from the server, pm.response object will receive the token and we will set the environment token “authtoken” variable with that value.

Let us quickly test some of the endpoints of our app

1. Create user API
Select the body tab and insert a user with name, age, email and password with raw data in JSON format. This API sends a request to the server to generate a new user

2. Login user API
Select the body tab and insert a user with email and password with raw data in JSON format. This API will allow users to log in to our application.

As you can see, we are successfully logged into our application. Token received from the server will get set into “authtoken” variable

3. Read your own profile
/user/me endpoint allows users to see their own profile.

4. Let’s create a post with /posts endpoint

In our Authorization tab, we have set the type as “Inherit auth from parent”. This will allow only the authenticated users to post the request, otherwise, it will show an error as “Please authenticate”

We have successfully created a new post

Conclusion

The main purpose of this blog is to make you aware of the POSTMAN for testing APIs. I will leave the testing of all other endpoints up to you. I hope you had a seamless experience in testing the application. Also, leave your responses in the forum and reach out to me if you face any issues.