Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make () and None return a HTTP 204 response instead of 200 #2393

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions axum-core/src/response/into_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ pub trait IntoResponse {

impl IntoResponse for StatusCode {
fn into_response(self) -> Response {
let mut res = ().into_response();
let mut res = Body::empty().into_response();
*res.status_mut() = self;
res
}
}

impl IntoResponse for () {
fn into_response(self) -> Response {
Body::empty().into_response()
StatusCode::NO_CONTENT.into_response()
}
}

Expand Down
2 changes: 1 addition & 1 deletion axum-extra/src/json_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ mod tests {
)
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions axum-extra/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);

let res = client.get("/foo/").send().await;
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(res.headers()["location"], "/foo");

let res = client.get("/bar/").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);

let res = client.get("/bar").send().await;
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
Expand Down
2 changes: 1 addition & 1 deletion axum/src/extract/connect_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ axum_core::__impl_deref!(ConnectInfo);
///
/// let request = Request::new(Body::empty());
/// let response = app.oneshot(request).await.unwrap();
/// assert_eq!(response.status(), StatusCode::OK);
/// assert_eq!(response.status(), StatusCode::NO_CONTENT);
/// }
/// #
/// # #[tokio::main]
Expand Down
18 changes: 9 additions & 9 deletions axum/src/extract/matched_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -225,7 +225,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -242,7 +242,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[tokio::test]
Expand All @@ -259,7 +259,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -279,7 +279,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -300,7 +300,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -314,7 +314,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

// https://github.com/tokio-rs/axum/issues/1579
Expand All @@ -334,7 +334,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -349,6 +349,6 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}
}
16 changes: 8 additions & 8 deletions axum/src/extract/nested_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/api/users").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -154,7 +154,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/api/users").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -172,7 +172,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/api/v2/users").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -190,7 +190,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/api/v2/users").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -208,7 +208,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/users").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -226,7 +226,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/api/users").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -241,7 +241,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/api/doesnt-exist").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -260,6 +260,6 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/api/users").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}
}
10 changes: 5 additions & 5 deletions axum/src/extract/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,10 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/users/42").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);

let res = client.post("/users/1337").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand All @@ -570,7 +570,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/users/42").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand Down Expand Up @@ -641,7 +641,7 @@ mod tests {
assert_eq!(res.status(), StatusCode::NOT_FOUND);

let res = client.get("/foo").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand Down Expand Up @@ -759,7 +759,7 @@ mod tests {
let client = TestClient::new(app);

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand Down
2 changes: 1 addition & 1 deletion axum/src/extract/request_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ mod tests {
let client = TestClient::new(Router::new().route("/", get(handler)).layer(Extension(Ext)));

let res = client.get("/").header("x-foo", "123").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}
}
2 changes: 1 addition & 1 deletion axum/src/extract/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ mod tests {

let res = svc.oneshot(req).await.unwrap();

assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion axum/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ mod tests {
.send()
.await;

res.status() == StatusCode::OK
res.status() == StatusCode::NO_CONTENT
}

assert!(valid_json_content_type("application/json").await);
Expand Down
2 changes: 1 addition & 1 deletion axum/src/middleware/from_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ mod tests {
.header(http::header::AUTHORIZATION, "secret")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

// just needs to compile
Expand Down
11 changes: 11 additions & 0 deletions axum/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl<T> From<T> for Html<T> {
#[cfg(test)]
mod tests {
use crate::extract::Extension;
use crate::test_helpers::TestClient;
use crate::{routing::get, Router};
use axum_core::response::IntoResponse;
use http::HeaderMap;
Expand Down Expand Up @@ -224,4 +225,14 @@ mod tests {
.route("/", get(header_array_extension_body))
.route("/", get(header_array_extension_mixed_body));
}

#[tokio::test]
async fn test_unit_http204_response() {
async fn unit() {} // returns the unit () value

let app = Router::new().route("/unit", get(unit));
let client = TestClient::new(app);
let res = client.get("/unit").send().await;
assert_eq!(res.status(), StatusCode::NO_CONTENT)
}
}
22 changes: 17 additions & 5 deletions axum/src/routing/tests/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ async fn basic() {

let client = TestClient::new(app);

assert_eq!(client.get("/foo").send().await.status(), StatusCode::OK);
assert_eq!(
client.get("/foo").send().await.status(),
StatusCode::NO_CONTENT
);

let res = client.get("/does-not-exist").send().await;
assert_eq!(res.status(), StatusCode::OK);
Expand All @@ -24,7 +27,10 @@ async fn nest() {

let client = TestClient::new(app);

assert_eq!(client.get("/foo/bar").send().await.status(), StatusCode::OK);
assert_eq!(
client.get("/foo/bar").send().await.status(),
StatusCode::NO_CONTENT
);

let res = client.get("/does-not-exist").send().await;
assert_eq!(res.status(), StatusCode::OK);
Expand All @@ -40,8 +46,14 @@ async fn or() {

let client = TestClient::new(app);

assert_eq!(client.get("/one").send().await.status(), StatusCode::OK);
assert_eq!(client.get("/two").send().await.status(), StatusCode::OK);
assert_eq!(
client.get("/one").send().await.status(),
StatusCode::NO_CONTENT
);
assert_eq!(
client.get("/two").send().await.status(),
StatusCode::NO_CONTENT
);

let res = client.get("/does-not-exist").send().await;
assert_eq!(res.status(), StatusCode::OK);
Expand Down Expand Up @@ -195,7 +207,7 @@ async fn doesnt_panic_if_used_with_nested_router() {
let client = TestClient::new(routes_all);

let res = client.get("/foobar").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), StatusCode::NO_CONTENT);
}

#[crate::test]
Expand Down
Loading