nushell/crates/nu-command/tests/commands/url/join.rs
Vincenzo Carlino 8d5165c449
Feat/7725 url join (#7823)
# Description

Added command: `url join`.
Closes: #7725 

# User-Facing Changes

```
Converts a record to url

Search terms: scheme, username, password, hostname, port, path, query, fragment

Usage:
  > url join

Flags:
  -h, --help - Display the help message for this command

Signatures:
  <record> | url join -> <string>

Examples:
  Outputs a url representing the contents of this record
  > {
          "scheme": "http",
          "username": "",
          "password": "",
          "host": "www.pixiv.net",
          "port": "",
          "path": "/member_illust.php",
          "query": "mode=medium&illust_id=99260204",
          "fragment": "",
          "params":
          {
            "mode": "medium",
            "illust_id": "99260204"
          }
        } | url join

  Outputs a url representing the contents of this record
  > {
        "scheme": "http",
        "username": "user",
        "password": "pwd",
        "host": "www.pixiv.net",
        "port": "1234",
        "query": "test=a",
        "fragment": ""
      } | url join

  Outputs a url representing the contents of this record
  > {
        "scheme": "http",
        "host": "www.pixiv.net",
        "port": "1234",
        "path": "user",
        "fragment": "frag"
      } | url join
```                  

# Questions
- Which parameters should be required? Currenlty are: `scheme` and
`host`.
2023-01-22 19:49:40 +01:00

368 lines
8.5 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn url_join_simple() {
let actual = nu!(
cwd: ".", 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!(
cwd: ".", pipeline(
r#"
{
"scheme": "http",
"username": "usr",
"password": "",
"host": "localhost",
"port": "",
} | url join
"#
)
);
assert_eq!(actual.out, "http://localhost");
}
#[test]
fn url_join_with_only_pwd() {
let actual = nu!(
cwd: ".", 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!(
cwd: ".", 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!(
cwd: ".", 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!(
cwd: ".", 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!(
cwd: ".", 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!(
cwd: ".", 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!(
cwd: ".", 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!(
cwd: ".", 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!(
cwd: ".", pipeline(
r#"
{
"scheme": "http",
"host": "localhost",
"port": "1234",
} | url join
"#
)
);
assert_eq!(actual.out, "http://localhost:1234");
let actual = nu!(
cwd: ".", 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!(
cwd: ".", pipeline(
r#"
{
"scheme": "http",
"host": "localhost",
"port": "aaaa",
} | url join
"#
)
);
assert!(actual
.err
.contains("Port parameter should represent an unsigned integer"));
let actual = nu!(
cwd: ".", pipeline(
r#"
{
"scheme": "http",
"host": "localhost",
"port": [],
} | url join
"#
)
);
assert!(actual
.err
.contains("Port parameter should be an unsigned integer or a string representing it"));
}
#[test]
fn url_join_with_missing_scheme() {
let actual = nu!(
cwd: ".", pipeline(
r#"
{
"host": "localhost"
} | url join
"#
)
);
assert!(actual.err.contains("missing parameter: scheme"));
}
#[test]
fn url_join_with_missing_host() {
let actual = nu!(
cwd: ".", pipeline(
r#"
{
"scheme": "https"
} | url join
"#
)
);
assert!(actual.err.contains("missing parameter: host"));
}
#[test]
fn url_join_with_fragment() {
let actual = nu!(
cwd: ".", 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!(
cwd: ".", 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"
);
}