Skip to content

Commit

Permalink
Remove IntoResponse implementation for Option<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
Jörn Bethune committed Dec 2, 2023
1 parent d31b610 commit 277eba2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 39 deletions.
12 changes: 0 additions & 12 deletions axum-core/src/response/into_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,6 @@ impl IntoResponse for Infallible {
}
}

impl<T> IntoResponse for Option<T>
where
T: IntoResponse,
{
fn into_response(self) -> Response {
match self {
Some(v) => v.into_response(),
None => StatusCode::NO_CONTENT.into_response(),
}
}
}

impl<T, E> IntoResponse for Result<T, E>
where
T: IntoResponse,
Expand Down
28 changes: 1 addition & 27 deletions axum/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<T> From<T> for Html<T> {

#[cfg(test)]
mod tests {
use crate::extract::{Extension, Path};
use crate::extract::Extension;
use crate::test_helpers::TestClient;
use crate::{routing::get, Router};
use axum_core::response::IntoResponse;
Expand Down Expand Up @@ -235,30 +235,4 @@ mod tests {
let res = client.get("/unit").send().await;
assert_eq!(res.status(), StatusCode::NO_CONTENT)
}

#[tokio::test]
async fn test_option_http204_response() {
const SOMETHING: &str = "something";
async fn optional_value(Path(number): Path<usize>) -> Option<&'static str> {
if number % 2 == 0 {
None
} else {
Some(SOMETHING)
}
}

let app = Router::new().route("/optional/:number", get(optional_value));
let client = TestClient::new(app);
for i in 0..4 {
let url = format!("/optional/{i}");
let res = client.get(&url).send().await;
if i % 2 == 0 {
assert_eq!(res.status(), StatusCode::NO_CONTENT);
assert_eq!(res.text().await.len(), 0);
} else {
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, SOMETHING);
}
}
}
}

0 comments on commit 277eba2

Please sign in to comment.