nushell/crates/nu-command/tests/commands/url/join.rs
ellis 3e0fa8ff85
Allow 'url join' to print username without password (#11697)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
Fixes: https://github.com/nushell/nushell/issues/11677
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
```
'https://example.com' | url parse | update scheme ssh | upda
te username user | url join
# => ssh://user@example.com/
'https://example.com' | url parse | update scheme ssh | upda
te password hackme | url join
# => ssh://example.com/
'https://example.com' | url parse | update scheme ssh | update username user | update password hackme | url join
# => ssh://user:hackme@example.com/
```
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: Richard Westhaver <ellis@rwest.io>
2024-01-31 16:52:23 -06:00

349 lines
8.3 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn url_join_simple() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "",
"password": "",
"host": "localhost",
"port": "",
} | url join
"#
));
assert_eq!(actual.out, "http://localhost");
}
#[test]
fn url_join_with_only_user() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "",
"host": "localhost",
"port": "",
} | url join
"#
));
assert_eq!(actual.out, "http://usr@localhost");
}
#[test]
fn url_join_with_only_pwd() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "",
"password": "pwd",
"host": "localhost",
"port": "",
} | url join
"#
));
assert_eq!(actual.out, "http://localhost");
}
#[test]
fn url_join_with_user_and_pwd() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"port": "",
} | url join
"#
));
assert_eq!(actual.out, "http://usr:pwd@localhost");
}
#[test]
fn url_join_with_query() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"query": "par_1=aaa&par_2=bbb"
"port": "",
} | url join
"#
));
assert_eq!(actual.out, "http://usr:pwd@localhost?par_1=aaa&par_2=bbb");
}
#[test]
fn url_join_with_params() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"params": {
"par_1": "aaa",
"par_2": "bbb"
},
"port": "1234",
} | url join
"#
));
assert_eq!(
actual.out,
"http://usr:pwd@localhost:1234?par_1=aaa&par_2=bbb"
);
}
#[test]
fn url_join_with_same_query_and_params() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"query": "par_1=aaa&par_2=bbb",
"params": {
"par_1": "aaa",
"par_2": "bbb"
},
"port": "1234",
} | url join
"#
));
assert_eq!(
actual.out,
"http://usr:pwd@localhost:1234?par_1=aaa&par_2=bbb"
);
}
#[test]
fn url_join_with_different_query_and_params() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"query": "par_1=aaa&par_2=bbb",
"params": {
"par_1": "aaab",
"par_2": "bbb"
},
"port": "1234",
} | url join
"#
));
assert!(actual
.err
.contains("Mismatch, qs from params is: ?par_1=aaab&par_2=bbb"));
assert!(actual
.err
.contains("instead query is: ?par_1=aaa&par_2=bbb"));
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"params": {
"par_1": "aaab",
"par_2": "bbb"
},
"query": "par_1=aaa&par_2=bbb",
"port": "1234",
} | url join
"#
));
assert!(actual
.err
.contains("Mismatch, query param is: par_1=aaa&par_2=bbb"));
assert!(actual
.err
.contains("instead qs from params is: ?par_1=aaab&par_2=bbb"));
}
#[test]
fn url_join_with_invalid_params() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"params": "aaa",
"port": "1234",
} | url join
"#
));
assert!(actual.err.contains("Key params has to be a record"));
}
#[test]
fn url_join_with_port() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"host": "localhost",
"port": "1234",
} | url join
"#
));
assert_eq!(actual.out, "http://localhost:1234");
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"host": "localhost",
"port": 1234,
} | url join
"#
));
assert_eq!(actual.out, "http://localhost:1234");
}
#[test]
fn url_join_with_invalid_port() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"host": "localhost",
"port": "aaaa",
} | url join
"#
));
assert!(actual
.err
.contains("Port parameter should represent an unsigned int"));
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"host": "localhost",
"port": [],
} | url join
"#
));
assert!(actual
.err
.contains("Port parameter should be an unsigned int or a string representing it"));
}
#[test]
fn url_join_with_missing_scheme() {
let actual = nu!(pipeline(
r#"
{
"host": "localhost"
} | url join
"#
));
assert!(actual.err.contains("missing parameter: scheme"));
}
#[test]
fn url_join_with_missing_host() {
let actual = nu!(pipeline(
r#"
{
"scheme": "https"
} | url join
"#
));
assert!(actual.err.contains("missing parameter: host"));
}
#[test]
fn url_join_with_fragment() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"fragment": "frag",
"port": "1234",
} | url join
"#
));
assert_eq!(actual.out, "http://usr:pwd@localhost:1234#frag");
}
#[test]
fn url_join_with_fragment_and_params() {
let actual = nu!(pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "pwd",
"host": "localhost",
"params": {
"par_1": "aaa",
"par_2": "bbb"
},
"port": "1234",
"fragment": "frag"
} | url join
"#
));
assert_eq!(
actual.out,
"http://usr:pwd@localhost:1234?par_1=aaa&par_2=bbb#frag"
);
}
#[test]
fn url_join_with_empty_params() {
let actual = nu!(pipeline(
r#"
{
"scheme": "https",
"host": "localhost",
"path": "/foo",
"params": {}
} | url join
"#
));
assert_eq!(actual.out, "https://localhost/foo");
}