Skip to content

Commit de02bae

Browse files
committed
Add test for panic location of overlapping method route panic
1 parent 25fac01 commit de02bae

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

axum/tests/panic_location.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! Separate test binary, because the panic hook is a global resource
2+
3+
use std::{
4+
panic::{catch_unwind, set_hook, take_hook},
5+
path::Path,
6+
sync::OnceLock,
7+
};
8+
9+
use axum::{routing::get, Router};
10+
11+
#[test]
12+
fn routes_with_overlapping_method_routes() {
13+
static PANIC_LOCATION_FILE: OnceLock<String> = OnceLock::new();
14+
15+
let default_hook = take_hook();
16+
set_hook(Box::new(|panic_info| {
17+
if let Some(location) = panic_info.location() {
18+
_ = PANIC_LOCATION_FILE.set(location.file().to_owned());
19+
}
20+
}));
21+
22+
let result = catch_unwind(|| {
23+
async fn handler() {}
24+
25+
let _: Router = Router::new()
26+
.route("/foo/bar", get(handler))
27+
.route("/foo/bar", get(handler));
28+
});
29+
set_hook(default_hook);
30+
31+
let panic_payload = result.unwrap_err();
32+
let panic_msg = panic_payload.downcast_ref::<String>().unwrap();
33+
34+
assert_eq!(
35+
panic_msg,
36+
"Overlapping method route. Handler for `GET /foo/bar` already exists"
37+
);
38+
39+
let file = PANIC_LOCATION_FILE.get().unwrap();
40+
assert_eq!(Path::new(file).file_name().unwrap(), "panic_location.rs");
41+
}

0 commit comments

Comments
 (0)