From eaa7d1c79037b668e8ae65905fb76c26bedeca42 Mon Sep 17 00:00:00 2001 From: epi Date: Mon, 30 Nov 2020 18:45:04 -0600 Subject: [PATCH] added test for ferox response; fixed bug found in status code deserialization --- src/lib.rs | 6 +++--- src/scan_manager.rs | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e58ce67..fa5d0f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ use reqwest::{header::HeaderMap, Response, StatusCode, Url}; use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; use serde_json::Value; use std::collections::HashMap; -use std::convert::TryInto; +use std::convert::{TryFrom, TryInto}; use std::str::FromStr; use std::{error, fmt}; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; @@ -409,8 +409,8 @@ impl<'de> Deserialize<'de> for FeroxResponse { } } "status" => { - if let Some(num) = value.as_str() { - if let Ok(smaller) = u16::from_str(num) { + if let Some(num) = value.as_u64() { + if let Ok(smaller) = u16::try_from(num) { if let Ok(status) = StatusCode::from_u16(smaller) { response.status = status; } diff --git a/src/scan_manager.rs b/src/scan_manager.rs index 2b8b83b..4f71ff4 100644 --- a/src/scan_manager.rs +++ b/src/scan_manager.rs @@ -905,4 +905,29 @@ mod tests { serde_json::to_string(&ferox_scans).unwrap() ); } + + #[test] + /// given a FeroxResponse, test that it serializes into the proper JSON entry + fn ferox_response_serialize_and_deserialize() { + // deserialize + let json_response = r#"{"type":"response","url":"https://nerdcore.com/css","path":"/css","wildcard":true,"status":301,"content_length":173,"line_count":10,"word_count":16,"headers":{"referrer-policy":"origin-when-cross-origin","server":"nginx/1.16.1"}}"#; + let response: FeroxResponse = serde_json::from_str(json_response).unwrap(); + + assert_eq!(response.url.as_str(), "https://nerdcore.com/css"); + assert_eq!(response.url.path(), "/css"); + assert_eq!(response.wildcard, true); + assert_eq!(response.status.as_u16(), 301); + assert_eq!(response.content_length, 173); + assert_eq!(response.line_count, 10); + assert_eq!(response.word_count, 16); + assert_eq!(response.headers.get("server").unwrap(), "nginx/1.16.1"); + assert_eq!( + response.headers.get("referrer-policy").unwrap(), + "origin-when-cross-origin" + ); + + // serialize, however, this can fail when headers are out of order + let new_json = serde_json::to_string(&response).unwrap(); + assert_eq!(json_response, new_json); + } }