From 15a15c123e4c69292bf7bb35613cc02b3f9e21f1 Mon Sep 17 00:00:00 2001 From: Gurpreet Singh Date: Wed, 7 Jun 2023 10:08:21 -0700 Subject: [PATCH] Add a check for empty params for `url join` (#9356) # Description Fix for #9347 # User-Facing Changes # Tests + Formatting - [x] Add unit tests - [x] Run all cargo tests + fmt commands # After Submitting --- crates/nu-command/src/network/url/join.rs | 6 +++++- crates/nu-command/tests/commands/url/join.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/network/url/join.rs b/crates/nu-command/src/network/url/join.rs index 8e1a745499..46ecc740c2 100644 --- a/crates/nu-command/src/network/url/join.rs +++ b/crates/nu-command/src/network/url/join.rs @@ -191,7 +191,11 @@ impl UrlComponents { .collect::, ShellError>>()? .join("&"); - qs = format!("?{qs}"); + qs = if !qs.trim().is_empty() { + format!("?{qs}") + } else { + qs + }; if let Some(q) = self.query { if q != qs { diff --git a/crates/nu-command/tests/commands/url/join.rs b/crates/nu-command/tests/commands/url/join.rs index cd10916ffb..5198228f0e 100644 --- a/crates/nu-command/tests/commands/url/join.rs +++ b/crates/nu-command/tests/commands/url/join.rs @@ -366,3 +366,21 @@ fn url_join_with_fragment_and_params() { "http://usr:pwd@localhost:1234?par_1=aaa&par_2=bbb#frag" ); } + +#[test] +fn url_join_with_empty_params() { + let actual = nu!( + cwd: ".", pipeline( + r#" + { + "scheme": "https", + "host": "localhost", + "path": "/foo", + "params": {} + } | url join + "# + ) + ); + + assert_eq!(actual.out, "https://localhost/foo"); +}