forked from extern/nushell
ec5396a352
# 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`). ![image](https://user-images.githubusercontent.com/3835355/227048504-6686445d-ad2e-4f5d-905d-e71b3a4b81a6.png) *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. ![image](https://user-images.githubusercontent.com/3835355/227047790-b9f5a25f-2c0d-4741-881f-4189b23e4ef6.png) *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
142 lines
3.1 KiB
Rust
142 lines
3.1 KiB
Rust
use mockito::Server;
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
#[test]
|
|
fn http_get_is_success() {
|
|
let mut server = Server::new();
|
|
|
|
let _mock = server.mock("GET", "/").with_body("foo").create();
|
|
|
|
let actual = nu!(pipeline(
|
|
format!(
|
|
r#"
|
|
http get {url}
|
|
"#,
|
|
url = server.url()
|
|
)
|
|
.as_str()
|
|
));
|
|
|
|
assert_eq!(actual.out, "foo")
|
|
}
|
|
|
|
#[test]
|
|
fn http_get_failed_due_to_server_error() {
|
|
let mut server = Server::new();
|
|
|
|
let _mock = server.mock("GET", "/").with_status(400).create();
|
|
|
|
let actual = nu!(pipeline(
|
|
format!(
|
|
r#"
|
|
http get {url}
|
|
"#,
|
|
url = server.url()
|
|
)
|
|
.as_str()
|
|
));
|
|
|
|
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.
|
|
|
|
#[test]
|
|
fn http_get_expired_cert_fails() {
|
|
let actual = nu!("http get https://expired.badssl.com/");
|
|
assert!(actual.err.contains("network_failure"));
|
|
}
|
|
|
|
#[test]
|
|
fn http_get_expired_cert_override() {
|
|
let actual = nu!("http get --insecure https://expired.badssl.com/");
|
|
assert!(actual.out.contains("<html>"));
|
|
}
|
|
|
|
#[test]
|
|
fn http_get_self_signed_fails() {
|
|
let actual = nu!("http get https://self-signed.badssl.com/");
|
|
assert!(actual.err.contains("network_failure"));
|
|
}
|
|
|
|
#[test]
|
|
fn http_get_self_signed_override() {
|
|
let actual = nu!("http get --insecure https://self-signed.badssl.com/");
|
|
assert!(actual.out.contains("<html>"));
|
|
}
|