From 16151650c55fc6eb5160f3215167f3dcd11db446 Mon Sep 17 00:00:00 2001 From: Pascal Lombard Date: Sat, 6 Feb 2021 09:45:53 +0100 Subject: [PATCH 1/6] - Add head http method. --- src/cli.rs | 3 +++ tests/cli.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index b830d2f3..caad104c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -117,6 +117,7 @@ pub enum Method { PUT, PATCH, DELETE, + HEAD, } impl FromStr for Method { @@ -128,6 +129,7 @@ impl FromStr for Method { "PUT" => Ok(Method::PUT), "PATCH" => Ok(Method::PATCH), "DELETE" => Ok(Method::DELETE), + "HEAD" => Ok(Method::HEAD), method => { return Err(Error::with_description( &format!("unknown http method {}", method), @@ -146,6 +148,7 @@ impl From for reqwest::Method { Method::PUT => reqwest::Method::PUT, Method::PATCH => reqwest::Method::PATCH, Method::DELETE => reqwest::Method::DELETE, + Method::HEAD => reqwest::Method::HEAD, } } } diff --git a/tests/cli.rs b/tests/cli.rs index f6400ae3..4d28765a 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -30,3 +30,25 @@ fn basic_post() -> Result<(), Box> { Ok(()) } + +#[test] +fn basic_head() -> Result<(), Box> { + let mut cmd = Command::cargo_bin("ht")?; + cmd.arg("-v") + .arg("--offline") + .arg("--ignore-stdin") + .arg("--pretty=format") + .arg("head") + .arg("httpbin.org/head"); + + cmd.assert().stdout(indoc! {r#" + HEAD /head HTTP/1.1 + accept: */* + accept-encoding: gzip, deflate + connection: keep-alive + host: httpbin.org + + "#}); + + Ok(()) +} From c56797747f33de61b730f7627295725030430d9c Mon Sep 17 00:00:00 2001 From: Pascal Lombard Date: Sun, 7 Feb 2021 11:26:55 +0100 Subject: [PATCH 2/6] - Add OPTIONS http method. --- src/cli.rs | 6 +++--- tests/cli.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 96b637c5..81829dee 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -126,7 +126,7 @@ pub enum Method { PUT, PATCH, DELETE, - HEAD, + OPTIONS, } impl FromStr for Method { @@ -139,7 +139,7 @@ impl FromStr for Method { "PUT" => Ok(Method::PUT), "PATCH" => Ok(Method::PATCH), "DELETE" => Ok(Method::DELETE), - "HEAD" => Ok(Method::HEAD), + "OPTIONS" => Ok(Method::OPTIONS), method => { return Err(Error::with_description( &format!("unknown http method {}", method), @@ -159,7 +159,7 @@ impl From for reqwest::Method { Method::PUT => reqwest::Method::PUT, Method::PATCH => reqwest::Method::PATCH, Method::DELETE => reqwest::Method::DELETE, - Method::HEAD => reqwest::Method::HEAD, + Method::OPTIONS => reqwest::Method::OPTIONS, } } } diff --git a/tests/cli.rs b/tests/cli.rs index 6832573a..a002bf90 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -74,3 +74,25 @@ fn basic_head() -> Result<(), Box> { Ok(()) } + +#[test] +fn basic_options() -> Result<(), Box> { + let mut cmd = Command::cargo_bin("ht")?; + cmd.arg("-v") + .arg("--offline") + .arg("--ignore-stdin") + .arg("--pretty=format") + .arg("options") + .arg("httpbin.org"); + + cmd.assert().stdout(indoc! {r#" + OPTIONS / HTTP/1.1 + accept: */* + accept-encoding: gzip, deflate + connection: keep-alive + host: httpbin.org + + "#}); + + Ok(()) +} From a5ccf0b75f78e353ada17c4c5cd57c44fd1ede0c Mon Sep 17 00:00:00 2001 From: Jan Verbeek Date: Sat, 6 Feb 2021 20:23:25 +0100 Subject: [PATCH 3/6] Use write_all instead of write to ensure everything is written --- src/download.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/download.rs b/src/download.rs index a74a4d4b..11d6cd04 100644 --- a/src/download.rs +++ b/src/download.rs @@ -103,7 +103,7 @@ pub async fn download_file( let mut downloaded = 0; while let Some(chunk) = response.chunk().await.unwrap() { - buffer.write(&chunk).unwrap(); + buffer.write_all(&chunk).unwrap(); downloaded += chunk.len() as u64; if let Some(pb) = &pb { pb.set_position(downloaded); From de819e7e4f828f766d27c975f8a0135328e10680 Mon Sep 17 00:00:00 2001 From: ducaale Date: Sun, 7 Feb 2021 12:32:24 +0000 Subject: [PATCH 4/6] run cargo clippy --fix --- src/request_items.rs | 2 +- src/url.rs | 6 +++--- src/utils.rs | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/request_items.rs b/src/request_items.rs index 4ac143fb..365222bc 100644 --- a/src/request_items.rs +++ b/src/request_items.rs @@ -82,7 +82,7 @@ impl RequestItems { _ => {} } } - if body.len() > 0 { + if !body.is_empty() { Ok(Some(Body::Json(body.into()))) } else { Ok(None) diff --git a/src/url.rs b/src/url.rs index 94570fa5..3f0dfdfe 100644 --- a/src/url.rs +++ b/src/url.rs @@ -6,12 +6,12 @@ impl Url { pub fn new(url: String, default_scheme: Option) -> Url { let default_scheme = default_scheme.unwrap_or("http://".to_string()); let re = Regex::new("[a-zA-Z]://.+").unwrap(); - if url.starts_with(":") { + if url.starts_with(':') { let url = format!("{}{}{}", default_scheme, "localhost", url); - Url(reqwest::Url::parse(&url).unwrap().into()) + Url(reqwest::Url::parse(&url).unwrap()) } else if !re.is_match(&url) { let url = format!("{}{}", default_scheme, url); - Url(reqwest::Url::parse(&url).unwrap().into()) + Url(reqwest::Url::parse(&url).unwrap()) } else { Url(reqwest::Url::parse(&url).unwrap()) } diff --git a/src/utils.rs b/src/utils.rs index f7930f27..364f6600 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -75,14 +75,14 @@ pub fn colorize<'a>( theme: &Theme, ) -> impl Iterator + 'a { lazy_static::lazy_static! { - static ref TS: ThemeSet = ThemeSet::from(from_binary(include_bytes!(concat!( + static ref TS: ThemeSet = from_binary(include_bytes!(concat!( env!("OUT_DIR"), "/themepack.themedump" - )))); - static ref PS: SyntaxSet = SyntaxSet::from(from_binary(include_bytes!(concat!( + ))); + static ref PS: SyntaxSet = from_binary(include_bytes!(concat!( env!("OUT_DIR"), "/syntax.packdump" - )))); + ))); } let syntax = PS.find_syntax_by_extension(syntax).unwrap(); let mut h = match theme { From 3ed6ba8775ecb494913d62d6023f1bc334be896c Mon Sep 17 00:00:00 2001 From: Pascal Lombard Date: Sun, 7 Feb 2021 13:48:20 +0100 Subject: [PATCH 5/6] - Test directly against httpbin.org/json. --- Cargo.lock | 23 +++++++++++++++++++++-- Cargo.toml | 1 + tests/cli.rs | 17 ++++++----------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f64026f4..bb85ebdf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,6 +280,15 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "float-cmp" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1267f4ac4f343772758f7b1bdcbe767c218bbab93bb432acbf5162bbf85a6c4" +dependencies = [ + "num-traits", +] + [[package]] name = "fnv" version = "1.0.7" @@ -422,6 +431,7 @@ dependencies = [ "indoc", "jsonxf", "lazy_static", + "predicates", "regex", "reqwest", "rpassword", @@ -699,6 +709,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + [[package]] name = "ntapi" version = "0.3.6" @@ -876,12 +892,15 @@ dependencies = [ [[package]] name = "predicates" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73dd9b7b200044694dfede9edf907c1ca19630908443e9447e624993700c6932" +checksum = "eeb433456c1a57cc93554dea3ce40b4c19c4057e41c55d4a0f3d84ea71c325aa" dependencies = [ "difference", + "float-cmp", + "normalize-line-endings", "predicates-core", + "regex", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index eee3d004..99524af8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ atty = "0.2" jsonxf = "1.0" indicatif = "0.15.0" lazy_static = "1.4.0" +predicates = "1.0.7" regex = "1" reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "stream", "json", "gzip", "brotli", "multipart"] } rpassword = "5.0.0" diff --git a/tests/cli.rs b/tests/cli.rs index a002bf90..a0dd1b4d 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,6 +1,7 @@ use assert_cmd::prelude::*; use indoc::indoc; use std::process::Command; +use predicates::prelude::*; #[test] fn basic_post() -> Result<(), Box> { @@ -79,20 +80,14 @@ fn basic_head() -> Result<(), Box> { fn basic_options() -> Result<(), Box> { let mut cmd = Command::cargo_bin("ht")?; cmd.arg("-v") - .arg("--offline") .arg("--ignore-stdin") .arg("--pretty=format") .arg("options") - .arg("httpbin.org"); - - cmd.assert().stdout(indoc! {r#" - OPTIONS / HTTP/1.1 - accept: */* - accept-encoding: gzip, deflate - connection: keep-alive - host: httpbin.org - - "#}); + .arg("httpbin.org/json"); + // Verify that the response is ok and contains an 'allow' header. + cmd.assert().stdout(predicate::str::contains("HTTP/1.1 200 OK")); + cmd.assert().stdout(predicate::str::contains("allow:")); + Ok(()) } From 84d9d49011f4e247cba35571506233f9828529e9 Mon Sep 17 00:00:00 2001 From: Pascal Lombard Date: Sun, 7 Feb 2021 15:58:15 +0100 Subject: [PATCH 6/6] - move tests-only dependency in correct section. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 99524af8..bef43b0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ atty = "0.2" jsonxf = "1.0" indicatif = "0.15.0" lazy_static = "1.4.0" -predicates = "1.0.7" regex = "1" reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "stream", "json", "gzip", "brotli", "multipart"] } rpassword = "5.0.0" @@ -36,6 +35,7 @@ features = ["parsing", "html", "yaml-load", "dump-load", "dump-create", "regex-o [dev-dependencies] assert_cmd = "1.0" indoc = "1.0" +predicates = "1.0.7" [build-dependencies] syntect = { version = "4.4", default-features = false }