Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yayanyang committed Feb 29, 2024
1 parent 9a336f0 commit 9a1751a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/net/http/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,35 @@ where
Ok(serde_json::from_slice(&buf)?)
}
}

#[cfg(test)]
mod tests {
use super::*;
use hala_io::test::io_test;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Mock {
a: i32,
b: String,
}

#[hala_test::test(io_test)]
async fn test_from_json() {
let mock = Mock {
a: 10,
b: "hello".to_string(),
};

let mut json_data = Bytes::from(serde_json::to_string_pretty(&mock).unwrap());

let body_reader = BodyReader::new(
json_data.split_to(json_data.len() / 2),
Cursor::new(json_data),
);

let mock2 = body_reader.from_json::<Mock>().await.unwrap();

assert_eq!(mock, mock2);
}
}
5 changes: 5 additions & 0 deletions crates/net/http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
pub mod parse;

pub use http::request::Builder as RequestBuilder;

pub use http::response::Builder as ResponseBuilder;

pub use http::{Method, Request, Response, Uri, Version};

pub mod body;
7 changes: 7 additions & 0 deletions crates/net/http/tests/parse_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Mock {
hello: u32,
world: String,
}

0 comments on commit 9a1751a

Please sign in to comment.