Skip to content

Commit

Permalink
feat: redirect options
Browse files Browse the repository at this point in the history
  • Loading branch information
kriogenia committed Apr 7, 2024
1 parent 5b6a059 commit ab5859b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
25 changes: 17 additions & 8 deletions bin/src/commands/reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
use crate::commands::run::RequestArgs;
use rede_parser::Request;
use reqwest::{ClientBuilder, Request as Reqwest, RequestBuilder, Url};
use reqwest::redirect::Policy;
use reqwest::{Client, ClientBuilder, Request as Reqwest, RequestBuilder, Url};

use crate::errors::RequestError;

pub async fn send(req: Request, args: RequestArgs) -> Result<String, RequestError<reqwest::Error>> {
let url = Url::parse(&req.url).map_err(|e| RequestError::invalid_url(&req.url, e))?;

let mut client = ClientBuilder::new();
if let Some(timeout) = args.timeout {
client = client.timeout(timeout);
}

let client = build_client(&args)?;
let reqwest = Reqwest::new(req.method, url);
// todo handle build errors
let builder = RequestBuilder::from_parts(client.build()?, reqwest).version(req.http_version);
let builder = RequestBuilder::from_parts(client, reqwest).version(req.http_version);
// todo handle send errors
// todo handle text errors
Ok(builder.send().await?.text().await?)
}

fn build_client(args: &RequestArgs) -> Result<Client, reqwest::Error> {
let mut client = ClientBuilder::new();
if let Some(timeout) = args.timeout {
client = client.timeout(timeout);
}
client = match (args.no_redirect, args.max_redirects) {
(true, _) => client.redirect(Policy::none()),
(false, Some(val)) => client.redirect(Policy::limited(val)),
_ => client,
};
client.build()
}
18 changes: 15 additions & 3 deletions bin/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ pub struct Command {
/// Request file to execute
#[arg(default_value = "-")]
request: String,
/// Timeout, in a string like \[0-9]+(ns|us|ms|\[smhdwy], for example "3m"
#[arg(short, long)]
/// Timeout, in a string like [0-9]+(ns|us|ms|[smhdwy], for example "3m"
#[arg(long)]
timeout: Option<String>,
/// Disallows auto-redirection
#[arg(long)]
no_redirect: bool,
/// Maximum number of redirects allowed, by default 10.
#[arg(long)]
max_redirects: Option<usize>,
}

impl Command {
Expand All @@ -36,6 +42,8 @@ impl Command {

pub struct RequestArgs {
pub timeout: Option<Duration>,
pub no_redirect: bool,
pub max_redirects: Option<usize>,
}

impl TryFrom<&Command> for RequestArgs {
Expand All @@ -59,6 +67,10 @@ impl TryFrom<&Command> for RequestArgs {
.with_source_code(t.to_owned())
})?;

Ok(RequestArgs { timeout })
Ok(RequestArgs {
timeout,
no_redirect: value.no_redirect,
max_redirects: value.max_redirects,
})
}
}
1 change: 1 addition & 0 deletions bin/tests/inputs/redirect_loop.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http.url = "http://localhost:8080/api/redirect_loop"
5 changes: 4 additions & 1 deletion bin/tests/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use assert_cmd::Command;
use predicates::boolean::PredicateBooleanExt;

Check warning on line 2 in bin/tests/run.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `predicates::boolean::PredicateBooleanExt`

Check warning on line 2 in bin/tests/run.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `predicates::boolean::PredicateBooleanExt`
use predicates::prelude::predicate::str::contains;

// Set of integration tests for `rede run`, they are all ignored to run them only manually.
Expand Down Expand Up @@ -49,11 +50,13 @@ macro_rules! test_error {
}

test_request!(get_simple -> contains(r#"{"hello":"world"}"#));
test_request!(http_version -> contains(r#"{"http_version":"HTTP/1.0"}"#));
test_request!(http_version -> contains(r#""http_version":"HTTP/1.0""#));
// todo -no-redirect, requires --verbose

test_error!(invalid_url -> contains("invalid url"));
test_error!(failed_connection -> contains("failed connection"));
test_error!(bad_url_scheme -> contains("failed request building"));
test_error!(timeout<>, "--timeout", "0s" -> contains("timeout"));

test_error!(#[ignore] unsupported_http_version -> contains("wrong http version"));
test_error!(#[ignore] redirect_loop, "--max-redirects", "5" -> contains("redirect"));

0 comments on commit ab5859b

Please sign in to comment.