NashTech Blog

Expert API Testing with Insomnia Advanced Features and Plugins

Table of Contents

Robust API testing is essential in today’s fast-paced software development environment to guarantee the functionality, dependability, and security of applications. Of all the tools out there, Insomnia is one of the most flexible platforms for API testing, with an extensive toolkit, an extensible foundation, and a thriving plugin ecosystem. This blog post will discuss how to use Insomnia’s testing toolkit, frameworks, libraries, and plugins to speed up your API testing.

UNDERSTANDING INSOMNIA

Insomnia is an open-source API testing tool that provides a user-friendly interface for designing, debugging, and testing APIs. It is appropriate for testing REST, GraphQL, and SOAP APIs since it supports a variety of HTTP methods, authentication protocols, and data types.  Testers and developers may build and execute queries, check responses, and automate workflows with ease because of its user-friendly interface.
Let’s break down the process of API testing using Insomnia by exploring various tools, frameworks, libraries, and plugins commonly used in this context. We’ll do this by providing code snippets and explanations for each aspect.

1. SETUP AND INSTALLATION OF INSOMNIA

Before diving into API testing with Insomnia, you need to ensure that Insomnia is installed on your system. You can download and install it from their official website or via package managers like npm or deb.

1.1 Install Insomnia via npm

npm install -g insomnia

1.2 Install Insomnia via apt

Step 1.  Add APT Source repository
echo "deb [trusted=yes arch=amd64] https://download.konghq.com/insomnia-ubuntu/ default all" \ | sudo tee -a /etc/apt/sources.list.d/insomnia.list

Step 2.  Install Insomnia
sudo apt update
sudo apt install insomnia

Step 3. Run Insomnia:

After installation, you can run Insomnia from the application menu or by typing insomnia in the terminal.

2. CREATING REQUESTS

Insomnia allows you to easily create HTTP requests for testing APIs.

2.1 Example: Creating a GET Request

  1. Open Insomnia and create a new workspace if needed.
  2. Click on “New Request”.
  3. Name your request (e.g., “Get User Info”) and select GET as the request type.
  4. Enter the URL (e.g., https://reqres.in/api/users?page=2).
  5. Add any necessary headers or query parameters.
  6. Click “Send”.
  7. View the response returned by the server.

insomnia api request

2.2 Example: Creating a POST Request with JSON Body

  1. Open Insomnia and create a new workspace if needed.
  2. Click on “New Request”.
  3. Name your request (e.g., “Create User”) and select POST as the request type.
  4. Enter the URL (e.g., https://reqres.in/api/users).
  5. Go to the “Headers” tab and add any necessary headers (e.g., Content-Type: application/json).
  6. Go to the “Body” tab, select JSON, and enter your JSON payload (e.g., {"name": "Kajal", "job": "Automation Tester"}).
  7. Click “Send”.
  8. View the response returned by the server.

3. TESTING TOOLBOX IN INSOMNIA

In Insomnia, the testing toolbox allows you to perform automated testing on your API requests. This can be very helpful for confirming that your APIs are accurate and ensuring they meet certain criteria or specifications. The testing tools in Insomnia can be used as follows:

3.1 Setting Up a Test

  1. Open Insomnia and Select a Request:
    • Open the Insomnia application.
    • Select the workspace and the specific request you want to test.
  2. Access the Test Tab:
    • With the request selected, navigate to the “Tests” tab. This tab allows you to write test scripts using JavaScript.
  3. Write Test Scripts:
    • Insomnia uses the Chai assertion library, so you can write your tests using assertions like expect, assert, and should.

3.2 Example Tests

Here are some common types of tests you might write:

3.2.1 Checking the Status Code

To verify that the response status code is what you expect (e.g., 200 for a successful GET request):

const response = await insomnia.send();
expect(response.status).to.equal(200);
3.2.2 Checking the Response Time

To ensure the API responds within an acceptable time frame:

const response = await insomnia.send();
expect(response.responseTime).to.be.below(200);
3.2.3 Validating Response Headers

To check if a specific header exists and has the correct value:

const response = await insomnia.send();
const contentTypeHeader = response.headers["Content-Type"];
expect(contentTypeHeader).to.include("application/json");
3.2.4 Validating the Response Body

To verify the structure and content of the response body:

const response = await insomnia.send();
const body = JSON.parse(response.data);
expect(body).to.have.property('name').that.is.a('string');
expect(body.name).to.equal('Kajal');
insomnia test cases
3.2.5 Running the Tests
  1. Send the Request:
    • Go back to the “Collection” tab and click “Send” to send the request and receive the response.
  2. View Test Results:
    • After sending the request, switch to the “Tests” tab to see the results of your test scripts.
    • The test results will indicate whether each test passed or failed, along with any error messages if a test failed.

insomnia test

4. FRAMEWORK AND LIBRARIES

While Insomnia itself is not a framework or library, you can integrate it with testing frameworks like mocha or libraries like chai for more advanced testing capabilities.

4.1 Testing Frameworks

4.1.1 Mocha

Mocha is a JavaScript test framework that runs on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting while mapping uncaught exceptions to the correct test cases.

Installation:
npm install mocha --save-dev
Example Usage:
const assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});

insomnia test cases

4.1.2 Jest

Jest is a delightful JavaScript testing framework with a focus on simplicity. It works with projects using TypeScript, Node.js, React, Angular etc.

Installation:
npm install jest --save-dev

Example Usage:

test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});

4.2 Assertion Libraries

4.2.1 Chai

Chai is a BDD / TDD assertion library for Node.js and the browser that can be paired with any JavaScript testing framework.

Installation:
npm install chai --save-dev
Example Usage:
const { expect } = require('chai');
describe('Array', function() {
it('should start empty', function() {
const arr = [];
expect(arr).to.be.an('array').that.is.empty;
});
});

5. Plugins

Insomnia supports plugins, which are add-ons that extend its functionality. Plugins can help automate tasks, integrate with other tools, and enhance the testing experience. Here’s a guide to using and managing plugins in Insomnia.

5.1 Installing Plugins

  1. Open Insomnia:
    • Launch the Insomnia application.
  2. Access Plugins:
    • Go to the application menu and select Preference and then choose Plugins.
  3. Search for Plugins:
    • In the dialog, you can install plugin by entering plugin in the input box.
  4. Install a Plugin:
    • Click the Install button next to it.
  5. Restart Insomnia:
    • After installing the plugin, you may need to restart Insomnia for the plugin to be fully integrated.

insomnia

5.2 Some Popular Plugins for Insomnia are:

5.2.1 insomnia-plugin-kong-declarative-config

This plugin allows you to export your Insomnia workspace to a Kong declarative configuration format.

Installation:
insomnia-plugin-kong-declarative-config
5.2.2 insomnia-plugin-env-generator

This plugin generates environment variables from a given schema, useful for managing complex environments.

Installation:
insomnia-plugin-env-generator
5.2.3 insomnia-plugin-query

This plugin provides a way to construct GraphQL queries more easily within Insomnia.

Installation:
insomnia-plugin-query
5.2.4 insomnia-plugin-encrypt

This plugin allows you to encrypt and decrypt sensitive data in your requests and responses

Installation:
insomnia-plugin-encrypt
5.2.3 insomnia-plugin-table

This plugin provides a visual table view for JSON responses, making it easier to read and analyze data.

Installation:
insomnia-plugin-table

6. HOW INSOMNIA ELEVATES API TESTING CAPABILITIES

Insomnia’s elevates API testing capabilities in several ways:

1. User-Friendly Interface:

It provides an intuitive and user-friendly interface for creating, organizing, and executing API requests. Its clean design makes it easy for both beginners and experienced users to navigate and use effectively.

2. Request Building:

It simplifies the process of building HTTP requests. You can easily create various types of requests (GET, POST, PUT, DELETE, etc.), add headers, query parameters, request bodies, and authentication credentials using its visual interface.

3. Dynamic Environments:

It supports dynamic environments, allowing you to define variables that can be used across requests. This feature is useful for testing APIs in different environments (e.g., development, staging, production) without having to modify individual requests.

4. Response Validation:

It allows you to validate API responses using JSON schema validation or custom validation rules. You can define validation rules to ensure that the response structure, data types, and values meet your expectations, helping to identify issues early in the development process.

5. Code Generation:

It can generate code snippets in various programming languages based on your requests. This feature is useful for quickly integrating API calls into your application code or test scripts without manual transcription errors.

6. Authentication Support:

It supports various authentication methods, including basic authentication, API keys, OAuth 2.0, and more. You can easily configure authentication settings for individual requests or use global authentication settings for all requests in a workspace.

7. Response Visualization:

It provides built-in support for visualizing API responses in various formats, including JSON, XML, HTML, and plain text. This makes it easy to inspect and understand the structure and content of API responses during testing.

8. Extensibility:

It is highly extensible through plugins, allowing you to customize and extend its functionality to suit your specific testing requirements. You can find plugins for generating documentation, mocking APIs, integrating with testing frameworks, and more.

7. CONCLUSION

By harnessing the power of Insomnia’s comprehensive toolbox, frameworks, libraries, and plugins, you can significantly elevate your API testing capabilities, streamline workflows, and enhance testing efficiency, ultimately revolutionizing your development process.

8. REFERENCES


https://docs.insomnia.rest/insomnia/get-started-with-documents

https://insomnia.rest/plugins

 

Picture of Kajal Keshri

Kajal Keshri

Leave a Comment

Suggested Article

Discover more from NashTech Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading