Skip to content

Commit

Permalink
Merge pull request #514 from Sebekerga/master
Browse files Browse the repository at this point in the history
Added functionality to delay response by specified time
  • Loading branch information
svenstaro authored Aug 22, 2024
2 parents d0ba00a + ee0f7cd commit 555270c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub struct Args {
)]
pub interface: IpAddr,

/// Delay in milliseconds before sending the response in milliseconds
#[arg(short, long, default_value = "0")]
pub delay: u64,

/// Generate completion file for a shell
#[arg(long = "print-completions", value_name = "shell")]
pub print_completions: Option<clap_complete::Shell>,
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use colored::*;
use colored_json::ToColoredJson;
use hyper::{header::CONTENT_TYPE, HeaderMap};
use inflector::Inflector;
use tokio::time::{sleep, Duration};

use crate::args::Args;

Expand Down Expand Up @@ -59,6 +60,10 @@ async fn dummy_response(_uri: Uri, Extension(args): Extension<Args>) -> impl Int
tera.register_function("uuid", template_uuid);
tera.register_function("lorem", template_lorem);
let rendered_body = tera.render_str(&args.body, &tera::Context::new()).unwrap();

// Delay response.
sleep(Duration::from_millis(args.delay)).await;

(status_code, headers, rendered_body)
}

Expand Down
25 changes: 25 additions & 0 deletions tests/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,28 @@ fn returns_custom_headers(method: Method) -> Result<(), Error> {

Ok(())
}

/// Setting a custom delay will delay the response making it at least that long.
#[rstest(
method,
case::get(Method::GET),
case::post(Method::POST),
case::put(Method::PUT),
case::delete(Method::DELETE),
case::options(Method::OPTIONS),
case::patch(Method::PATCH),
)]
fn returns_custom_delay(method: Method) -> Result<(), Error> {
let dh = DummyhttpProcess::new(vec!["-d", "1000"])?;

let client = Client::new();
let start = std::time::Instant::now();
let resp = client.request(method, &dh.url).send()?;
let elapsed = start.elapsed();

assert_eq!(resp.status(), StatusCode::OK);
assert!(elapsed >= std::time::Duration::from_millis(1000));
assert_eq!(resp.text()?, "dummyhttp");

Ok(())
}

0 comments on commit 555270c

Please sign in to comment.