diff options
| author | Kevin J Hoerr <kjhoerr@submelon.tech> | 2019-10-31 11:43:08 -0400 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@submelon.tech> | 2019-10-31 11:43:08 -0400 |
| commit | 35381296e644fd63aa9e1a901da4d2f7ee269add (patch) | |
| tree | 5ef0c291985a24a658ea2ae70923de756350fe9f /src | |
| parent | 6fa8950cf867a8e1246c2f2c90a4047ae5175f6f (diff) | |
| download | august-offensive-35381296e644fd63aa9e1a901da4d2f7ee269add.tar.gz august-offensive-35381296e644fd63aa9e1a901da4d2f7ee269add.tar.bz2 august-offensive-35381296e644fd63aa9e1a901da4d2f7ee269add.zip | |
Test routed message types
Diffstat (limited to 'src')
| -rw-r--r-- | src/messages/callback.rs | 2 | ||||
| -rw-r--r-- | src/messages/mod.rs | 2 | ||||
| -rw-r--r-- | src/messages/not_understood.rs | 2 | ||||
| -rw-r--r-- | src/routes/mod.rs | 33 |
4 files changed, 30 insertions, 9 deletions
diff --git a/src/messages/callback.rs b/src/messages/callback.rs index fe3ef30..f8c7bb3 100644 --- a/src/messages/callback.rs +++ b/src/messages/callback.rs @@ -1,7 +1,7 @@ use messages::Message; use std::collections::HashMap; -#[derive(Clone, Debug, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Callback { pub path: Vec<String>, pub request: String, diff --git a/src/messages/mod.rs b/src/messages/mod.rs index 18f2bb7..afc97c6 100644 --- a/src/messages/mod.rs +++ b/src/messages/mod.rs @@ -8,7 +8,7 @@ pub use self::format_msg::FormatMsg; pub use self::callback::Callback; pub use self::not_understood::NotUnderstood; -#[derive(Serialize)] +#[derive(Deserialize, Serialize)] pub struct OutgoingMsg<T: Message> { pub result_type: String, pub content: T, diff --git a/src/messages/not_understood.rs b/src/messages/not_understood.rs index b567676..0089b9c 100644 --- a/src/messages/not_understood.rs +++ b/src/messages/not_understood.rs @@ -1,6 +1,6 @@ use messages::Message; -#[derive(Clone, Debug, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct NotUnderstood { pub path: Vec<String>, } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index a631978..dd7a526 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -32,6 +32,9 @@ mod tests { use super::*; use actix_web::{http::Method, test::TestRequest}; use actix_web::{App, dev::Service, test::{block_on, init_service}}; + use actix_web::dev::{Body, ServiceResponse}; + use serde::Deserialize; + use std::str; #[test] fn test_get_scope_callback() { @@ -41,12 +44,14 @@ mod tests { let mut srv = init_service(App::new().service(scope)); // Act - let resp = block_on(srv.call(req)).unwrap(); + let resp = &block_on(srv.call(req)).unwrap(); // Assert assert_eq!(resp.status(), StatusCode::OK); - //TODO assert response is messages::Callback - //assert_eq!(Json::from_request(req, resp.payload()).content_type, "CALLBACK"); + + let content = get_message::<OutgoingMsg<Callback>>(resp); + assert_eq!(content.result_type, "CALLBACK"); + assert_eq!(content.content.path, vec!["api", "callback"]); } #[test] @@ -57,12 +62,14 @@ mod tests { let mut srv = init_service(App::new().service(scope)); // Act - let resp = block_on(srv.call(req)).unwrap(); + let resp = &block_on(srv.call(req)).unwrap(); // Assert assert_eq!(resp.status(), StatusCode::NOT_FOUND); - //TODO assert response is messages::NotUnderstood - //assert_eq!(resp.response().json()); + + let content = get_message::<OutgoingMsg<NotUnderstood>>(resp); + assert_eq!(content.result_type, "NOT_UNDERSTOOD"); + assert_eq!(content.content.path, vec!["api", "404"]); } #[test] @@ -129,4 +136,18 @@ mod tests { Query::from_query(&query_str).unwrap() } + + fn get_message<'a, T: Deserialize<'a>>(response: &'a ServiceResponse) -> T { + let body = response.response().body().as_ref().unwrap(); + let mut array = &[b'0';0][..]; + match body { + Body::Bytes(b) => { + array = b.as_ref(); + }, + _ => {}, + }; + + let van = str::from_utf8(array).unwrap(); + serde_json::from_str(van).unwrap() + } } |
