aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-24 07:27:41 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-24 07:27:41 +0000
commit63be75f5440c981fa50d8a56e14fff337b6f7b25 (patch)
treefbe5f05e2ffd424116db46438d7428a2ec49ac1f
parentbdd52c6b902f2af98ca18e07a887585633dc4db9 (diff)
Print substituted request line
-rw-r--r--Cargo.toml2
-rw-r--r--src/executor.rs29
-rw-r--r--src/main.rs24
3 files changed, 34 insertions, 21 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 2a1eef1..09bd918 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "barb"
-version = "0.1.4"
+version = "0.1.5"
edition = "2021"
license = "GPL-3.0-or-later"
description = "Command-line tool to perform file-based HTTP(s) requests."
diff --git a/src/executor.rs b/src/executor.rs
index bd9767b..f78fd2a 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -57,7 +57,14 @@ impl Executor {
Executor { context }
}
- fn make_req(&self, bfile: &BarbFile, print_headers: bool) -> ureq::Request {
+ fn make_req(&self, bfile: &BarbFile, print_req: bool, print_headers: bool) -> ureq::Request {
+ if print_req {
+ println!(
+ "{} {}",
+ bfile.method_as_string().as_str(),
+ self.context.substitute(&bfile.url()).as_str()
+ );
+ }
let mut req = ureq::request(
bfile.method_as_string().as_str(),
self.context.substitute(&bfile.url()).as_str(),
@@ -91,12 +98,17 @@ impl Executor {
match resp {
Ok(resp) => Ok(resp),
Err(UreqError::Status(_, resp)) => Ok(resp),
- Err(UreqError::Transport(transp)) => Err(String::from(transp.to_string()))
+ Err(UreqError::Transport(transp)) => Err(String::from(transp.to_string())),
}
}
- pub fn execute(&mut self, bfile: &BarbFile, print_headers: bool) -> Result<ureq::Response, String> {
- self.run(bfile, self.make_req(&bfile, print_headers))
+ pub fn execute(
+ &mut self,
+ bfile: &BarbFile,
+ print_req: bool,
+ print_headers: bool,
+ ) -> Result<ureq::Response, String> {
+ self.run(bfile, self.make_req(&bfile, print_req, print_headers))
}
}
@@ -142,12 +154,13 @@ mod tests {
#[test]
fn test_make_req_simple_headers() {
let executor = Executor::new(Context::empty());
- let bfile = BarbFile::from_str(
- "#GET^http://foo.bar\n#Foo: Bar\n#Bar: Baz\n\n"
- ).unwrap();
+ let bfile = BarbFile::from_str("#GET^http://foo.bar\n#Foo: Bar\n#Bar: Baz\n\n").unwrap();
let req = executor.make_req(&bfile, false);
- assert_eq!(req.header_names(), vec![String::from("foo"), String::from("bar")]);
+ assert_eq!(
+ req.header_names(),
+ vec![String::from("foo"), String::from("bar")]
+ );
assert_eq!(req.header("foo"), Some("Bar"));
assert_eq!(req.header("bar"), Some("Baz"));
}
diff --git a/src/main.rs b/src/main.rs
index a35ad7d..6099020 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -31,6 +31,10 @@ struct Args {
}
impl Args {
+ pub fn print_req(&self) -> bool {
+ return !self.body
+ }
+
pub fn print_headers(&self) -> bool {
self.headers || self.all_headers || !self.body
}
@@ -47,13 +51,11 @@ impl Args {
self.all_headers
}
- pub fn files_iter(&self) -> Iter<String>
- {
+ pub fn files_iter(&self) -> Iter<String> {
self.files.iter()
}
- pub fn jq_filter(&self) -> &Option<String>
- {
+ pub fn jq_filter(&self) -> &Option<String> {
&self.filter
}
}
@@ -72,8 +74,9 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<
fs::read_to_string(file_name.as_str())
.map_err(|_| format!("Failed to read file '{}'", file_name))?
.as_str(),
- ).map_err(|_| format!("Failed to parse file '{}'", file_name))?;
- let response = executor.execute(&bfile, args.req_headers())?;
+ )
+ .map_err(|_| format!("Failed to parse file '{}'", file_name))?;
+ let response = executor.execute(&bfile, args.print_req(), args.req_headers())?;
if args.print_headers() {
println!("{} {}", response.status(), response.status_text());
@@ -81,7 +84,7 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<
println!(
"{}: {}",
header_name,
- // Header is guaranteed to exist
+ // Header is guaranteed to exist
response.header(header_name.as_str()).unwrap()
);
}
@@ -93,10 +96,7 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<
"{}",
match args.raw_body() {
true => body,
- false => format_json(
- body.as_str(),
- Indentation::Default
- ),
+ false => format_json(body.as_str(), Indentation::Default),
}
);
}
@@ -112,7 +112,7 @@ fn main() {
for file in args.files_iter() {
match run_file(&args, &mut executor, file) {
Ok(()) => (),
- Err(err) => println!("{}", err)
+ Err(err) => println!("{}", err),
}
}
}