From 385f154d2515f4330659a5304f7d88a9ee731b2a Mon Sep 17 00:00:00 2001 From: jcamiel Date: Tue, 14 Feb 2023 14:28:45 +0100 Subject: [PATCH] Add Default impl for http::Response --- packages/hurl/src/http/response.rs | 64 +++++---------- packages/hurl/src/http/response_decoding.rs | 90 ++++----------------- packages/hurl/src/runner/query.rs | 42 ++-------- 3 files changed, 42 insertions(+), 154 deletions(-) diff --git a/packages/hurl/src/http/response.rs b/packages/hurl/src/http/response.rs index 7d9efe6b1..d3ee3693d 100644 --- a/packages/hurl/src/http/response.rs +++ b/packages/hurl/src/http/response.rs @@ -34,6 +34,20 @@ pub struct Response { pub certificate: Option, } +impl Default for Response { + fn default() -> Self { + Response { + version: Version::Http10, + status: 200, + headers: vec![], + body: vec![], + duration: Default::default(), + url: "".to_string(), + certificate: None, + } + } +} + #[derive(Clone, Debug, PartialEq, Eq)] pub enum Version { Http10, @@ -72,8 +86,6 @@ pub mod tests { pub fn hello_http_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![ Header { name: String::from("Content-Type"), @@ -85,16 +97,12 @@ pub mod tests { }, ], body: String::into_bytes(String::from("Hello World!")), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } pub fn html_http_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: String::from("Content-Type"), value: String::from("text/html; charset=utf-8"), @@ -102,16 +110,12 @@ pub mod tests { body: String::into_bytes(String::from( "
", )), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } pub fn xml_invalid_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![ Header { name: String::from("Content-Type"), @@ -128,16 +132,12 @@ xxx "# .to_string(), ), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } pub fn xml_two_users_http_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![ Header { name: String::from("Content-Type"), @@ -158,16 +158,12 @@ xxx "# .to_string(), ), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } pub fn xml_three_users_http_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![ Header { name: String::from("Content-Type"), @@ -189,17 +185,12 @@ xxx "# .to_string(), ), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } pub fn json_http_response() -> Response { Response { - version: Version::Http10, - status: 0, - headers: vec![], body: String::into_bytes( r#" { @@ -213,16 +204,12 @@ xxx "# .to_string(), ), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } pub fn bytes_http_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![ Header { name: String::from("Content-Type"), @@ -234,25 +221,18 @@ xxx }, ], body: vec![255], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } #[test] fn get_header_values() { let response = Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Length".to_string(), value: "12".to_string(), }], - body: vec![], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; assert_eq!( response.get_header_values("Content-Length"), diff --git a/packages/hurl/src/http/response_decoding.rs b/packages/hurl/src/http/response_decoding.rs index b2ee89e89..c28ad8d32 100644 --- a/packages/hurl/src/http/response_decoding.rs +++ b/packages/hurl/src/http/response_decoding.rs @@ -198,7 +198,7 @@ fn uncompress_zlib(data: &[u8]) -> Result, HttpError> { #[cfg(test)] pub mod tests { use super::*; - use crate::http::{Header, Response, Version}; + use crate::http::{Header, Response}; #[test] fn test_parse_content_encoding() { @@ -216,28 +216,15 @@ pub mod tests { #[test] fn test_content_encoding() { - let response = Response { - version: Version::Http10, - status: 200, - headers: vec![], - body: vec![], - duration: Default::default(), - url: "".to_string(), - certificate: None, - }; + let response = Response::default(); assert_eq!(response.content_encoding().unwrap(), vec![]); let response = Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Encoding".to_string(), value: "xx".to_string(), }], - body: vec![], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; assert_eq!( response.content_encoding().err().unwrap(), @@ -247,16 +234,11 @@ pub mod tests { ); let response = Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Encoding".to_string(), value: "br".to_string(), }], - body: vec![], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; assert_eq!( response.content_encoding().unwrap(), @@ -267,16 +249,11 @@ pub mod tests { #[test] fn test_multiple_content_encoding() { let response = Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Encoding".to_string(), value: "br, identity".to_string(), }], - body: vec![], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; assert_eq!( response.content_encoding().unwrap(), @@ -287,8 +264,6 @@ pub mod tests { #[test] fn test_uncompress_body() { let response = Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Encoding".to_string(), value: "br".to_string(), @@ -297,15 +272,11 @@ pub mod tests { 0x21, 0x2c, 0x00, 0x04, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x03, ], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; assert_eq!(response.uncompress_body().unwrap(), b"Hello World!"); let response = Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Encoding".to_string(), value: "br, identity".to_string(), @@ -314,20 +285,13 @@ pub mod tests { 0x21, 0x2c, 0x00, 0x04, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x03, ], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; assert_eq!(response.uncompress_body().unwrap(), b"Hello World!"); let response = Response { - version: Version::Http10, - status: 200, - headers: vec![], body: b"Hello World!".to_vec(), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; assert_eq!(response.uncompress_body().unwrap(), b"Hello World!"); } @@ -379,43 +343,30 @@ pub mod tests { fn hello_response() -> Response { Response { - version: Version::Http10, - status: 200, - headers: vec![], body: b"Hello World!".to_vec(), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } fn utf8_encoding_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Type".to_string(), value: "text/plain; charset=utf-8".to_string(), }], body: vec![0x63, 0x61, 0x66, 0xc3, 0xa9], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } fn latin1_encoding_response() -> Response { Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Type".to_string(), value: "text/plain; charset=ISO-8859-1".to_string(), }], body: vec![0x63, 0x61, 0x66, 0xe9], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } @@ -468,16 +419,12 @@ pub mod tests { pub fn test_invalid_charset() { assert_eq!( Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Type".to_string(), value: "test/plain; charset=xxx".to_string() }], body: b"Hello World!".to_vec(), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } .character_encoding() .err() @@ -492,13 +439,8 @@ pub mod tests { pub fn test_invalid_decoding() { assert_eq!( Response { - version: Version::Http10, - status: 200, - headers: vec![], body: vec![0x63, 0x61, 0x66, 0xe9], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } .text() .err() @@ -510,16 +452,12 @@ pub mod tests { assert_eq!( Response { - version: Version::Http10, - status: 200, headers: vec![Header { name: "Content-Type".to_string(), value: "text/plain; charset=ISO-8859-1".to_string() }], body: vec![0x63, 0x61, 0x66, 0xc3, 0xa9], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } .text() .unwrap(), diff --git a/packages/hurl/src/runner/query.rs b/packages/hurl/src/runner/query.rs index f7c2955ff..a8aab128d 100644 --- a/packages/hurl/src/runner/query.rs +++ b/packages/hurl/src/runner/query.rs @@ -387,9 +387,6 @@ pub mod tests { pub fn json_http_response() -> http::Response { http::Response { - version: http::Version::Http10, - status: 0, - headers: vec![], body: String::into_bytes( r#" { @@ -402,9 +399,7 @@ pub mod tests { "# .to_string(), ), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } } @@ -598,18 +593,13 @@ pub mod tests { source_info: SourceInfo::new(0, 0, 0, 0), }; let response = http::Response { - version: http::Version::Http10, - status: 0, headers: vec![ http::Header { name: "Set-Cookie".to_string(), value: "LSID=DQAAAKEaem_vYg; Path=/accounts; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly".to_string(), } ], - body: vec![], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; // cookie "LSID" @@ -835,13 +825,8 @@ pub mod tests { fn test_query_invalid_utf8() { let variables = HashMap::new(); let http_response = http::Response { - version: http::Version::Http10, - status: 0, - headers: vec![], body: vec![200], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; let error = eval_query(&xpath_users(), &variables, &http_response) .err() @@ -992,13 +977,8 @@ pub mod tests { fn test_query_invalid_json() { let variables = HashMap::new(); let http_response = http::Response { - version: http::Version::Http10, - status: 0, - headers: vec![], body: String::into_bytes(String::from("xxx")), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; let error = eval_query(&jsonpath_success(), &variables, &http_response) .err() @@ -1011,13 +991,8 @@ pub mod tests { fn test_query_json_not_found() { let variables = HashMap::new(); let http_response = http::Response { - version: http::Version::Http10, - status: 0, - headers: vec![], body: String::into_bytes(String::from("{}")), - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() }; //assert_eq!(jsonpath_success().eval(http_response).unwrap(), Value::List(vec![])); assert_eq!( @@ -1098,13 +1073,8 @@ pub mod tests { }, &variables, &http::Response { - version: http::Version::Http10, - status: 200, - headers: vec![], body: vec![0xff], - duration: Default::default(), - url: "".to_string(), - certificate: None, + ..Default::default() } ) .unwrap()