Skip to content

Commit

Permalink
test: validate new command saves correctly (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Dec 29, 2023
1 parent d6aa145 commit 038e347
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
50 changes: 46 additions & 4 deletions hitt-cli/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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? {
Expand Down
24 changes: 14 additions & 10 deletions hitt-cli/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());

Expand Down
4 changes: 3 additions & 1 deletion hitt-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 4 additions & 2 deletions hitt-request/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 038e347

Please sign in to comment.