Skip to content

Commit

Permalink
Fix client-wide redirect policy not respected (#251)
Browse files Browse the repository at this point in the history
Fix a regression from #240 that caused redirect policies to be ignored if being set client-wide instead of per-request.

Fixes #250.
  • Loading branch information
sagebind authored Nov 11, 2020
1 parent 11bc004 commit 6d8186b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
- name: Generate code coverage report
uses: actions-rs/[email protected]
with:
version: '0.16.0'
args: "-p isahc --run-types Doctests Tests --features cookies,psl"

- name: Upload coverage to Codecov
Expand Down
9 changes: 8 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,13 +892,20 @@ impl HttpClient {
}

/// Actually send the request. All the public methods go through here.
async fn send_async_inner(&self, request: Request<Body>) -> Result<Response<Body>, Error> {
async fn send_async_inner(&self, mut request: Request<Body>) -> Result<Response<Body>, Error> {
let span = tracing::debug_span!(
"send_async",
method = ?request.method(),
uri = ?request.uri(),
);

// Set redirect policy if not specified.
if request.extensions().get::<RedirectPolicy>().is_none() {
if let Some(policy) = self.inner.defaults.get::<RedirectPolicy>().cloned() {
request.extensions_mut().insert(policy);
}
}

let ctx = interceptor::Context {
invoker: Arc::new(self),
interceptors: &self.inner.interceptors,
Expand Down
27 changes: 27 additions & 0 deletions tests/redirects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ fn redirect_also_sends_post(status: u16) {
assert_eq!(m2.request().method, "POST");
}

// Issue #250
#[test]
fn redirect_policy_from_client() {
let m2 = mock!();
let location = m2.url();

let m1 = mock! {
status: 302,
headers {
"Location": location,
}
};

let client = HttpClient::builder()
.redirect_policy(RedirectPolicy::Limit(8))
.build()
.unwrap();

let response = client.post(m1.url(), ()).unwrap();

assert_eq!(response.status(), 200);
assert_eq!(response.effective_uri().unwrap().to_string(), m2.url());

assert_eq!(m1.request().method, "POST");
assert_eq!(m2.request().method, "GET");
}

#[test]
fn redirect_non_rewindable_body_returns_error() {
let m2 = mock!();
Expand Down

0 comments on commit 6d8186b

Please sign in to comment.