follow_redirect: expose previous and next request methods#559
Conversation
|
For context:
|
| } | ||
|
|
||
| /// Returns the method for the next request, after applying redirection logic. | ||
| pub fn next_method(&self) -> &Method { |
| } | ||
|
|
||
| /// Returns the method for the previous request, before redirection. | ||
| pub fn previous_method(&self) -> &Method { |
| } | ||
|
|
||
| /// Returns the method for the next request, after applying redirection logic. | ||
| pub fn next_method(&self) -> &Method { |
There was a problem hiding this comment.
this should probably be method to match location
| @@ -33,7 +33,7 @@ impl<B, E> Policy<B, E> for SameOrigin { | |||
| #[cfg(test)] | |||
There was a problem hiding this comment.
would be nice to throw in some simple assertions using these new accessors, if they fit into any existing tests
There was a problem hiding this comment.
I've added a stops_post_redirect_get test to cover this.
|
@jlizen thanks for the review! I'll try to find some time in the next days to rebase and address your feedback, I'll ping you when this is ready for another pass. Just one question on:
There is already a cyclic-redirection example at https://docs.rs/tower-http/latest/tower_http/follow_redirect/policy/trait.Policy.html#example, maybe it would be better to tweak that to use a |
Sounds good! No rush.
Yeah that sounds like a nice improvement, as long as we can capture cycles where it is flipping methods (which sounds like your HashSet idea would). Totally optional though. Just was thinking about an example that would realistically surface this new api's usage. |
e2189ad to
754d2a3
Compare
|
@jlizen updated, PTAL. |
jlizen
left a comment
There was a problem hiding this comment.
Looks great! Couple more nits, then another one that's up to you.
| self.status | ||
| } | ||
|
|
||
| /// Returns the method for the next request, after applying redirection logic. |
There was a problem hiding this comment.
| /// Returns the method for the next request, after applying redirection logic. | |
| /// Returns the destination method of the redirection. |
| let mut res = ready!(this.future.as_mut().poll(cx)?); | ||
| res.extensions_mut().insert(RequestUri(this.uri.clone())); | ||
|
|
||
| let previous_method = &this.method.clone(); |
There was a problem hiding this comment.
| let previous_method = &this.method.clone(); | |
| let previous_method = this.method.clone(); |
| status: res.status(), | ||
| method: this.method, | ||
| location: &location, | ||
| previous_method, |
There was a problem hiding this comment.
| previous_method, | |
| previous_method: &previous_method |
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn stops_post_redirect_get() { |
There was a problem hiding this comment.
Optional: This is a good opportunity to flesh out our test coverage a bit, since we have new accessors. Up to you if you care to!
We could test:
307/308 POSTpreservation303 POST-> GET303 HEADpreservation
Probably is worth parameterizing a helper at that point.
There was a problem hiding this comment.
That makes sense. I added a shared redirection handler and then dedicated tests to cover the following matrix:
- 301
- POST -> GET
- GET -> GET
- 302
- POST -> GET
- PUT -> PUT
- HEAD -> HEAD
- 303
- POST -> GET
- PUT -> GET
- HEAD -> HEAD
- 307/308
- POST -> POST
This augments follow-redirect `Attempt` in order to expose two relevant HTTP methods during redirects: - the method for the original/previous request - the method to be performed on the next request upon following a redirection This allows consumers to write policies based on both previous and next requests methods.
754d2a3 to
68fe5f0
Compare
jlizen
left a comment
There was a problem hiding this comment.
Looks good, thanks for the contribution!
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [tower-http](https://github.com/tower-rs/tower-http) | workspace.dependencies | patch | `0.6.9` → `0.6.10` | --- ### Release Notes <details> <summary>tower-rs/tower-http (tower-http)</summary> ### [`v0.6.10`](https://github.com/tower-rs/tower-http/releases/tag/tower-http-0.6.10) [Compare Source](tower-rs/tower-http@tower-http-0.6.9...tower-http-0.6.10) #### Added - `follow-redirect`: expose `Attempt::method()` and `Attempt::previous_method()` so redirect policies can react to method changes across redirects (e.g. POST to GET on 301/303) ([#​559]) #### Fixed - Restore `tokio` and `async-compression` as no-op features. These will be removed next breaking release ([#​667]) [#​559]: tower-rs/tower-http#559 [#​667]: tower-rs/tower-http#667 #### What's Changed - fix: restore tokio and async-compression as no-op features by [@​jlizen](https://github.com/jlizen) in [#​667](tower-rs/tower-http#667) - fix gate-ing of atomic64 in tests by [@​alexanderkjall](https://github.com/alexanderkjall) in [#​607](tower-rs/tower-http#607) - follow\_redirect: expose previous and next request methods by [@​lucab](https://github.com/lucab) in [#​559](tower-rs/tower-http#559) - chore: release tower-http 0.6.10 by [@​jlizen](https://github.com/jlizen) in [#​669](tower-rs/tower-http#669) #### New Contributors - [@​lucab](https://github.com/lucab) made their first contribution in [#​559](tower-rs/tower-http#559) **Full Changelog**: <tower-rs/tower-http@tower-http-0.6.9...tower-http-0.6.10> </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xMDEuMSIsInVwZGF0ZWRJblZlciI6IjQzLjEwMS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJ0eXBlL3BhdGNoIl19--> Reviewed-on: https://git.erwanleboucher.dev/eleboucher/towonel/pulls/34
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [tower-http](https://github.com/tower-rs/tower-http) | workspace.dependencies | patch | `0.6.9` → `0.6.10` | --- ### Release Notes <details> <summary>tower-rs/tower-http (tower-http)</summary> ### [`v0.6.10`](https://github.com/tower-rs/tower-http/releases/tag/tower-http-0.6.10) [Compare Source](tower-rs/tower-http@tower-http-0.6.9...tower-http-0.6.10) #### Added - `follow-redirect`: expose `Attempt::method()` and `Attempt::previous_method()` so redirect policies can react to method changes across redirects (e.g. POST to GET on 301/303) ([#​559]) #### Fixed - Restore `tokio` and `async-compression` as no-op features. These will be removed next breaking release ([#​667]) [#​559]: tower-rs/tower-http#559 [#​667]: tower-rs/tower-http#667 #### What's Changed - fix: restore tokio and async-compression as no-op features by [@​jlizen](https://github.com/jlizen) in [#​667](tower-rs/tower-http#667) - fix gate-ing of atomic64 in tests by [@​alexanderkjall](https://github.com/alexanderkjall) in [#​607](tower-rs/tower-http#607) - follow\_redirect: expose previous and next request methods by [@​lucab](https://github.com/lucab) in [#​559](tower-rs/tower-http#559) - chore: release tower-http 0.6.10 by [@​jlizen](https://github.com/jlizen) in [#​669](tower-rs/tower-http#669) #### New Contributors - [@​lucab](https://github.com/lucab) made their first contribution in [#​559](tower-rs/tower-http#559) **Full Changelog**: <tower-rs/tower-http@tower-http-0.6.9...tower-http-0.6.10> </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xMDEuMSIsInVwZGF0ZWRJblZlciI6IjQzLjEwMS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJ0eXBlL3BhdGNoIl19--> Reviewed-on: https://git.erwanleboucher.dev/eleboucher/towonel/pulls/34
This augments follow-redirect
Attemptin order to expose two relevant HTTP methods during redirects:This allows consumers to write policies based on both previous and next requests methods.