Skip to main content

HTTP Plugin

ktestify-plugin-http is a first-party KTestify plugin that adds a synchronous HTTP transport to your test scenarios. It lets you send HTTP requests and assert on the response status, body, and headers, including polling until an expected status is returned.

The plugin implements the RequestResponseClient<Req, V> contract from ktestify-core. Unlike the Kafka and Azure Blob transports, which block until a record appears, the HTTP transport sends a request right now and returns the answer immediately.

Work in progress

This plugin is actively being developed. The API surface, step definitions, and configuration keys may evolve before the first stable release.


When to Use​

Use the HTTP plugin when your system,

  • Exposes a synchronous REST API that you want to call from a scenario
  • Needs request and response assertions on status, body, or headers
  • Requires polling a endpoint until it returns an expected status
  • Combines HTTP calls with Kafka produce and consume steps in one flow

Installation​

Maven Dependency​

<dependency>
<groupId>io.github.ktestify</groupId>
<artifactId>ktestify-plugin-http</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>

With ktestify-cucumber (Docker)​

The HTTP plugin is a first-party plugin and is bundled in the ktestify-cucumber fat JAR. No additional installation is required.


Configuration​

The plugin reads settings from ktestify.plugins.http in your HOCON configuration file. Place your configuration in application.conf or override via environment variables.

Configuration Keys​

ktestify.plugins.http {
connect-timeout = 10s
read-timeout = 30s
poll-interval = 500ms
follow-redirects = true
tls {
trust-all = false
}
}
KeyTypeDefaultDescription
connect-timeoutduration10sMaximum time to establish a connection
read-timeoutduration30sMaximum time to read a response
poll-intervalduration500msDelay between polling attempts
follow-redirectsbooleantrueWhether to follow HTTP redirects
tls.trust-allbooleanfalseTrust all TLS certificates (disable verification)

Background Steps​

Given HTTP endpoint​

Declares a single HTTP endpoint with a base URL.

Given HTTP endpoint
| endpointAlias | baseUrl |
| orders-api | http://localhost:8080/api |
ColumnTypeRequiredDefaultDescription
endpointAliasstringyes-Alias referenced by request steps
baseUrlstringyes-Base URL for the endpoint

Given HTTP endpoints​

Declares multiple HTTP endpoints at once.

Given HTTP endpoints
| endpointAlias | baseUrl |
| orders-api | http://localhost:8080/api |
| users-api | http://localhost:8081 |
ColumnTypeRequiredDefaultDescription
endpointAliasstringyes-Alias referenced by request steps
baseUrlstringyes-Base URL for the endpoint

Given HTTP bearer token​

Attaches a bearer token to requests sent to an endpoint.

Given HTTP bearer token
| endpointAlias | token |
| orders-api | {{ENV:API_TOKEN}} |
ColumnTypeRequiredDefaultDescription
endpointAliasstringyes-Endpoint the token applies to
tokenstringyes-Bearer token value, supports dynamic variables

Given HTTP assets directory​

Declares the base directory for request and expected response files.

Given HTTP assets directory
| absolutePath |
| ./src/test/resources/data |
ColumnTypeRequiredDefaultDescription
absolutePathstringyes-Absolute or relative path to asset files

Action Steps​

When HTTP request is sent​

Sends an HTTP request and stores the response under an alias for later assertions.

When HTTP request is sent
| endpointAlias | method | path | file | responseAlias |
| orders-api | POST | /orders/validate | order.json | validate-resp |

When HTTP request is sent
| endpointAlias | method | path | queryParams | responseAlias |
| orders-api | GET | /orders/{id} | id=ORD-001 | fetch-resp |
ColumnTypeRequiredDefaultDescription
endpointAliasstringyes-Endpoint to send the request to
methodstringyes-HTTP method (GET, POST, PUT, DELETE, ...)
pathstringyes-Path appended to the base URL, supports {placeholder} substitution
filestringno-Request body file, resolved against the assets directory
queryParamsstringno-Query parameters as key=value pairs, comma separated
responseAliasstringyes-Alias used to reference the response in assertions

Validation Steps​

Then expected HTTP response status​

Asserts the HTTP status code of a stored response.

Then expected HTTP response status
| responseAlias | statusCode |
| validate-resp | 200 |
ColumnTypeRequiredDefaultDescription
responseAliasstringyes-Response to assert
statusCodeintyes-Expected HTTP status code

Then expected HTTP response body from file​

Asserts the response body against a file, with optional excluded keys.

Then expected HTTP response body from file
| responseAlias | file | excludedKeys |
| validate-resp | expected.json | timestamp,id |
ColumnTypeRequiredDefaultDescription
responseAliasstringyes-Response to assert
filestringyes-Expected body file
excludedKeysstringno-Comma separated keys to ignore during comparison

Then expected HTTP response XML body from file​

Asserts an XML response body against a file, with optional excluded elements.

Then expected HTTP response XML body from file
| responseAlias | file | excludedElements |
| validate-resp | expected.xml | ns:CreationDateTime |
ColumnTypeRequiredDefaultDescription
responseAliasstringyes-Response to assert
filestringyes-Expected XML body file
excludedElementsstringno-Comma separated elements to ignore during comparison

And HTTP response header should match​

Asserts a response header value.

And HTTP response header should match
| responseAlias | header | value |
| validate-resp | Content-Type | application/json |
ColumnTypeRequiredDefaultDescription
responseAliasstringyes-Response to assert
headerstringyes-Header name
valuestringyes-Expected header value

Then HTTP endpoint should eventually return​

Polls an endpoint until it returns an expected status or the read timeout elapses.

Then HTTP endpoint should eventually return
| endpointAlias | method | path | expectedStatus | readTimeout |
| orders-api | GET | /orders/ORD-001/status | 200 | 30 |
ColumnTypeRequiredDefaultDescription
endpointAliasstringyes-Endpoint to poll
methodstringyes-HTTP method
pathstringyes-Path appended to the base URL
expectedStatusintyes-Status code to wait for
readTimeoutintyes-Maximum seconds to keep polling