Attempt to guess the content type of a file when opening with --raw (#13521)

# Description
Attempt to guess the content type of a file when opening with --raw and
set it in the pipeline metadata.

<img width="644" alt="Screenshot 2024-08-02 at 11 30 10"
src="https://github.com/user-attachments/assets/071f0967-c4dd-405a-b8c8-f7aa073efa98">


# User-Facing Changes
- Content of files can be directly piped into commands like `http post`
with the content type set appropriately when using `--raw`.
This commit is contained in:
Jack Wright
2024-08-06 02:36:24 -07:00
committed by GitHub
parent 4e83ccdf86
commit 73e8de9753
5 changed files with 70 additions and 6 deletions

View File

@ -379,3 +379,27 @@ fn open_files_inside_glob_metachars_dir() {
assert!(actual.out.contains("hello"));
});
}
#[test]
fn test_content_types_with_open_raw() {
Playground::setup("open_files_content_type_test", |dirs, _| {
let result = nu!(cwd: dirs.formats(), "open --raw random_numbers.csv | metadata");
assert!(result.out.contains("text/csv"));
let result = nu!(cwd: dirs.formats(), "open --raw caco3_plastics.tsv | metadata");
assert!(result.out.contains("text/tab-separated-values"));
let result = nu!(cwd: dirs.formats(), "open --raw sample-simple.json | metadata");
assert!(result.out.contains("application/json"));
let result = nu!(cwd: dirs.formats(), "open --raw sample.ini | metadata");
assert!(result.out.contains("text/plain"));
let result = nu!(cwd: dirs.formats(), "open --raw sample_data.xlsx | metadata");
assert!(result.out.contains("vnd.openxmlformats-officedocument"));
let result = nu!(cwd: dirs.formats(), "open --raw sample_def.nu | metadata");
assert!(!result.out.contains("content_type"));
let result = nu!(cwd: dirs.formats(), "open --raw sample.eml | metadata");
assert!(result.out.contains("message/rfc822"));
let result = nu!(cwd: dirs.formats(), "open --raw cargo_sample.toml | metadata");
assert!(result.out.contains("text/x-toml"));
let result = nu!(cwd: dirs.formats(), "open --raw appveyor.yml | metadata");
assert!(result.out.contains("application/yaml"));
})
}