From 9a3bce52947f616c0d14da8c752f0908ebf005e6 Mon Sep 17 00:00:00 2001 From: jcamiel Date: Mon, 13 Feb 2023 18:04:12 +0100 Subject: [PATCH] Implements Default trait for RequestSpec. --- packages/hurl/src/http/client.rs | 8 +- packages/hurl/src/http/request_spec.rs | 42 +++--- packages/hurl/tests/libcurl.rs | 174 ++++++------------------- 3 files changed, 61 insertions(+), 163 deletions(-) diff --git a/packages/hurl/src/http/client.rs b/packages/hurl/src/http/client.rs index f537bb48a..a98f13575 100644 --- a/packages/hurl/src/http/client.rs +++ b/packages/hurl/src/http/client.rs @@ -102,13 +102,7 @@ impl Client { request_spec = RequestSpec { method: Method::Get, url: redirect_url, - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; } Ok(calls) diff --git a/packages/hurl/src/http/request_spec.rs b/packages/hurl/src/http/request_spec.rs index 8f9a9fbeb..9c139202b 100644 --- a/packages/hurl/src/http/request_spec.rs +++ b/packages/hurl/src/http/request_spec.rs @@ -34,6 +34,22 @@ pub struct RequestSpec { pub content_type: Option, } +impl Default for RequestSpec { + fn default() -> Self { + RequestSpec { + method: Method::Get, + url: "".to_string(), + headers: vec![], + querystring: vec![], + form: vec![], + multipart: vec![], + cookies: vec![], + body: Body::Binary(vec![]), + content_type: None, + } + } +} + #[derive(Clone, Debug, PartialEq, Eq)] pub enum Method { Get, @@ -143,13 +159,7 @@ pub mod tests { RequestSpec { method: Method::Get, url: "http://localhost:8000/hello".to_string(), - querystring: vec![], - headers: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - multipart: vec![], - form: vec![], - content_type: None, + ..Default::default() } } @@ -157,7 +167,6 @@ pub mod tests { RequestSpec { method: Method::Get, url: "http://localhost/custom".to_string(), - querystring: vec![], headers: vec![ Header { name: String::from("User-Agent"), @@ -178,10 +187,7 @@ pub mod tests { value: String::from("abc123"), }, ], - body: Body::Binary(vec![]), - multipart: vec![], - form: vec![], - content_type: None, + ..Default::default() } } @@ -199,12 +205,7 @@ pub mod tests { value: String::from("a b"), }, ], - headers: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - multipart: vec![], - form: vec![], - content_type: None, + ..Default::default() } } @@ -212,14 +213,10 @@ pub mod tests { RequestSpec { method: Method::Post, url: "http://localhost/form-params".to_string(), - querystring: vec![], headers: vec![Header { name: String::from("Content-Type"), value: String::from("application/x-www-form-urlencoded"), }], - cookies: vec![], - body: Body::Binary(vec![]), - multipart: vec![], form: vec![ Param { name: String::from("param1"), @@ -231,6 +228,7 @@ pub mod tests { }, ], content_type: Some("multipart/form-data".to_string()), + ..Default::default() } } } diff --git a/packages/hurl/tests/libcurl.rs b/packages/hurl/tests/libcurl.rs index 49450302f..1992966db 100644 --- a/packages/hurl/tests/libcurl.rs +++ b/packages/hurl/tests/libcurl.rs @@ -29,17 +29,10 @@ fn new_header(name: &str, value: &str) -> Header { } } -fn default_get_request(url: &str) -> RequestSpec { +fn default_get_(url: &str) -> RequestSpec { RequestSpec { - method: Method::Get, url: url.to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() } } @@ -58,7 +51,7 @@ fn test_hello() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://localhost:8000/hello"); + let request_spec = default_get_("http://localhost:8000/hello"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl 'http://localhost:8000/hello'".to_string() @@ -103,13 +96,7 @@ fn test_put() { let request_spec = RequestSpec { method: Method::Put, url: "http://localhost:8000/put".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), @@ -156,12 +143,7 @@ fn test_patch() { value: "\"e0023aa4e\"".to_string(), }, ], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), @@ -205,12 +187,7 @@ fn test_custom_headers() { new_header("Fruit", "Grape"), new_header("Color", "Green"), ], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; assert!(options.curl_args().is_empty()); assert_eq!( @@ -243,7 +220,6 @@ fn test_querystring_params() { let request_spec = RequestSpec { method: Method::Get, url: "http://localhost:8000/querystring-params".to_string(), - headers: vec![], querystring: vec![ Param { name: "param1".to_string(), @@ -266,11 +242,7 @@ fn test_querystring_params() { value: "5".to_string(), }, ], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), @@ -296,8 +268,6 @@ fn test_form_params() { let request_spec = RequestSpec { method: Method::Post, url: "http://localhost:8000/form-params".to_string(), - headers: vec![], - querystring: vec![], form: vec![ Param { name: "param1".to_string(), @@ -324,10 +294,8 @@ fn test_form_params() { value: "1".to_string(), }, ], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), content_type: Some("application/x-www-form-urlencoded".to_string()), + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), @@ -346,7 +314,7 @@ fn test_form_params() { assert!(response.body.is_empty()); // make sure you can reuse client for other request - let request = default_get_request("http://localhost:8000/hello"); + let request = default_get_("http://localhost:8000/hello"); let (request, response) = client.execute(&request, &options, &logger).unwrap(); assert_eq!(request.method, "GET".to_string()); assert_eq!(request.url, "http://localhost:8000/hello".to_string()); @@ -357,7 +325,7 @@ fn test_form_params() { #[test] fn test_redirect() { - let request_spec = default_get_request("http://localhost:8000/redirect-absolute"); + let request_spec = default_get_("http://localhost:8000/redirect-absolute"); let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); @@ -384,7 +352,7 @@ fn test_redirect() { #[test] fn test_follow_location() { - let request_spec = default_get_request("http://localhost:8000/redirect-absolute"); + let request_spec = default_get_("http://localhost:8000/redirect-absolute"); let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); @@ -432,7 +400,7 @@ fn test_follow_location() { "http://localhost:8000/redirected".to_string() ); - let request = default_get_request("http://localhost:8000/hello"); + let request = default_get_("http://localhost:8000/hello"); let calls = client .execute_with_redirect(&request, &options, &logger) .unwrap(); @@ -454,7 +422,7 @@ fn test_max_redirect() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://localhost:8000/redirect/15"); + let request_spec = default_get_("http://localhost:8000/redirect/15"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl --location --max-redirs 10 'http://localhost:8000/redirect/15'".to_string() @@ -465,7 +433,7 @@ fn test_max_redirect() { .unwrap(); assert_eq!(error, HttpError::TooManyRedirect); - let request_spec = default_get_request("http://localhost:8000/redirect/8"); + let request_spec = default_get_("http://localhost:8000/redirect/8"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl --location --max-redirs 10 'http://localhost:8000/redirect/8'".to_string() @@ -490,9 +458,6 @@ fn test_multipart_form_data() { let request_spec = RequestSpec { method: Method::Post, url: "http://localhost:8000/multipart-form-data".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], multipart: vec![ MultipartParam::Param(Param { name: "key1".to_string(), @@ -517,9 +482,8 @@ fn test_multipart_form_data() { content_type: "text/html".to_string(), }), ], - cookies: vec![], - body: Body::Binary(vec![]), content_type: Some("multipart/form-data".to_string()), + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), @@ -535,7 +499,7 @@ fn test_multipart_form_data() { assert!(response.body.is_empty()); // make sure you can reuse client for other request - let request_spec = default_get_request("http://localhost:8000/hello"); + let request_spec = default_get_("http://localhost:8000/hello"); let (request, response) = client.execute(&request_spec, &options, &logger).unwrap(); assert_eq!(request.method, "GET".to_string()); assert_eq!(response.status, 200); @@ -623,17 +587,7 @@ fn test_basic_authentication() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = RequestSpec { - method: Method::Get, - url: "http://localhost:8000/basic-authentication".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, - }; + let request_spec = default_get_("http://localhost:8000/basic-authentication"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl --user 'bob@email.com:secret' 'http://localhost:8000/basic-authentication'" @@ -650,17 +604,8 @@ fn test_basic_authentication() { let options = ClientOptions::default(); let mut client = Client::new(None); - let request_spec = RequestSpec { - method: Method::Get, - url: "http://bob%40email.com:secret@localhost:8000/basic-authentication".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, - }; + let request_spec = + default_get_("http://bob%40email.com:secret@localhost:8000/basic-authentication"); assert_eq!( request_spec.curl_args(&ContextDir::default()), vec!["'http://bob%40email.com:secret@localhost:8000/basic-authentication'".to_string()] @@ -685,7 +630,7 @@ fn test_cacert() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("https://localhost:8001/hello"); + let request_spec = default_get_("https://localhost:8001/hello"); let (_, response) = client.execute(&request_spec, &options, &logger).unwrap(); assert_eq!(response.status, 200); } @@ -697,7 +642,7 @@ fn test_error_could_not_resolve_host() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://unknown"); + let request_spec = default_get_("http://unknown"); let error = client .execute(&request_spec, &options, &logger) .err() @@ -722,7 +667,7 @@ fn test_error_fail_to_connect() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://localhost:9999"); + let request_spec = default_get_("http://localhost:9999"); let error = client .execute(&request_spec, &options, &logger) .err() @@ -744,7 +689,7 @@ fn test_error_fail_to_connect() { ..Default::default() }; let mut client = Client::new(None); - let request = default_get_request("http://localhost:8000/hello"); + let request = default_get_("http://localhost:8000/hello"); let error = client.execute(&request, &options, &logger).err().unwrap(); assert!(matches!(error, HttpError::Libcurl { .. })); if let HttpError::Libcurl { @@ -770,7 +715,7 @@ fn test_error_could_not_resolve_proxy_name() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://localhost:8000/hello"); + let request_spec = default_get_("http://localhost:8000/hello"); let error = client .execute(&request_spec, &options, &logger) .err() @@ -795,7 +740,7 @@ fn test_error_ssl() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("https://localhost:8001/hello"); + let request_spec = default_get_("https://localhost:8001/hello"); let error = client .execute(&request_spec, &options, &logger) .err() @@ -839,7 +784,7 @@ fn test_timeout() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://localhost:8000/timeout"); + let request_spec = default_get_("http://localhost:8000/timeout"); let error = client .execute(&request_spec, &options, &logger) .err() @@ -866,18 +811,7 @@ fn test_accept_encoding() { let mut client = Client::new(None); let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - - let request_spec = RequestSpec { - method: Method::Get, - url: "http://localhost:8000/compressed/gzip".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, - }; + let request_spec = default_get_("http://localhost:8000/compressed/gzip"); let (request, response) = client.execute(&request_spec, &options, &logger).unwrap(); assert!(request.headers.contains(&Header { name: "Accept-Encoding".to_string(), @@ -901,7 +835,7 @@ fn test_connect_timeout() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://10.0.0.0"); + let request_spec = default_get_("http://10.0.0.0"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl --connect-timeout 1 'http://10.0.0.0'".to_string() @@ -945,16 +879,11 @@ fn test_cookie() { let request_spec = RequestSpec { method: Method::Get, url: "http://localhost:8000/cookies/set-request-cookie1-valueA".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], cookies: vec![RequestCookie { name: "cookie1".to_string(), value: "valueA".to_string(), }], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), @@ -972,23 +901,14 @@ fn test_cookie() { assert_eq!(response.status, 200); assert!(response.body.is_empty()); - let request_spec = RequestSpec { - method: Method::Get, - url: "http://localhost:8000/cookies/assert-that-cookie1-is-not-in-session".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, - }; - let (_request, response) = client.execute(&request_spec, &options, &logger).unwrap(); + let request_spec = + default_get_("http://localhost:8000/cookies/assert-that-cookie1-is-not-in-session"); + let (_, response) = client.execute(&request_spec, &options, &logger).unwrap(); assert_eq!(response.status, 200); } #[test] -fn test_multiple_request_cookies() { +fn test_multiple__cookies() { let options = ClientOptions::default(); let context_dir = ContextDir::default(); let mut client = Client::new(None); @@ -998,10 +918,6 @@ fn test_multiple_request_cookies() { let request_spec = RequestSpec { method: Method::Get, url: "http://localhost:8000/cookies/set-multiple-request-cookies".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], cookies: vec![ RequestCookie { name: "user1".to_string(), @@ -1016,8 +932,7 @@ fn test_multiple_request_cookies() { value: "Bruce".to_string(), }, ], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), @@ -1040,8 +955,7 @@ fn test_cookie_storage() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = - default_get_request("http://localhost:8000/cookies/set-session-cookie2-valueA"); + let request_spec = default_get_("http://localhost:8000/cookies/set-session-cookie2-valueA"); let (request, response) = client.execute(&request_spec, &options, &logger).unwrap(); assert_eq!( request.url, @@ -1065,8 +979,7 @@ fn test_cookie_storage() { } ); - let request_spec = - default_get_request("http://localhost:8000/cookies/assert-that-cookie2-is-valueA"); + let request_spec = default_get_("http://localhost:8000/cookies/assert-that-cookie2-is-valueA"); let (request, response) = client.execute(&request_spec, &options, &logger).unwrap(); assert!(request.headers.contains(&Header { name: "Cookie".to_string(), @@ -1087,8 +1000,7 @@ fn test_cookie_file() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = - default_get_request("http://localhost:8000/cookies/assert-that-cookie2-is-valueA"); + let request_spec = default_get_("http://localhost:8000/cookies/assert-that-cookie2-is-valueA"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl --cookie tests/cookies.txt 'http://localhost:8000/cookies/assert-that-cookie2-is-valueA'".to_string() @@ -1120,7 +1032,7 @@ fn test_proxy() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); - let request_spec = default_get_request("http://localhost:8000/proxy"); + let request_spec = default_get_("http://localhost:8000/proxy"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl --proxy 'localhost:8888' 'http://localhost:8000/proxy'".to_string() @@ -1141,7 +1053,7 @@ fn test_insecure() { let mut builder = LoggerBuilder::new(); let logger = builder.filename("").content("").build().unwrap(); assert_eq!(options.curl_args(), vec!["--insecure".to_string()]); - let request_spec = default_get_request("https://localhost:8001/hello"); + let request_spec = default_get_("https://localhost:8001/hello"); assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options), "curl --insecure 'https://localhost:8001/hello'".to_string() @@ -1165,13 +1077,7 @@ fn test_head() { let request_spec = RequestSpec { method: Method::Head, url: "http://localhost:8000/head".to_string(), - headers: vec![], - querystring: vec![], - form: vec![], - multipart: vec![], - cookies: vec![], - body: Body::Binary(vec![]), - content_type: None, + ..Default::default() }; assert_eq!( client.curl_command_line(&request_spec, &context_dir, &options),