Reworked MessageBody Typesystem and added challenge 03
Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
This commit is contained in:
95
tests/broadcast.rs
Normal file
95
tests/broadcast.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use axum::http::{self, Request, StatusCode};
|
||||
use echo::{
|
||||
app,
|
||||
messages::{Message, MessageBody},
|
||||
};
|
||||
use http_body_util::BodyExt;
|
||||
use serde_json::{json, Value};
|
||||
use tower::{Service, ServiceExt};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_rw() {
|
||||
let mut app = app().into_service();
|
||||
|
||||
let message = "test".to_string();
|
||||
|
||||
let response = {
|
||||
let request: Message = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let body = {
|
||||
let msg_id = 1;
|
||||
let message = message.clone();
|
||||
|
||||
MessageBody::Broadcast { msg_id, message }
|
||||
};
|
||||
Message { src, dest, body }
|
||||
};
|
||||
|
||||
let request = Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.uri("/")
|
||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||
.body(serde_json::to_string(&request).unwrap())
|
||||
.unwrap();
|
||||
|
||||
let response = ServiceExt::<Request<String>>::ready(&mut app)
|
||||
.await
|
||||
.unwrap()
|
||||
.call(request)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
let request: Message = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let body = {
|
||||
let msg_id = 2;
|
||||
|
||||
MessageBody::Read { msg_id }
|
||||
};
|
||||
Message { src, dest, body }
|
||||
};
|
||||
|
||||
let request = Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.uri("/")
|
||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||
.body(serde_json::to_string(&request).unwrap())
|
||||
.unwrap();
|
||||
|
||||
let response = ServiceExt::<Request<String>>::ready(&mut app)
|
||||
.await
|
||||
.unwrap()
|
||||
.call(request)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
response
|
||||
};
|
||||
|
||||
{
|
||||
let body: Value = {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
serde_json::from_slice(&body).unwrap()
|
||||
};
|
||||
|
||||
let expected = json!(
|
||||
{
|
||||
"src": "n1",
|
||||
"dest": "c1",
|
||||
"body": {
|
||||
"type": "read_ok",
|
||||
"msg_id": 2,
|
||||
"in_reply_to": 2,
|
||||
"message": message
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(body, expected);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +1,61 @@
|
||||
use axum::http::{self, Request, StatusCode};
|
||||
use echo::{
|
||||
app,
|
||||
messages::{EchoRequest, Message, MessageBody},
|
||||
messages::{Message, MessageBody},
|
||||
};
|
||||
use http_body_util::BodyExt;
|
||||
use serde_json::{json, Value};
|
||||
use tower::ServiceExt;
|
||||
use tracing::info;
|
||||
|
||||
#[tokio::test]
|
||||
async fn specification() {
|
||||
tracing_subscriber::fmt::init();
|
||||
let request: Message = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let response_type = "echo".to_string();
|
||||
let msg_id = 1;
|
||||
let echo = "Please echo 35".to_string();
|
||||
let body = MessageBody::EchoRequest(EchoRequest {
|
||||
response_type,
|
||||
msg_id,
|
||||
echo,
|
||||
});
|
||||
Message { src, dest, body }
|
||||
};
|
||||
let body = serde_json::to_string_pretty(&request).unwrap();
|
||||
info!("Request: {body}");
|
||||
|
||||
async fn test_echo() {
|
||||
let app = app().into_service();
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.uri("/echo")
|
||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||
.body(serde_json::to_string(&request).unwrap())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let response = {
|
||||
let request: Message = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let body = {
|
||||
let msg_id = 1;
|
||||
let echo = "Please echo 35".to_string();
|
||||
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let body: Value = serde_json::from_slice(&body).unwrap();
|
||||
let expected = json!(
|
||||
{
|
||||
"src": "n1",
|
||||
"dest": "c1",
|
||||
"body": {
|
||||
"type": "echo_ok",
|
||||
"msg_id": 1,
|
||||
"in_reply_to": 1,
|
||||
"echo": "Please echo 35"
|
||||
MessageBody::Echo { msg_id, echo }
|
||||
};
|
||||
Message { src, dest, body }
|
||||
};
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.uri("/")
|
||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||
.body(serde_json::to_string(&request).unwrap())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
response
|
||||
};
|
||||
|
||||
{
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let body: Value = serde_json::from_slice(&body).unwrap();
|
||||
let expected = json!(
|
||||
{
|
||||
"src": "n1",
|
||||
"dest": "c1",
|
||||
"body": {
|
||||
"type": "echo_ok",
|
||||
"msg_id": 1,
|
||||
"in_reply_to": 1,
|
||||
"echo": "Please echo 35"
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
assert_eq!(body, expected);
|
||||
assert_eq!(body, expected);
|
||||
}
|
||||
}
|
||||
|
||||
150
tests/uid.rs
Normal file
150
tests/uid.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
use axum::http::{self, Request, StatusCode};
|
||||
use echo::{
|
||||
app,
|
||||
messages::{Message, MessageBody},
|
||||
};
|
||||
use http_body_util::BodyExt;
|
||||
use serde_json::{json, Value};
|
||||
use tower::{Service, ServiceExt};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_single() {
|
||||
let app = app().into_service();
|
||||
|
||||
let response = {
|
||||
let request: Message = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let body = {
|
||||
let msg_id = 1;
|
||||
|
||||
MessageBody::Generate { msg_id }
|
||||
};
|
||||
Message { src, dest, body }
|
||||
};
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.uri("/")
|
||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||
.body(serde_json::to_string(&request).unwrap())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
response
|
||||
};
|
||||
|
||||
{
|
||||
let body: Value = {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
serde_json::from_slice(&body).unwrap()
|
||||
};
|
||||
|
||||
let expected = json!(
|
||||
{
|
||||
"src": "n1",
|
||||
"dest": "c1",
|
||||
"body": {
|
||||
"type": "generate_ok",
|
||||
"msg_id": 1,
|
||||
"in_reply_to": 1,
|
||||
"id": 0
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(body, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_multiple() {
|
||||
let mut app = app().into_service();
|
||||
let request_count = 3;
|
||||
|
||||
let response = {
|
||||
for i in 0..request_count {
|
||||
let request: Message = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let body = {
|
||||
let msg_id = i;
|
||||
|
||||
MessageBody::Generate { msg_id }
|
||||
};
|
||||
Message { src, dest, body }
|
||||
};
|
||||
|
||||
let request = Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.uri("/")
|
||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||
.body(serde_json::to_string(&request).unwrap())
|
||||
.unwrap();
|
||||
|
||||
let response = ServiceExt::<Request<String>>::ready(&mut app)
|
||||
.await
|
||||
.unwrap()
|
||||
.call(request)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
let request: Message = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let body = {
|
||||
let msg_id = request_count;
|
||||
|
||||
MessageBody::Generate { msg_id }
|
||||
};
|
||||
Message { src, dest, body }
|
||||
};
|
||||
|
||||
let request = Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.uri("/")
|
||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||
.body(serde_json::to_string(&request).unwrap())
|
||||
.unwrap();
|
||||
|
||||
let response = ServiceExt::<Request<String>>::ready(&mut app)
|
||||
.await
|
||||
.unwrap()
|
||||
.call(request)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
response
|
||||
};
|
||||
|
||||
{
|
||||
let body: Value = {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
serde_json::from_slice(&body).unwrap()
|
||||
};
|
||||
|
||||
let expected = json!(
|
||||
{
|
||||
"src": "n1",
|
||||
"dest": "c1",
|
||||
"body": {
|
||||
"type": "generate_ok",
|
||||
"msg_id": request_count,
|
||||
"in_reply_to": request_count,
|
||||
"id": request_count
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(body, expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user