ubisync/tests/api.rs

33 lines
1.6 KiB
Rust
Raw Normal View History

2023-12-07 19:51:19 +01:00
use std::time::Duration;
use tracing::{Level, debug};
use ubisync::{Ubisync, config::Config, state::types::{ElementContent, Element, ElementId}};
#[tokio::test(flavor = "multi_thread")]
async fn two_nodes_element_creation() {
tracing_subscriber::fmt().pretty().with_max_level(Level::DEBUG).init();
// Two nodes need to bind to different ports
let mut c2 = Config::default();
c2.api_config.port = Some(9982);
let ubi1 = Ubisync::new(&Config::default()).await.unwrap();
let ubi2 = Ubisync::new(&c2).await.unwrap();
ubi1.add_peer_from_id(ubi2.get_destination().unwrap().into()).unwrap();
let http_client = reqwest::Client::new();
let test_element_content = ElementContent::Text("Text".to_string());
let put_resp = http_client.put(&format!("http://localhost:9981/v0/element")).json(&test_element_content).send().await.unwrap();
debug!("{:?}", &put_resp);
let id = serde_json::from_str::<ElementId>(&put_resp.text().await.expect("No put response body")).expect("Could not deserialize ElementId");
tokio::time::sleep(Duration::from_millis(3000)).await;
let get_resp = http_client.get(&format!("http://localhost:9982/v0/element/{}", Into::<String>::into(&id))).send().await.expect("Get request failed");
let received_element = serde_json::from_str::<Element>(&get_resp.text().await.expect("No get request body")).expect("Could not deserialize Element");
debug!("Other node received this element: {:?}", received_element);
assert_eq!(&test_element_content, received_element.content());
std::process::exit(0);
}