Skip to content

Commit

Permalink
update response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Apr 6, 2024
1 parent 8bbbfd5 commit 05c44dd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ extensions:
healthy_response_time_ms: 500 # max response time to be considered healthy, default is 500ms
health_method: system_health
expected_response: # response contains { isSyncing: false }
!object
!contains
- - isSyncing
- !value false
- !eq false
event_bus:
substrate_api:
stale_timeout_seconds: 180 # rotate endpoint if no new blocks for 3 minutes
Expand Down
6 changes: 3 additions & 3 deletions eth_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ extensions:
healthy_response_time_ms: 500 # max response time to be considered healthy, default is 500ms
health_method: net_health # eth-rpc-adapter bodhijs
expected_response: # response contains { isHealthy: true, isRPCOK: true }
!object
!contains
- - isHealthy
- !value true
- !eq true
- - isRPCOK
- !value true
- !eq true
# health_method: eth_syncing # eth node
# expected_response: !value false
event_bus:
Expand Down
47 changes: 25 additions & 22 deletions src/extensions/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,17 @@ pub fn healthy_response_time_ms() -> u64 {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HealthResponse {
Value(JsonValue),
Object(Vec<(String, Box<HealthResponse>)>),
Eq(JsonValue),
NotEq(JsonValue),
Contains(Vec<(String, Box<HealthResponse>)>),
}

impl HealthResponse {
pub fn validate(&self, response: &JsonValue) -> bool {
match (self, response) {
(HealthResponse::Value(value), response) => value.eq(response),
(HealthResponse::Object(items), response) => {
match self {
HealthResponse::Eq(value) => value.eq(response),
HealthResponse::NotEq(value) => !value.eq(response),
HealthResponse::Contains(items) => {
for (key, expected) in items {
if let Some(response) = response.get(key) {
if !expected.validate(response) {
Expand Down Expand Up @@ -494,16 +496,16 @@ fn test_get_backoff_time() {

#[test]
fn health_response_serialize_deserialize_works() {
let response = HealthResponse::Object(vec![(
let response = HealthResponse::Contains(vec![(
"isSyncing".to_string(),
Box::new(HealthResponse::Value(false.into())),
Box::new(HealthResponse::Eq(false.into())),
)]);

let expected = serde_yaml::from_str::<HealthResponse>(
r"
!object
!contains
- - isSyncing
- !value false
- !eq false
",
)
.unwrap();
Expand All @@ -517,7 +519,7 @@ fn health_response_validation_works() {

let expected = serde_yaml::from_str::<HealthResponse>(
r"
!value true
!eq true
",
)
.unwrap();
Expand All @@ -526,9 +528,9 @@ fn health_response_validation_works() {

let expected = serde_yaml::from_str::<HealthResponse>(
r"
!object
!contains
- - isSyncing
- !value false
- !eq false
",
)
.unwrap();
Expand All @@ -547,11 +549,11 @@ fn health_response_validation_works() {
// multiple items
let expected = serde_yaml::from_str::<HealthResponse>(
r"
!object
!contains
- - isSyncing
- !value false
- !eq false
- - peers
- !value 3
- !eq 3
",
)
.unwrap();
Expand All @@ -567,9 +569,9 @@ fn health_response_validation_works() {
// works with strings
let expected = serde_yaml::from_str::<HealthResponse>(
r"
!object
!contains
- - foo
- !value bar
- !eq bar
",
)
.unwrap();
Expand All @@ -579,18 +581,19 @@ fn health_response_validation_works() {
// multiple nested items
let expected = serde_yaml::from_str::<HealthResponse>(
r"
!object
!contains
- - foo
- !object
- !contains
- - one
- !value subway
- !eq subway
- - two
- !value subway
- !not_eq subway
",
)
.unwrap();
let cases = [
(json!({ "foo": { "one": "subway", "two": "subway" } }), true),
(json!({ "foo": { "one": "subway", "two": "not_subway" } }), true),
(json!({ "foo": { "one": "subway", "two": "subway" } }), false),
(json!({ "foo": { "subway": "one" } }), false),
(json!({ "bar" : { "foo": { "subway": "one", "two": "subway" } }}), false),
(json!({ "foo": "subway" }), false),
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/client/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ async fn health_check_works() {
interval_sec: 1,
healthy_response_time_ms: 250,
health_method: Some("system_health".into()),
expected_response: Some(HealthResponse::Object(vec![(
expected_response: Some(HealthResponse::Contains(vec![(
"isSyncing".to_string(),
Box::new(HealthResponse::Value(false.into())),
Box::new(HealthResponse::Eq(false.into())),
)])),
}),
)
Expand Down

0 comments on commit 05c44dd

Please sign in to comment.