mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
feat: added multiple options to http commands (#8571)
# Description All `http` commands now have a `-f` flag which for now contains the `headers`, `body` and `status` fields (we can later add stuff like `is-redirect` or `cookies`).  *Try it yourself* ``` http get http://mockbin.org/bin/630069dc-2c09-483a-a484-672561b7de14 http get -f http://mockbin.org/bin/630069dc-2c09-483a-a484-672561b7de14 ``` The `http` commands can also now use the `-e` flag, which stands for `--allow-errors`. When the status code is `>= 400`, it will still allow you to interpret it like a normal response.  *Try it yourself* ``` http get http://mockbin.org/bin/2ebd3d27-bdc2-4ee8-b042-0bc2c0d1ad2a # should fail like usual http get -e http://mockbin.org/bin/2ebd3d27-bdc2-4ee8-b042-0bc2c0d1ad2a # will return the body http get -e -f http://mockbin.org/bin/2ebd3d27-bdc2-4ee8-b042-0bc2c0d1ad2a # will let you see the full response ``` # User-Facing Changes - Adds `-f` (`--full`) to all `http` commands - Adds `-e` (--allow-errors) to all `http` commands
This commit is contained in:
@ -39,6 +39,80 @@ fn http_get_failed_due_to_server_error() {
|
||||
assert!(actual.err.contains("Bad request (400)"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_get_with_accept_errors() {
|
||||
let mut server = Server::new();
|
||||
|
||||
let _mock = server
|
||||
.mock("GET", "/")
|
||||
.with_status(400)
|
||||
.with_body("error body")
|
||||
.create();
|
||||
|
||||
let actual = nu!(pipeline(
|
||||
format!(
|
||||
r#"
|
||||
http get -e {url}
|
||||
"#,
|
||||
url = server.url()
|
||||
)
|
||||
.as_str()
|
||||
));
|
||||
|
||||
assert!(actual.out.contains("error body"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_get_with_accept_errors_and_full_raw_response() {
|
||||
let mut server = Server::new();
|
||||
|
||||
let _mock = server
|
||||
.mock("GET", "/")
|
||||
.with_status(400)
|
||||
.with_body("error body")
|
||||
.create();
|
||||
|
||||
let actual = nu!(pipeline(
|
||||
format!(
|
||||
r#"
|
||||
http get -e -f {url} | $"($in.status) => ($in.body)"
|
||||
"#,
|
||||
url = server.url()
|
||||
)
|
||||
.as_str()
|
||||
));
|
||||
|
||||
assert!(actual.out.contains("400 => error body"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_get_with_accept_errors_and_full_json_response() {
|
||||
let mut server = Server::new();
|
||||
|
||||
let _mock = server
|
||||
.mock("GET", "/")
|
||||
.with_status(400)
|
||||
.with_header("content-type", "application/json")
|
||||
.with_body(
|
||||
r#"
|
||||
{"msg": "error body"}
|
||||
"#,
|
||||
)
|
||||
.create();
|
||||
|
||||
let actual = nu!(pipeline(
|
||||
format!(
|
||||
r#"
|
||||
http get -e -f {url} | $"($in.status) => ($in.body.msg)"
|
||||
"#,
|
||||
url = server.url()
|
||||
)
|
||||
.as_str()
|
||||
));
|
||||
|
||||
assert!(actual.out.contains("400 => error body"))
|
||||
}
|
||||
|
||||
// These tests require network access; they use badssl.com which is a Google-affiliated site for testing various SSL errors.
|
||||
// Revisit this if these tests prove to be flaky or unstable.
|
||||
|
||||
|
Reference in New Issue
Block a user