Expand description
§Grillon
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
Before you begin, be sure to read the book to learn more about configuring logs and assertions!
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.6.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(())
}
Modules§
- assert
- The
assert
module provides everything to assert parts of http responses with built-in matchers. - assertion
- Functionnality for asserting.
- dsl
- A domain-specific language organized into various modules providing built-in types and functions for performing declarative assertions.
- header
- HTTP header types
- request
- The
request
module provides everything to build http requests for endpoints under tests. - response
- The
response
module provides everything to implement custom responses that can be asserted withAssert
.
Macros§
- json
- Construct a
serde_json::Value
from a JSON literal.
Structs§
- Assert
Assert
uses an internal representation of the http response to assert against. If the HTTP request was successfully sent, then each field will beSome
, otherwiseNone
.- Grillon
- Top-level instance to configure a REST API http client.
- Method
- The Request Method (VERB)
- Request
- Represents an outgoing http request.
- Status
Code - An HTTP status code (
status-code
in RFC 9110 et al.).
Enums§
- Error
- Represents the library errors.
- LogSettings
- The log settings to output test results.
- Value
- Represents any valid JSON value.
Traits§
- Response
- A generic http response representation with
convenience methods for subsequent assertions
with
Assert
.
Type Aliases§
- Result
- Short hand for
Result
type.