diff options
| author | Kevin J Hoerr <kjhoerr@submelon.tech> | 2019-10-30 14:36:53 -0400 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@submelon.tech> | 2019-10-30 14:36:53 -0400 |
| commit | 6fa8950cf867a8e1246c2f2c90a4047ae5175f6f (patch) | |
| tree | b5a538ceff7d66c4f98417394db3eecc242b5ffb /src | |
| parent | 82c3cc2c1952020ef13c330047cbc8ba1dc7cc75 (diff) | |
| download | august-offensive-6fa8950cf867a8e1246c2f2c90a4047ae5175f6f.tar.gz august-offensive-6fa8950cf867a8e1246c2f2c90a4047ae5175f6f.tar.bz2 august-offensive-6fa8950cf867a8e1246c2f2c90a4047ae5175f6f.zip | |
Replace Json handler with handler for status codes
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/messages/format_msg.rs | 37 | ||||
| -rw-r--r-- | src/messages/mod.rs | 2 | ||||
| -rw-r--r-- | src/routes/callback.rs | 2 | ||||
| -rw-r--r-- | src/routes/mod.rs | 12 | ||||
| -rw-r--r-- | src/routes/not_understood.rs | 5 |
6 files changed, 51 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index b5bab39..ecb7219 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ extern crate diesel; extern crate serde; #[macro_use] extern crate serde_derive; +extern crate serde_json; pub mod messages; pub mod routes; diff --git a/src/messages/format_msg.rs b/src/messages/format_msg.rs new file mode 100644 index 0000000..34e4981 --- /dev/null +++ b/src/messages/format_msg.rs @@ -0,0 +1,37 @@ +use actix_web::{http::StatusCode, Error, HttpRequest, HttpResponse, Responder}; +use serde::Serialize; + +pub struct FormatMsg<T> { + pub message: T, + pub code: StatusCode, +} + +impl<T> FormatMsg<T> { + /// Deconstruct to an inner value + pub fn into_inner(self) -> T { + self.message + } + + pub fn ok(message: T) -> Self { + FormatMsg { + message: message, + code: StatusCode::OK, + } + } +} + +impl<T: Serialize> Responder for FormatMsg<T> { + type Error = Error; + type Future = Result<HttpResponse, Error>; + + fn respond_to(self, _: &HttpRequest) -> Self::Future { + let body = match serde_json::to_string(&self.message) { + Ok(body) => body, + Err(e) => return Err(e.into()), + }; + + Ok(HttpResponse::build(self.code) + .content_type("application/json") + .body(body)) + } +}
\ No newline at end of file diff --git a/src/messages/mod.rs b/src/messages/mod.rs index ef326a0..18f2bb7 100644 --- a/src/messages/mod.rs +++ b/src/messages/mod.rs @@ -1,8 +1,10 @@ use std::marker::Sized; +pub mod format_msg; pub mod callback; pub mod not_understood; +pub use self::format_msg::FormatMsg; pub use self::callback::Callback; pub use self::not_understood::NotUnderstood; diff --git a/src/routes/callback.rs b/src/routes/callback.rs index e81a080..4a258ae 100644 --- a/src/routes/callback.rs +++ b/src/routes/callback.rs @@ -11,7 +11,7 @@ pub fn callback(req: HttpRequest, query: Query<HashMap<String, String>>) -> Json content: query.into_inner(), }; - Ok(Json(callback.as_outgoing())) + Ok(FormatMsg::ok(callback.as_outgoing())) } #[cfg(test)] diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 920b172..a631978 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,4 +1,5 @@ -use actix_web::{web::{route, scope, Json, Query}, HttpRequest, Result, Scope}; +use actix_web::{web::{route, scope, Query}, HttpRequest, Result, Scope}; +use actix_web::http::StatusCode; use messages::*; use std::collections::HashMap; @@ -8,7 +9,7 @@ mod not_understood; use self::callback::callback; use self::not_understood::not_understood; -type JsonMessage<U> = Result<Json<OutgoingMsg<U>>>; +type JsonMessage<U> = Result<FormatMsg<OutgoingMsg<U>>>; // Provides the routes for the application pub fn get_scope() -> Scope { @@ -29,7 +30,7 @@ fn destruct_path(path: &str) -> Vec<String> { #[cfg(test)] mod tests { use super::*; - use actix_web::{http::{Method, StatusCode}, test::TestRequest}; + use actix_web::{http::Method, test::TestRequest}; use actix_web::{App, dev::Service, test::{block_on, init_service}}; #[test] @@ -45,7 +46,7 @@ mod tests { // Assert assert_eq!(resp.status(), StatusCode::OK); //TODO assert response is messages::Callback - //assert_eq!(resp.response().json()); + //assert_eq!(Json::from_request(req, resp.payload()).content_type, "CALLBACK"); } #[test] @@ -59,8 +60,7 @@ mod tests { let resp = block_on(srv.call(req)).unwrap(); // Assert - //FIXME NotUnderstood response's code should be NOT_FOUND? - assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.status(), StatusCode::NOT_FOUND); //TODO assert response is messages::NotUnderstood //assert_eq!(resp.response().json()); } diff --git a/src/routes/not_understood.rs b/src/routes/not_understood.rs index c94e6e2..4e30361 100644 --- a/src/routes/not_understood.rs +++ b/src/routes/not_understood.rs @@ -6,7 +6,10 @@ pub fn not_understood(req: HttpRequest) -> JsonMessage<NotUnderstood> { path: destruct_path(req.path()), }; - Ok(Json(message.as_outgoing())) + Ok(FormatMsg { + message: message.as_outgoing(), + code: StatusCode::NOT_FOUND, + }) } #[cfg(test)] |
