1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use anyhow::Context as _;
use reqwest::blocking::Response;
use reqwest::header::LOCATION;
use reqwest::Url;
use crate::Result;
pub mod act;
mod cookie;
pub mod scrape;
pub mod session;
pub use self::cookie::CookieStorage;
pub use act::Act;
pub trait ResponseExt {
fn location_url(&self, base: &Url) -> Result<Url>;
}
impl ResponseExt for Response {
fn location_url(&self, base: &Url) -> Result<Url> {
let loc_str = self
.headers()
.get(LOCATION)
.context("Could not find location header in response")?
.to_str()?;
base.join(loc_str)
.context("Could not parse redirection url")
}
}
#[cfg(test)]
mod tests {
use reqwest::blocking::Client;
use reqwest::redirect::Policy;
use super::*;
#[test]
fn test_location_url() -> anyhow::Result<()> {
let client = Client::builder()
.redirect(Policy::none())
.build()
.unwrap();
let res = client.get("https://mail.google.com").send()?;
let actual = res.location_url(&Url::parse("https://mail.google.com").unwrap())?;
let expected = Url::parse("https://mail.google.com/mail/").unwrap();
assert_eq!(actual, expected);
Ok(())
}
}