Skip to content

Commit ec6e096

Browse files
committed
update
1 parent cbc82dc commit ec6e096

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/redirect.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ pub struct Action {
5454
#[derive(Clone, Debug)]
5555
pub struct History {
5656
status: StatusCode,
57-
uri: Uri,
57+
location: Uri,
58+
previous: Uri,
5859
headers: HeaderMap,
5960
}
6061

@@ -238,20 +239,29 @@ impl<'a> Attempt<'a> {
238239
// ===== impl History =====
239240

240241
impl History {
241-
/// Get the status code of the redirect response.
242+
/// Returns the redirection response.
243+
#[inline(always)]
242244
pub fn status(&self) -> StatusCode {
243245
self.status
244246
}
245247

246-
/// Get the URI of the redirect response.
247-
pub fn uri(&self) -> &Uri {
248-
&self.uri
249-
}
250-
251-
/// Get the headers of the redirect response.
248+
/// Returns the headers of the redirection response.
249+
#[inline(always)]
252250
pub fn headers(&self) -> &HeaderMap {
253251
&self.headers
254252
}
253+
254+
/// Returns the destination URI of the redirection.
255+
#[inline(always)]
256+
pub fn location(&self) -> &Uri {
257+
&self.location
258+
}
259+
260+
/// Returns the URI of the original request.
261+
#[inline(always)]
262+
pub fn previous(&self) -> &Uri {
263+
&self.previous
264+
}
255265
}
256266

257267
#[derive(Clone)]
@@ -385,7 +395,8 @@ impl policy::Policy<Body, BoxError> for FollowRedirectPolicy {
385395
.get_or_insert_with(Vec::new)
386396
.push(History {
387397
status: attempt.status(),
388-
uri: attempt.location().clone(),
398+
location: attempt.location().clone(),
399+
previous: attempt.previous().clone(),
389400
headers: attempt.headers().clone(),
390401
});
391402
}

tests/redirect.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,19 @@ async fn test_redirect_history() {
498498
&"test-dst"
499499
);
500500

501-
let history = res.history().collect::<Vec<_>>();
502-
assert_eq!(history.len(), 2);
503-
assert_eq!(history[0].status(), 302);
504-
assert_eq!(history[0].uri().path(), "/second");
505-
assert_eq!(history[1].status(), 302);
506-
assert_eq!(history[1].uri().path(), "/dst");
501+
let mut history = res.history();
502+
503+
let next1 = history.next().unwrap();
504+
assert_eq!(next1.status(), 302);
505+
assert_eq!(next1.previous().path(), "/first");
506+
assert_eq!(next1.location().path(), "/second");
507+
assert_eq!(next1.headers()["location"], "/second");
508+
509+
let next2 = history.next().unwrap();
510+
assert_eq!(next2.status(), 302);
511+
assert_eq!(next2.previous().path(), "/second");
512+
assert_eq!(next2.location().path(), "/dst");
513+
assert_eq!(next2.headers()["location"], "/dst");
514+
515+
assert!(history.next().is_none());
507516
}

0 commit comments

Comments
 (0)