Skip to content

Commit

Permalink
Add schema tests for service
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
  • Loading branch information
Jarema committed Nov 2, 2023
1 parent 63db072 commit 16141c3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions async-nats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
43 changes: 43 additions & 0 deletions async-nats/tests/service_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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://raw.githubusercontent.com/nats-io/jsm.go/main/schemas/micro/v1/{}_response.json", url)
}
}

#[tokio::test]
#[cfg(not(target_os = "windows"))]
async fn cross_clients_tests() {
Expand Down

0 comments on commit 16141c3

Please sign in to comment.