#api-testing #testing-http #e2e #http-api #test #api

grillon

Grillon offers an elegant and natural way to approach API testing in Rust

7 releases (4 breaking)

new 0.5.0 Oct 27, 2024
0.5.0-alpha.2 Oct 22, 2024
0.5.0-alpha.1 Sep 4, 2023
0.4.0 Jan 26, 2023
0.1.0 Dec 10, 2021

#107 in Testing

Download history 2/week @ 2024-07-19 5/week @ 2024-07-26 2/week @ 2024-09-20 2/week @ 2024-09-27 102/week @ 2024-10-18 176/week @ 2024-10-25

278 downloads per month

MIT/Apache

180KB
4K SLoC

Grillon

Crates.io docs.rs GitHub Workflow Status Check Links

Grillon offers an elegant and natural way to approach API testing in Rust.

  • Elegant, intuitive and expressive API
  • Built-in testing functions
  • Extensible

Please note that the API is subject to a lot of changes until the v1.0.0.

Documentation

Getting started

You need Tokio as asynchronous runtime. Generally, testing libs are used in unit or integration tests so let's declare grillon as a dev-dependency.

Add grillon to Cargo.toml

[dev-dependencies]
grillon = "0.5.0"
tokio = { version = "1", features = ["macros"] }

Then use grillon :

use grillon::{dsl::*, dsl::http::*, json, Grillon, StatusCode, Result};
use grillon::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE};
use grillon::Assert;

#[tokio::test]
async fn end_to_end_test() -> Result<()> {
    Grillon::new("https://jsonplaceholder.typicode.com")?
        .post("posts")
        .payload(json!({
            "title": "foo",
            "body": "bar",
            "userId": 1
        }))
        .assert()
        .await
        .status(is_success())
        .status(is(201))
        .response_time(is_less_than(700))
        .json_body(is(json!({
            "id": 101,
        })))
        .json_body(schema(json!({
            "properties": {
                "id": { "type": "number" }
            }
        })))
        .json_path("$.id", is(json!(101)))
        .header(CONTENT_TYPE, is("application/json; charset=utf-8"))
        .headers(contains(vec![
            (
                CONTENT_TYPE,
                HeaderValue::from_static("application/json; charset=utf-8"),
            ),
            (CONTENT_LENGTH, HeaderValue::from_static("15")),
        ]))
        .assert_fn(|assert| {
            let Assert {
                headers,
                status,
                json,
                ..
            } = assert.clone();

            assert!(!headers.unwrap().is_empty());
            assert!(status.unwrap() == StatusCode::CREATED);
            assert!(json.is_some());

            println!("Json response : {:#?}", assert.json);
        });

    Ok(())
}

Dependencies

~14–27MB
~417K SLoC