-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subscription ID recognition to tungstenite and ws-rpc client (#662)
* fix error * fix clippy warning * seems to wrok * check for subscirption id * fix clippy * fix typo * remove error logs * add info log * we expect an immediate response * add tests for rpc client * fix test and clean up ws_rpc client * fix test * fix test * fix clippy * fix tests * git commit * add missing comma * extract helper functions * add missing helper file * remove code from ws-client and use helpers * fix compile errors
- Loading branch information
Showing
13 changed files
with
384 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
Copyright 2019 Supercomputing Systems AG | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
use alloc::{ | ||
format, | ||
string::{String, ToString}, | ||
}; | ||
use serde_json::Value; | ||
|
||
pub fn read_subscription_id(value: &Value) -> Option<String> { | ||
value["result"].as_str().map(|str| str.to_string()) | ||
} | ||
|
||
pub fn read_error_message(value: &Value, msg: &str) -> String { | ||
match value["error"].as_str() { | ||
Some(error_message) => error_message.to_string(), | ||
None => format!("Unexpected Response: {}", msg), | ||
} | ||
} | ||
|
||
pub fn subscription_id_matches(value: &Value, subscription_id: &str) -> bool { | ||
match value["params"]["subscription"].as_str() { | ||
Some(retrieved_subscription_id) => subscription_id == retrieved_subscription_id, | ||
None => false, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use serde_json::json; | ||
|
||
#[test] | ||
fn read_valid_subscription_response() { | ||
let subcription_id = "tejkataa12124a"; | ||
let value = json!({ | ||
"result": subcription_id, | ||
"id": 43, | ||
"and_so_on": "test", | ||
}); | ||
|
||
let maybe_subcription_id = read_subscription_id(&value); | ||
assert_eq!(maybe_subcription_id, Some(subcription_id.to_string())); | ||
} | ||
|
||
#[test] | ||
fn read_invalid_subscription_response() { | ||
let subcription_id = "tejkataa12124a"; | ||
let value = json!({ | ||
"error": subcription_id, | ||
"id": 43, | ||
"and_so_on": "test", | ||
}); | ||
|
||
let maybe_subcription_id = read_subscription_id(&value); | ||
assert!(maybe_subcription_id.is_none()); | ||
} | ||
|
||
#[test] | ||
fn read_error_message_returns_error_if_available() { | ||
let error_message = "some_error_message"; | ||
let value = json!({ | ||
"error": error_message, | ||
"id": 43, | ||
"and_so_on": "test", | ||
}); | ||
|
||
let msg = serde_json::to_string(&value).unwrap(); | ||
|
||
let message = read_error_message(&value, &msg); | ||
assert!(message.contains(error_message)); | ||
assert!(message.contains("error")); | ||
} | ||
|
||
#[test] | ||
fn read_error_message_returns_full_msg_if_error_is_not_available() { | ||
let error_message = "some_error_message"; | ||
let value = json!({ | ||
"result": error_message, | ||
"id": 43, | ||
"and_so_on": "test", | ||
}); | ||
|
||
let msg = serde_json::to_string(&value).unwrap(); | ||
|
||
let message = read_error_message(&value, &msg); | ||
assert!(message.contains(&msg)); | ||
} | ||
|
||
#[test] | ||
fn subscription_id_matches_returns_true_for_equal_id() { | ||
let subcription_id = "tejkataa12124a"; | ||
let value = json!({ | ||
"params": { | ||
"subscription": subcription_id, | ||
"message": "Test" | ||
}, | ||
"id": 43, | ||
"and_so_on": "test", | ||
}); | ||
|
||
assert!(subscription_id_matches(&value, subcription_id)); | ||
} | ||
|
||
#[test] | ||
fn subscription_id_matches_returns_false_for_not_equal_id() { | ||
let subcription_id = "tejkataa12124a"; | ||
let value = json!({ | ||
"params": { | ||
"subscription": "something else", | ||
"message": "Test" | ||
}, | ||
"id": 43, | ||
"and_so_on": "test", | ||
}); | ||
|
||
assert!(!subscription_id_matches(&value, subcription_id)); | ||
} | ||
|
||
#[test] | ||
fn subscription_id_matches_returns_false_for_missing_subscription() { | ||
let subcription_id = "tejkataa12124a"; | ||
let value = json!({ | ||
"params": { | ||
"result": subcription_id, | ||
"message": "Test" | ||
}, | ||
"id": 43, | ||
"and_so_on": "test", | ||
}); | ||
|
||
assert!(!subscription_id_matches(&value, subcription_id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.