added test for ferox response; fixed bug found in status code deserialization

This commit is contained in:
epi
2020-11-30 18:45:04 -06:00
parent f29cd16616
commit eaa7d1c790
2 changed files with 28 additions and 3 deletions

View File

@@ -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;
}

View File

@@ -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);
}
}