Skip to content

Treat both 2XX and 3XX responses as successful.#14

Merged
connec merged 1 commit intodigital-society-coop:masterfrom
alexmingoia:change/accept-3xx
Feb 8, 2023
Merged

Treat both 2XX and 3XX responses as successful.#14
connec merged 1 commit intodigital-society-coop:masterfrom
alexmingoia:change/accept-3xx

Conversation

@alexmingoia
Copy link
Contributor

@alexmingoia alexmingoia commented Nov 27, 2022

The 303 See Other response is often used to direct clients to another page after successfully creating a new resource, and such behavior is encouraged by web standards.

Thanks for this module, it's very useful. I'm using it to develop a web application.

Copy link
Contributor

@greizgh greizgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also add a test case checking for commit on redirection:

#[tokio::test]
async fn commit_on_redirection() {
    let (_db, pool, response) = build_app(|mut tx: Tx| async move {
        let (_, _) = insert_user(&mut tx, 1, "john redirect").await;
        http::StatusCode::SEE_OTHER
    })
    .await;

    assert!(response.status.is_redirection());

    let users: Vec<(i32, String)> = sqlx::query_as("SELECT * FROM users")
        .fetch_all(&pool)
        .await
        .unwrap();
    assert_eq!(users, vec![(1, "john redirect".to_string())]);
}

src/layer.rs Outdated
let res = res.await.unwrap(); // inner service is infallible

if res.status().is_success() {
if res.status().is_success() || res.status().is_redirection() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be safer to check that there was no error:

Suggested change
if res.status().is_success() || res.status().is_redirection() {
if !(res.status().is_server_error() || res.status().is_client_error()) {

@alexmingoia
Copy link
Contributor Author

I've made both your suggested changes. Thanks for taking a look at this.

@connec connec closed this Feb 8, 2023
@connec connec reopened this Feb 8, 2023
@connec
Copy link
Member

connec commented Feb 8, 2023

Thank you both for the PR, apologies for taking so long to get to it. I'll just wait for the workflows to pass then go ahead with a merge.

connec
connec previously approved these changes Feb 8, 2023
The `303 See Other` response is often used to direct clients
to another page after successfully creating a new resource,
and such behavior is encouraged by web standards.
@connec connec merged commit 24b812c into digital-society-coop:master Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants