API request libraries

Introduction: 

Today every application relies on APIs. That is why it is crucial to thoroughly verify APIs before rolling out the product because it is one of the layers of the testing pyramid. API testing can be done in multiple ways using different open-source frameworks, tools & libraries for API testing.  

In this blog, we are covering API testing libraries with their HTTP response and comparison between them.  

HTTP relies on concepts like resources and Uniform Resource identifiers (URIs), simple message structure, and client communication flow. HTTP has their response code like (200- ok, 404- not found) and methods like Get, Put, Post, Patch, etc.  

In this blog, we are covering three libraries – Supertest, Axios, and Request.  

 Axios SuperTest Request 
   What is it? It is a JavaScript library used to make HTTP requests from Node.js or XML http Requests from the browser.  It is a Super-agent driven library for testing Node.js HTTP servers using a fluent API. Simplified HTTP request client. 
 Latest Version  0.27.2, MIT  6.2.4, MIT  2.88.2, Apache 2 (deprecated) 
 Dependencies  Follow-redirects, From-data   Methods, superagent  From-data, extend 
DevDependencies   Mocha, Typescript, karma-chrome-launcher Express, mocha, should Json-stringify-safe, qs 
 frameworks  Mocha, jest, jasmine  Mocha, jest, jasmine  mocha 
 CI/CD Integration  Circle CI, Jenkins  Circle CI  Circle CI 
 Testing of XML & Json-based web services   Yes   Yes   Yes 
 SOAP APIs   possible  Possible after sending XML file as a post request  possible 
 REST APIs  Yes  Yes  Yes 
 GraphQL APIs  Yes  Yes  Yes 
 library  JavaScript  JavaScript  JavaScript 
  Alternatives  Node-cache, methods  Truffle, puppeteer  Postman-request, fetch-to-curl 
 useability  Quite tricky to use  Fluent API  Simple to use API, fast & reliable  
 promise  Promised based  Promised based  Not promised-based 
vulnerabilities Incorrect comparison in Axios, Denial of service in Axios, Server-side request forgery in Axios No vulnerabilities  Remote memory exposure on request 
 Community support  Maximus users & high support  good  Low support 
 CRUD operations  Yes  Yes  Yes 
Global request setup  Yes  Yes   Yes 

Axios:

To know the working with Axios library go to: https://www.neovasolutions.com/2022/04/19/api-automation-testing-using-axios-with-javascript/ 

Pros:

  • It is isomorphic which means it can run on both the browser and NodeJS with the same codebase. 
  • Axios can be easily installed therefore providing an easy-to-use API in a compact package for most of your HTTP communication needs.  
  • Due to the large volume of users, it provides high community support.  
  • Axios is lightweight and based on Promise API.  
  • Error handling is quite easy.  

Cons:

  • It is not native JavaScript, to use the Axios library, we must install it and later import it to our project using npm/yarn.  
  • In the case of small Projects, with just a few simple API calls Axios is not a feasible/ideal solution, instead fetch can be used.  

Supertest:

To know the working with the Supertest library go to: https://www.neovasolutions.com/2022/04/12/api-automation-testing-using-request-with-javascript/ 

Pros:

  • It has a fluent API.  
  • It makes HTTP assertions easy.
  • It can integrate nicely with other testing frameworks.  
  • SuperTest is that it can run tests without any additional tools.  
  • Supertest is excellent for implementing test assertions for HTTP API systems for both REST and GraphQL-based backend middleware.  
  • Can be mixed with different testing suites like Chai.js and Mocha.  
  • Old-style callbacks are also supported. 

Cons:

  • It does not work on browsers. 
  • Poor Documentation. 

Request:

To know the working with the Request library go to: https://www.neovasolutions.com/2022/04/05/api-automation-testing-using-supertest-with-javascript/ 

Pros:

  • For finding bugs at an early stage of software development, API testing using the Request JS module is an efficient tool for fast and reliable performance.  
  • Can be used with different testing frameworks like Chai and Mocha.  
  • We can easily stream the response to the file stream.  
  • It provides custom HTTP headers.  

Cons:

  • The API is currently depreciated and for that reason, no future updates are possible.  
  • The community support for this API is low compared to other testing tools. 

A quick comparison:

This comparison is based on the last 1-year downloads of these libraries and stats:

API testing libraries trend
API testing libraries stats

Conclusion:

We compared the most popular HTTP libraries in this blog. Now the question arises which one should I use?  So, the choice depends on the project’s requirements, and we can select from this comparison which is perfect for our requirements.