What’s up Guys! Welcome to automationcalling.com

dashboard_page

In this post, we are going to take a look at what is “Unirest” and how to perform with simple REST API automation examples using Unirest Java Library and what are all additional capabilities when compare with Rest Assured. In this blog, I may not compare deep dive between Rest Assured and Unirest.

In terms of HTTP request related testing for eg., REST API, etc., there are a number of tools in the market considering License/Open Source in the category of Standalone, Browser extension/plugin, library, etc., for eg., SoapUI, Ready API, Rest Assured, Postman, Rest Client and many other but we may still look better options considering in the following ways:

  • Learn and easy implementation by Beginners level.
  • Simple/less and Clean code.
  • Multi-language support.
  • Lightweight.
  • Adaptability and Flexibility.
  • Integration with CI.
  • Implement Test Code in any language part of the Development Source code.
  • Encourage Developers to write Integration Test/End to End Test code.
  • Sync/Async Request

To meet above criteria, I suggest you to try with Unirest Library

What is Unirest?

Unirest is Open Source HTTP lightweight client Library, which is come under MIT opensource license from Mashape. The beauty of Unirest is support 8 different programming language (Please refer the above image).

Dependencies

Just Add the maven dependencies, that’s it all set to go :-).

<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.1.00</version>
</dependency>

Implementations

In this blog, I’m experimenting with Java language but you are advised free to explore in other languages.

The great stuff about Unirequest is simple usage in reusable methods, which helps to achieve clean code.

simple_usage

Simple Request

All you have to do is, create a java class, add @Test annotation (either Junit or TestNG), implement UniRest Methods. Please take a look on below examples

Unirest_Methods

When you run the above program, the result is shown below in the pretty print format

response_string

The sample example with JSONsimple_response_JSONNode

In above example, I use JsonNOde Object in HttpResponse and return asJson() which execute and return result in non pretty print format. To get pretty format, you should add the method “.toPrettyString()” after “getbody()”.

  • Unirest is a class which holds static methods to initialize test configuration for invoking type of request for eg., GET, PUT, POST, DELETE etc.,

Query Params:

Query Param supports Single value or collections which is quite easy to add multi values.

queryparam

queryparam_1

Response Validation:

As far as response validation concern, there is no third-party library support available to navigate JSON or XML nodes, instead we can use JSON Object/JSON Array or XML Object to validate the test.

Response_Validation

Other than above, there is a lot of cool features in Unirest, which are missing in Rest Assured:

  • Upload Progress Monitoring.
  • Asynch Request Support.
  • Paged Request (To verify all required page is retrieved in rest response).
  • If you don’t expect a body back, asEmpty() is verification for eg., Delete Request.
  • Handling Large Responses.
  • Configurable socketTimeout, connectTimeout and Concurrency methods available.
  • Possible to bring up Metrics during runtime code.

Conclusion:

Unirest library has been addressed with a lot of Cool features for http request. The great advantage of this library is supporting multi-languages and capabilities of inbuild and configurable methods to handle runtime code, Async request, error handling etc., The few areas I miss in Unirest, which are available in Rest Assured like DSL, easy response validation (using JSON, XML Path, GPath (Using Groovy)), support file type in request body, etc.,

I strongly recommend this library for test automation as I see it’s growing and they regularly maintain source code and fixes. I believe, in future other than JSONObject/JSON Array validation they may come up with common response object validation like Groovy Path to address both JSON and XML.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s