File: handle_error.rs

package info (click to toggle)
rust-axum 0.7.9-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,992 kB
  • sloc: javascript: 26; makefile: 21; sql: 6; sh: 1
file content (99 lines) | stat: -rw-r--r-- 2,755 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use super::*;
use std::future::pending;
use tower::timeout::TimeoutLayer;

async fn unit() {}

async fn forever() {
    pending().await
}

fn timeout() -> TimeoutLayer {
    TimeoutLayer::new(Duration::from_millis(10))
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
#[crate::test]
async fn handler() {
    let app = Router::new().route(
        "/",
        get(forever.layer((
            HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }),
            timeout(),
        ))),
    );

    let client = TestClient::new(app);

    let res = client.get("/").await;
    assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
#[crate::test]
async fn handler_multiple_methods_first() {
    let app = Router::new().route(
        "/",
        get(forever.layer((
            HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }),
            timeout(),
        )))
        .post(unit),
    );

    let client = TestClient::new(app);

    let res = client.get("/").await;
    assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
#[crate::test]
async fn handler_multiple_methods_middle() {
    let app = Router::new().route(
        "/",
        delete(unit)
            .get(forever.layer((
                HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }),
                timeout(),
            )))
            .post(unit),
    );

    let client = TestClient::new(app);

    let res = client.get("/").await;
    assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
#[crate::test]
async fn handler_multiple_methods_last() {
    let app = Router::new().route(
        "/",
        delete(unit).get(forever.layer((
            HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }),
            timeout(),
        ))),
    );

    let client = TestClient::new(app);

    let res = client.get("/").await;
    assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
#[crate::test]
async fn handler_service_ext() {
    let fallible_service = tower::service_fn(|_| async { Err::<(), ()>(()) });
    let handle_error_service =
        fallible_service.handle_error(|_| async { StatusCode::INTERNAL_SERVER_ERROR });

    let app = Router::new().route("/", get_service(handle_error_service));

    let client = TestClient::new(app);

    let res = client.get("/").await;
    assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
}