Nowadays, building an API driven app is one of the easiest ways to market your service in the long-run. This approach brings many benefits for both users and developers. Being able to share information and data without the need of a direct connection to the shared device permits users and programmers to design apps that could be accessed via different platforms. With the thousands of APIs already out there it gets more challenging every day to develop new software, optimize current known products and services, but also understand how others are designing their platforms. However, not everyone has time or money to invest in proper performance testing tools. Luckily this post will help you find authoritative open source performance testing tools for rest api.
The tools you have at your disposal for performance testing your REST API can make or break the success of the project. This is why it is important to choose the right tools for your specific needs, and to do so with a comprehensive understanding of the testing problem being solved.
REST API Testing – How to Do it Right
REST is a software architecture style, commonly used for web services. Due to its popularity, you will probably need to load test RESTful APIs at some point. API testing can be done with Apache JMeter™. Read this blog post to find out how.
REST and RESTful APIs – An Introduction
REST (Representational State Transfer) is a software design pattern typically used for web applications. We can think about REST as a software architecture style. The basic idea of REST is treating objects on the server-side (e.g rows in a database table) as resources that can be created or destroyed. When we say RESTful APIs we are referring to the web services implementation of REST architecture.
The most basic way of thinking about RESTful is as a way of formatting the URLs of your web applications. For example, if your resource was called “articles”, then:
- /articles – How a user accesses ALL posts.
- /articles/:id -How a user accesses and views an individual post which was retrieved based on their unique id.
- /articles/new How a form for creating a new post is displayed.
RESTful API defines a set of functions that developers can use to perform requests and receive responses via HTTP protocol, such as GET, POST, PUT and DELETE.
- Sending a GET request to /users would retrieve the users from the database level.
- Sending a POST request to /users would create a new user on the database level.
- Sending a PUT request to /users/:id would update the attributes of a given user, again identified by a unique id.
- Sending a DELETE request to /users/:id would delete a given user, again identified by a unique id.
How to Load Test RESTful APIs with JMeter
Load testing RESTful APIs can be easily done in JMeter with the HTTP Request Sampler.
Our working environment:
- Ubuntu 16.04
- JMeter 3.2
- RESTful API available at jsonplaceholder.typicode.com. It’s very simple, but it’s enough for our work and for practicing. If you need to load test you need to add your own server’s name.
1. Add a Thread Group
Right Click -> Add- > Thread Group
Here we defined “Number of Threads = 50” and “Loop Count = 5”. This allows us to simulate 50 different requests for 5 times. I named the Thread Group “REST Example”.
![Add a thread group. API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/API1-500x129.png)
2. Add a HTTP Request.
Right Click on REST Example -> Add -> Sampler -> HTTP Request.
![Add a HTTP Request. API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/API2-500x330.png)
3. Fill in the necessary values:
- Name – the name of the current sampler
- Protocol – by default this is HTTP, but it can also be HTTPS or FILE
- Server – the IP address or domain name of the server. This field is required
- Port – the web server the port listens to. By default: 80
- Method – GET, POST, PUT, DELETE, etc. This field is required
- Path – the path to the resource. If this requires string parameters in the query, add them below in the “Send Parameters With the Request” section
- Content encoding – if you are using POST, PUT, PATCH or FILE, there is a character encoding to be used
POST
We will post data as a JSON object. JSON, or JavaScript Object Notation, is a way to store information in an organized, easy-to-access manner. In a nutshell, JSON gives us a human-readable collection of data that we can access in a logical manner.
4. Set a Content-Type header.
Right click on Test Plan -> Add -> Config element -> HTTP Header Manager
5. Add a “Content-Type” field equal to “application/json”, as shown in the picture below:
![Add a “Content-Type” field equal to “application/json”, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/API3-500x105.png)
Important! Our RESTful API in this blog post is so simple that we do not need to send headers, and you can skip this step. But in other cases, this step is necessary.
5. Add a HTTP Request Sampler to our REST Example thread group.
Right click on REST Example -> Add -> Sampler -> HTTP Request
6. Fill in the relevant fields:
- Name – POST method
- Server – jsonplaceholder.typicode.com
- Method – POST
- Path – “/articles”
- Parameters-Post Body – add the body request
{ title: 'foo', body: 'blazemeter', userId: 1 }
You should now see this window:
![Add a HTTP Request Sampler to our REST Example thread group. API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api4-500x132.png)
7. Add a Listener to see the Request response.
Right Click on REST Example -> Add -> Listener -> View Results Tree
It will look like this:
![Add a Listener to see the Request response. API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api5-500x166.png)
GET
8. To check the GET method let’s create a new HTTP Request sampler and fill in following credentials:
- Name – GET method
- Server – jsonplaceholder.typicode.com
- Method – GET
- Path – “/articles”
![GET method, API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api6-500x142.png)
9. In the View Results Tree you can see all the posts received by the GET method:
![In the View Results Tree you can see all the posts received by the GET method. API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api7-500x252.png)
Response Assertions
10. If you need to verify that the post is presented in your received response, use the Response Assertion.
Right Click on GET method -> Add -> Assertions -> Response Assertion
11. Leave this window’s settings as default. You only need to configure the “Patterns to Test” field, like below, according to the response you want to test.
![response assertions, API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api8-500x163.png)
How does the Response Assertion work?
In responses, different posts have different ids, titles and bodies. The response assertion looks through the whole response and looks for matches between the patterns you wrote down and what the response contains. If there is a match, the test will pass. If not – test will not pass.
In this example I’ve taken the title of a post at our server. It is equal to “qui est esse”. Since there is a match, the test passes.
![Response Assertion, API testing, jmeter, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api9-500x253.png)
But, if we change our pattern to “blazemeter title”:
![response assertion api load testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api10-500x166.png)
Then you can see, that our GET method tests doesn’t pass.
![assertion failure message API testing, rest api testing, jmeter api testing](https://obiztools.com/wp-content/uploads/2022/01/api11-500x135.png)
The Assertion failure message: Test failed: text expected to contain /blazemeter title/
That’s it! You have now learned all the necessary information to create your own RESTful API tests. To learn more about performance testing with JMeter check out this free 5 day online JMeter training course.
To learn about load testing with BlazeMeter, which enhances JMeter abilities, request a demo or put your URL or JMX file in the box below and your test will start in just minutes.
<?php include path_to_theme().”/templates/_test_wizard_url_widget.tpl.php” ?>
CONCLUSION
Before we dig into the details of each testing tool, lets take a step back and look at Performance Testing in general. Performance testing is defined by Miragliotta as “the process of validating the performance characteristics of any given solution”. In other words, it is used to verify that your solutions works according to its requirements, without compromising on its performance.
In my previous article, I explained what performance testing is, and why we need to test our web applications. I also approximately discussed the kind of challenges a developer faces during performance testing process.