61
tests/specification.rs
Normal file
61
tests/specification.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use axum::http::{self, Request, StatusCode};
|
||||
use echo::{
|
||||
app,
|
||||
messages::{BodyRequest, EchoRequest},
|
||||
};
|
||||
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 = {
|
||||
let src = "c1".to_string();
|
||||
let dest = "n1".to_string();
|
||||
let r#type = "echo".to_string();
|
||||
let msg_id = 1;
|
||||
let echo = "Please echo 35".to_string();
|
||||
let body = BodyRequest {
|
||||
r#type,
|
||||
msg_id,
|
||||
echo,
|
||||
};
|
||||
EchoRequest { src, dest, body }
|
||||
};
|
||||
let body = serde_json::to_string_pretty(&request).unwrap();
|
||||
info!("Request: {body}");
|
||||
|
||||
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 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);
|
||||
}
|
||||
Reference in New Issue
Block a user