Simple and powerful HTTP API tests
Tags: restapi, api, testing
Published: 2023-01-03 14:33:40

---
title: Simple and powerful HTTP API tests
published: true
description: 
tags: restapi, api, testing
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-01-03 14:06 +0000
---

I want to show you how to write and run concise, readable and powerful HTTP API tests using [WebTau](https://github.com/testingisdocumenting/webtau) tool.

## First Test

We are going to test a Todo API. [JSON Placeholder](https://jsonplaceholder.typicode.com) website is going to help us with this.

`GET https://jsonplaceholder.typicode.com/todos/1`
```json
{
  "userId": 1,
  "id": 1,
  "title": --> "delectus aut autem", <--
  "completed": false
}
```

Our first test will send GET request to `https://jsonplaceholder.typicode.com/todos/1` and assert on the `title` response field

`scenarios/apitest.groovy`
```groovy
scenario("check todo item title") {
    http.get("https://jsonplaceholder.typicode.com/todos/1") {//1
        title.should == "delectus aut autem"//2
    }
}
```
1. GET call to a provided URL (full URL is not required)
2. automatic mapping of the JSON response

To run the test, we will use [WebTau](https://github.com/testingisdocumenting/webtau) command line tool. One way to install it is to use [brew](https://brew.sh).
Other options available in documentation [Installation](https://testingisdocumenting.org/webtau/getting-started/installation#groovy-runner) section.

```cli
$ brew install testingisdocumenting/brew/webtau
$ webtau scenarios/apitest.groovy
```

Let's take a look at the output produced by the test run

![webtau console output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewom5ou5m5en2j3khd07.png)

Few things to note:
* WebTau produces detailed log of what happens
* Automatic assertion on statusCode when no explicit assertion provided
* Highlighting asserted fields in the response

## Base URL

In the example above we use the full URL to the TODO item resource. To be able to run the test against different environments (e.g. localhost)
we should extract base URL.

There are multiple ways to specify base URL, most commons are:
* Command line parameter
* Config file

```groovy
scenario("check todo item title") {
    http.get("/todos/1") {
        title.should == "delectus aut autem"
    }
}
```

To set base URL via command line we need to pass `--url` parameter to the CLI call

```cli
$ webtau scenarios/apitest.groovy --url https://jsonplaceholder.typicode.com
```

To set base URL using a config file use 

`webtau.cfg.groovy`
```groovy
url = "https://jsonplaceholder.typicode.com"
```

# Extracting Data

Next, let me show you how to use data from one response to initiate the next one. 
We will create a TODO item and then extract its id for further usage.

`POST https://jsonplaceholder.typicode.com/posts`
```json
{
  "userId": 1,
  "id":  --> 1 <--,
  "title": "delectus aut autem",
  "completed": false
}
```

```groovy
scenario("create and update todo item") {
    //1
    def itemId = http.post("/posts", [title: "my todo item", body: "write test", userId: 1]) {
        return id //2
    }

    http.get("/todos/${itemId}") { //3
        id.should == itemId
    }
}
```
1. create new item
2. extract auto parsed response field
3. use extracted id as part of URL

## Reporting

I already show you console output `WebTau` produces. Let me finish this post with a screenshot of an HTML report that `WebTau` generates.

![webtau web report](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrj36yknz6p2qlw51rkx.png)

Generated report is a self-contained single HTML file that you can Slack around. Report captures a lot of details and provides permalink behavior. 
E.g. open a specific test and specific HTML call and then share the link. 

# Outro 

I demoed basic functionality of testing HTTP based API. Should be enough for you to start writing your tests.

There are so many other things you can do with WebTau and HTTP API, Browser, CLI, DataBase and Business Logic.