From 8731cb2c3665648dbaffdf3f26e86e7ce8af7b4b Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:47:35 -0500 Subject: [PATCH] tests: add `fetchpost` `--content-type` tests --- tests/test_fetch.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/test_fetch.rs b/tests/test_fetch.rs index fcecea88f..3376bce2f 100644 --- a/tests/test_fetch.rs +++ b/tests/test_fetch.rs @@ -1716,3 +1716,69 @@ fn fetchpost_disk_cache() { // Clean up fs::remove_dir_all(temp_dir).unwrap(); } + +#[test] +fn fetchpost_content_type() { + let wrk = Workdir::new("fetchpost_content_type"); + wrk.create( + "data.csv", + vec![ + svec!["URL", "message"], + svec!["https://httpbin.org/post", "Hello World"], + ], + ); + + // Create template file + wrk.create_from_string("payload.tpl", "Greeting: {{ message }}"); + + // Test plain text content type + let mut cmd = wrk.command("fetchpost"); + cmd.arg("URL") + .arg("--payload-tpl") + .arg("payload.tpl") + .arg("--content-type") + .arg("text/plain") + .arg("--jaq") + .arg(r#"."data""#) + .arg("data.csv"); + + let got = wrk.stdout::(&mut cmd); + assert_eq!(got, "\"\"Greeting: Hello World\"\""); + + // Create JSON template file + wrk.create_from_string( + "jsonpayload.tpl", + r#"{ + "URL": "{{ URL }}", + "Message": "{{ message }}", +}"#, + ); + + // Test custom JSON content type + let mut cmd = wrk.command("fetchpost"); + cmd.arg("URL") + .arg("--payload-tpl") + .arg("jsonpayload.tpl") + .arg("--content-type") + .arg("application/json") + .arg("--jaq") + .arg(r#"."json""#) + .arg("data.csv"); + + let got = wrk.stdout::(&mut cmd); + assert_eq!( + got, + "\"{\n \"URL\": \"https://httpbin.org/post\",\n \"Message\": \"Hello World\",\n}\"" + ); + + // Test form data content type + let mut cmd = wrk.command("fetchpost"); + cmd.arg("URL") + .arg("message") + .arg("--jaq") + .arg(r#"."form""#) + .arg("data.csv"); + + let got = wrk.stdout::(&mut cmd); + assert_eq!(got, "{\"message\":\"Hello World\"}"); +}