From 110c3aee33f5cfc16f92aa6453ce89748e15721f Mon Sep 17 00:00:00 2001 From: anhcuky <42137630+anhcuky@users.noreply.github.com> Date: Mon, 10 Oct 2022 22:46:31 +0700 Subject: [PATCH] Fix redirect limited(0) should not follow redirects (#1645) Closes #1639 --- src/redirect.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/redirect.rs b/src/redirect.rs index 00faf6e..eabaea3 100644 --- a/src/redirect.rs +++ b/src/redirect.rs @@ -128,7 +128,7 @@ impl Policy { match self.inner { PolicyKind::Custom(ref custom) => custom(attempt), PolicyKind::Limit(max) => { - if attempt.previous.len() == max { + if attempt.previous.len() >= max { attempt.error(TooManyRedirects) } else { attempt.follow() @@ -277,6 +277,18 @@ fn test_redirect_policy_limit() { } } +#[test] +fn test_redirect_policy_limit_to_0() { + let policy = Policy::limited(0); + let next = Url::parse("http://x.y/z").unwrap(); + let previous = vec![Url::parse("http://a.b/c").unwrap()]; + + match policy.check(StatusCode::FOUND, &next, &previous) { + ActionKind::Error(err) if err.is::() => (), + other => panic!("unexpected {:?}", other), + } +} + #[test] fn test_redirect_policy_custom() { let policy = Policy::custom(|attempt| {