diff --git a/hitt-cli/src/commands/new.rs b/hitt-cli/src/commands/new.rs index 12485f6..798270a 100644 --- a/hitt-cli/src/commands/new.rs +++ b/hitt-cli/src/commands/new.rs @@ -112,20 +112,20 @@ mod test_try_find_content_type { let headers = vec![("content-type".to_owned(), content_type.to_owned())]; assert_eq!(Some(content_type), try_find_content_type(&headers)); - } + }; { let content_type = "application/JSON".to_lowercase(); - let headers = vec![("content-type".to_owned(), content_type.to_owned())]; + let headers = vec![("content-type".to_owned(), content_type.clone())]; assert_eq!(Some(content_type.as_str()), try_find_content_type(&headers)); - } + }; { let content_type = "application/JSON".to_uppercase(); - let headers = vec![("content-type".to_owned(), content_type.to_owned())]; + let headers = vec![("content-type".to_owned(), content_type.clone())]; assert_eq!(Some(content_type.as_str()), try_find_content_type(&headers)); } @@ -168,6 +168,48 @@ fn save_request( std::fs::write(path, contents).map_err(|error| HittCliError::IoWrite(path.to_path_buf(), error)) } +#[cfg(test)] +mod test_save_request { + use super::save_request; + + #[test] + fn it_should_save_request_input() { + let file = tempfile::Builder::new() + .prefix("test-hitt-") + .suffix(".http") + .rand_bytes(12) + .tempfile() + .expect("it to return a file path"); + + let method = "GET"; + let url = "https://mhouge.dk/"; + let headers = vec![ + ("x-key1".to_owned(), "x-value1".to_owned()), + ("x-key2".to_owned(), "x-value2".to_owned()), + ]; + + let body = "{ + \"key\": \"value\" +}"; + + let expected_result = format!( + "{method} {url} +x-key1: x-value1 +x-key2: x-value2 + +{body} +" + ); + + save_request(file.path(), method, url, &headers, Some(body.to_owned())) + .expect("it to save succesfully"); + + let result = std::fs::read_to_string(file.path()).expect("it to read the string"); + + assert_eq!(result, expected_result); + } +} + #[inline] async fn check_if_exist(term: &Term, path: &std::path::Path) -> Result<(), std::io::Error> { if tokio::fs::try_exists(path).await? { diff --git a/hitt-cli/src/fs/mod.rs b/hitt-cli/src/fs/mod.rs index 26f59c4..bc205c3 100644 --- a/hitt-cli/src/fs/mod.rs +++ b/hitt-cli/src/fs/mod.rs @@ -89,11 +89,11 @@ mod test_find_http_files { .tempdir() .expect("it to return a valid dir"); - std::fs::create_dir_all(dir.path().join("nested")).unwrap(); + std::fs::create_dir_all(dir.path().join("nested")).expect("it to create dir"); - std::fs::File::create(dir.path().join("nested/file1.http")).unwrap(); + std::fs::File::create(dir.path().join("nested/file1.http")).expect("it to create a file"); - std::fs::File::create(dir.path().join("nested/file2.http")).unwrap(); + std::fs::File::create(dir.path().join("nested/file2.http")).expect("it to create a file"); let result = find_http_files(dir.path()); @@ -108,22 +108,26 @@ mod test_find_http_files { .tempdir() .expect("it to return a valid dir"); - std::fs::create_dir_all(dir.path().join("ignored_folder")).unwrap(); + std::fs::create_dir_all(dir.path().join("ignored_folder")) + .expect("it to create directories"); - std::fs::File::create(dir.path().join("not-ignored-file.http")).unwrap(); + std::fs::File::create(dir.path().join("not-ignored-file.http")) + .expect("it to create the file"); - std::fs::File::create(dir.path().join("ignored_folder/file1.http")).unwrap(); + std::fs::File::create(dir.path().join("ignored_folder/file1.http")) + .expect("it to create the file"); - std::fs::File::create(dir.path().join("ignored_folder/file2.http")).unwrap(); + std::fs::File::create(dir.path().join("ignored_folder/file2.http")) + .expect("it to create a file"); let gitignore = " ignored_folder "; std::fs::File::create(dir.path().join(".gitignore")) - .unwrap() - .write(gitignore.as_bytes()) - .unwrap(); + .expect("it to create .gitignore") + .write_all(gitignore.as_bytes()) + .expect("it to write to .gitignore"); let result = find_http_files(dir.path()); diff --git a/hitt-parser/src/lib.rs b/hitt-parser/src/lib.rs index 8bfd65d..9aa10cf 100644 --- a/hitt-parser/src/lib.rs +++ b/hitt-parser/src/lib.rs @@ -448,8 +448,10 @@ mod test_partial_http_request { #[test] fn build_should_reject_if_no_method() { + let uri = Uri::from_str("https://mhouge.dk/").expect("it to be a valid url"); + let request = PartialHittRequest { - uri: Some(Uri::from_str("https://mhouge.dk/").unwrap()), + uri: Some(uri), method: None, http_version: None, headers: HeaderMap::default(), diff --git a/hitt-request/src/lib.rs b/hitt-request/src/lib.rs index aeafa95..d60d91f 100644 --- a/hitt-request/src/lib.rs +++ b/hitt-request/src/lib.rs @@ -72,7 +72,8 @@ mod test_send_request { let timeout = None; - let uri = http::Uri::from_str("https://dummyjson.com/products/1").unwrap(); + let uri = + http::Uri::from_str("https://dummyjson.com/products/1").expect("it to be a valid uri"); let input = hitt_parser::HittRequest { method: http::Method::GET, @@ -97,7 +98,8 @@ mod test_send_request { let timeout = Some(Duration::from_millis(5)); - let uri = http::Uri::from_str("https://dummyjson.com/products/1").unwrap(); + let uri = + http::Uri::from_str("https://dummyjson.com/products/1").expect("it to be a valid uri"); let input = hitt_parser::HittRequest { method: http::Method::GET,