Testing of a RESTful API
Tags: testing, codequality
Published: 2020-10-09 07:59:57

This is a follow-up to the article RESTful API quick tutorial that went trough basic REST-topics.

There is a lot of tutorials online that describe how to manually set up tests for your API. With Humlix, you can either import, e.g., an OpenAPI specification or use the request builder to set up your API. Humlix will then automatically generate and execute tests for you.

We will use Humlix to test the GitLab Groups API. We will use a Docker-container from Gitlab: gitlab/gitlab-ce:13.1.0-ce.0.

#Set up the API within Humlix
You need to download Humlix. Remember to sign up for the Humlix mailing list to stay up to date with the latest news. You can follow the Humlix getting started guide if you don't know how to run it.

Select the HOME menu and click on ADD SUITE and create a test suite called *GitLab Suite*.

![GitLab Suite](https://blog.humlix.com/content/images/2020/10/gitlab-suite.png)

Press the Add request link and create a new test request called Get a group list by page.

![GitLab test request](https://blog.humlix.com/content/images/2020/10/gitlab-request.png)

Go to the *ENVIRONMENT VARIABLES* tab and change the environment name to GitLab Environment. Add two variables base_url and gitlab_token. Set the values according to your setup. Make sure to use your token for GitLab.

![GitLab Environment](https://blog.humlix.com/content/images/2020/10/gitlab-environment.png)

Go to the *REQUEST tab* so that you can see the Humlix test request builder. Select *GET* verb and *Http* protocol and add base_url/api/v4/groups. Add a new query parameter page of type integer according to the picture below.

![Request builder](https://blog.humlix.com/content/images/2020/10/gitlab-request-builder.png)

You can run a test with the example data you entered by pressing the Run single test button. This will verify that you can connect to the GitLab service trough the API endpoint that you built.

![Single test button](https://blog.humlix.com/content/images/2020/10/single-test-button.png)

If everything works as expected, you should receive something like this in the log area.

![Log view](https://blog.humlix.com/content/images/2020/10/one-off-run.png)

The result above shows that our setup works and that your Gitlab API token is correct.

> Disclaimer: The next step will generate many requests targeting the selected environment. Make sure that it is a test- or dev-environment that you can easily re-create if needed. Don't run this against a production environment.
Press the Generate tests button to see some real magic. This will trigger Humlix test generation, and during that time, you will see a progress indicator at the top of the user interface. When it is done, you should be able to see something similar to the image below.

![Test result](https://blog.humlix.com/content/images/2020/10/1048-errors-1.png)

Experiment with the test request settings, if you don't get the same result. E.g., you can change the Max number of generated test calls to 1000 and rerun the test generation.

![Test settings](https://blog.humlix.com/content/images/2020/10/settings.png)

#Looking for the cause
We can see the cause of the problem by going into the production.log file for GitLab and see that during the execution of some SQL statement, the error was:

> 2020-10-02_19:43:52.85686 ERROR:  bigint out of range
2020-10-02_19:43:52.85689 STATEMENT:  SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = 'Group' ORDER BY "namespaces"."name" ASC, "namespaces"."id" ASC LIMIT 20 OFFSET 9223372036854775820

We have found a bug in the production code!
We can also verify the bug using this cURL command:

> curl -I localhost/api/v4/groups?page=461168601842738792 
HTTP/1.1 500 Internal Server Error

#Wrap up
Now you have seen how easy it is to let Humlix create tests for you. Download and try it out if you haven't already! Make sure to subscribe to our newsletter, so you don't miss coming articles and updates around testing Web APIs.