Skip to content

Commit dea224f

Browse files
committed
Update tests to use new #[catch] macro
1 parent f8c8bb8 commit dea224f

File tree

12 files changed

+67
-76
lines changed

12 files changed

+67
-76
lines changed

core/codegen/src/attribute/route/parse.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use devise::{Spanned, SpanWrapped, Result, FromMeta};
22
use devise::ext::{SpanDiagnosticExt, TypeExt};
3-
use indexmap::{IndexSet, IndexMap};
3+
use indexmap::IndexSet;
44
use proc_macro2::Span;
55

66
use crate::attribute::suppress::Lint;

core/codegen/tests/async-routes.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#[macro_use] extern crate rocket;
44
use rocket::http::uri::Origin;
5-
use rocket::request::Request;
65

76
async fn noop() { }
87

@@ -19,7 +18,7 @@ async fn repeated_query(sort: Vec<&str>) -> &str {
1918
}
2019

2120
#[catch(404)]
22-
async fn not_found(req: &Request<'_>) -> String {
21+
async fn not_found(uri: &Origin<'_>) -> String {
2322
noop().await;
24-
format!("{} not found", req.uri())
23+
format!("{} not found", uri)
2524
}

core/codegen/tests/catcher.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55

66
#[macro_use] extern crate rocket;
77

8-
use rocket::{Request, Rocket, Build};
8+
use rocket::{Rocket, Build};
99
use rocket::local::blocking::Client;
10-
use rocket::http::Status;
10+
use rocket::http::{Status, uri::Origin};
1111

12-
#[catch(404)] fn not_found_0() -> &'static str { "404-0" }
13-
#[catch(404)] fn not_found_1(_: &Request<'_>) -> &'static str { "404-1" }
14-
#[catch(404)] fn not_found_2(_: Status, _: &Request<'_>) -> &'static str { "404-2" }
15-
#[catch(default)] fn all(_: Status, r: &Request<'_>) -> String { r.uri().to_string() }
12+
#[catch(404)]
13+
fn not_found_0() -> &'static str { "404-0" }
14+
#[catch(404)]
15+
fn not_found_1() -> &'static str { "404-1" }
16+
#[catch(404, status = "<_s>")]
17+
fn not_found_2(_s: Status) -> &'static str { "404-2" }
18+
#[catch(default, status = "<_s>")]
19+
fn all(_s: Status, uri: &Origin<'_>) -> String { uri.to_string() }
1620

1721
#[test]
1822
fn test_simple_catchers() {
@@ -37,10 +41,14 @@ fn test_simple_catchers() {
3741
}
3842

3943
#[get("/<code>")] fn forward(code: u16) -> Status { Status::new(code) }
40-
#[catch(400)] fn forward_400(status: Status, _: &Request<'_>) -> String { status.code.to_string() }
41-
#[catch(404)] fn forward_404(status: Status, _: &Request<'_>) -> String { status.code.to_string() }
42-
#[catch(444)] fn forward_444(status: Status, _: &Request<'_>) -> String { status.code.to_string() }
43-
#[catch(500)] fn forward_500(status: Status, _: &Request<'_>) -> String { status.code.to_string() }
44+
#[catch(400, status = "<status>")]
45+
fn forward_400(status: Status) -> String { status.code.to_string() }
46+
#[catch(404, status = "<status>")]
47+
fn forward_404(status: Status) -> String { status.code.to_string() }
48+
#[catch(444, status = "<status>")]
49+
fn forward_444(status: Status) -> String { status.code.to_string() }
50+
#[catch(500, status = "<status>")]
51+
fn forward_500(status: Status) -> String { status.code.to_string() }
4452

4553
#[test]
4654
fn test_status_param() {
@@ -60,11 +68,11 @@ fn test_status_param() {
6068
}
6169

6270
#[catch(404)]
63-
fn bad_req_untyped(_: Status, _: &Request<'_>) -> &'static str { "404" }
64-
#[catch(404)]
65-
fn bad_req_string(_: &String, _: Status, _: &Request<'_>) -> &'static str { "404 String" }
66-
#[catch(404)]
67-
fn bad_req_tuple(_: &(), _: Status, _: &Request<'_>) -> &'static str { "404 ()" }
71+
fn bad_req_untyped() -> &'static str { "404" }
72+
#[catch(404, error = "<_e>")]
73+
fn bad_req_string(_e: &String) -> &'static str { "404 String" }
74+
#[catch(404, error = "<_e>")]
75+
fn bad_req_tuple(_e: &()) -> &'static str { "404 ()" }
6876

6977
#[test]
7078
fn test_typed_catchers() {

core/codegen/tests/route-raw.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[macro_use] extern crate rocket;
22

33
use rocket::local::blocking::Client;
4+
use rocket_http::Method;
45

56
// Test that raw idents can be used for route parameter names
67

@@ -15,8 +16,8 @@ fn swap(r#raw: String, bare: String) -> String {
1516
}
1617

1718
#[catch(400)]
18-
fn catch(r#raw: &rocket::Request<'_>) -> String {
19-
format!("{}", raw.method())
19+
fn catch(r#raw: Method) -> String {
20+
format!("{}", raw)
2021
}
2122

2223
#[test]

core/lib/src/catcher/catcher.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,21 @@ use super::ErasedError;
7676
/// ```rust,no_run
7777
/// #[macro_use] extern crate rocket;
7878
///
79-
/// use rocket::Request;
80-
/// use rocket::http::Status;
79+
/// use rocket::http::{Status, uri::Origin};
8180
///
8281
/// #[catch(500)]
8382
/// fn internal_error() -> &'static str {
8483
/// "Whoops! Looks like we messed up."
8584
/// }
8685
///
8786
/// #[catch(404)]
88-
/// fn not_found(req: &Request) -> String {
89-
/// format!("I couldn't find '{}'. Try something else?", req.uri())
87+
/// fn not_found(uri: &Origin) -> String {
88+
/// format!("I couldn't find '{}'. Try something else?", uri)
9089
/// }
9190
///
92-
/// #[catch(default)]
93-
/// fn default(status: Status, req: &Request) -> String {
94-
/// format!("{} ({})", status, req.uri())
91+
/// #[catch(default, status = "<status>")]
92+
/// fn default(status: Status, uri: &Origin) -> String {
93+
/// format!("{} ({})", status, uri)
9594
/// }
9695
///
9796
/// #[launch]
@@ -100,13 +99,6 @@ use super::ErasedError;
10099
/// }
101100
/// ```
102101
///
103-
/// A function decorated with `#[catch]` may take zero, one, or two arguments.
104-
/// It's type signature must be one of the following, where `R:`[`Responder`]:
105-
///
106-
/// * `fn() -> R`
107-
/// * `fn(`[`&Request`]`) -> R`
108-
/// * `fn(`[`Status`]`, `[`&Request`]`) -> R`
109-
///
110102
/// See the [`catch`] documentation for full details.
111103
///
112104
/// [`catch`]: crate::catch

core/lib/src/rocket.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,16 @@ impl Rocket<Build> {
379379
///
380380
/// ```rust,no_run
381381
/// # #[macro_use] extern crate rocket;
382-
/// use rocket::Request;
382+
/// use rocket::http::uri::Origin;
383383
///
384384
/// #[catch(500)]
385385
/// fn internal_error() -> &'static str {
386386
/// "Whoops! Looks like we messed up."
387387
/// }
388388
///
389389
/// #[catch(404)]
390-
/// fn not_found(req: &Request) -> String {
391-
/// format!("I couldn't find '{}'. Try something else?", req.uri())
390+
/// fn not_found(uri: &Origin) -> String {
391+
/// format!("I couldn't find '{}'. Try something else?", uri)
392392
/// }
393393
///
394394
/// #[launch]

core/lib/tests/catcher-cookies-1213.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#[macro_use] extern crate rocket;
22

3-
use rocket::request::Request;
43
use rocket::http::CookieJar;
54

65
#[catch(404)]
7-
fn not_found(request: &Request<'_>) -> &'static str {
8-
request.cookies().add(("not_found", "404"));
6+
fn not_found(jar: &CookieJar<'_>) -> &'static str {
7+
jar.add(("not_found", "404"));
98
"404 - Not Found"
109
}
1110

docs/guide/05-requests.md

+11-18
Original file line numberDiff line numberDiff line change
@@ -1981,14 +1981,14 @@ Application processing is fallible. Errors arise from the following sources:
19811981
* A routing failure.
19821982

19831983
If any of these occur, Rocket returns an error to the client. To generate the
1984-
error, Rocket invokes the _catcher_ corresponding to the error's status code and
1985-
scope. Catchers are similar to routes except in that:
1984+
error, Rocket invokes the _catcher_ corresponding to the error's status code,
1985+
scope, and type. Catchers are similar to routes except in that:
19861986

19871987
1. Catchers are only invoked on error conditions.
19881988
2. Catchers are declared with the `catch` attribute.
19891989
3. Catchers are _registered_ with [`register()`] instead of [`mount()`].
19901990
4. Any modifications to cookies are cleared before a catcher is invoked.
1991-
5. Error catchers cannot invoke guards.
1991+
// 5. Error catchers cannot invoke guards.
19921992
6. Error catchers should not fail to produce a response.
19931993
7. Catchers are scoped to a path prefix.
19941994

@@ -2000,26 +2000,20 @@ instance, to declare a catcher for `404 Not Found` errors, you'd write:
20002000
# #[macro_use] extern crate rocket;
20012001
# fn main() {}
20022002

2003-
use rocket::Request;
2004-
20052003
#[catch(404)]
2006-
fn not_found(req: &Request) { /* .. */ }
2004+
fn not_found() { /* .. */ }
20072005
```
20082006

2009-
Catchers may take zero, one, or two arguments. If the catcher takes one
2010-
argument, it must be of type [`&Request`]. It it takes two, they must be of type
2011-
[`Status`] and [`&Request`], in that order. As with routes, the return type must
2012-
implement `Responder`. A concrete implementation may look like:
2007+
TODO: See the catcher documentation
20132008

20142009
```rust
20152010
# #[macro_use] extern crate rocket;
20162011
# fn main() {}
2017-
2018-
# use rocket::Request;
2012+
# use rocket::http::uri::Origin;
20192013

20202014
#[catch(404)]
2021-
fn not_found(req: &Request) -> String {
2022-
format!("Sorry, '{}' is not a valid path.", req.uri())
2015+
fn not_found(uri: &Origin) -> String {
2016+
format!("Sorry, '{}' is not a valid path.", uri)
20232017
}
20242018
```
20252019

@@ -2032,8 +2026,7 @@ looks like:
20322026
```rust
20332027
# #[macro_use] extern crate rocket;
20342028

2035-
# use rocket::Request;
2036-
# #[catch(404)] fn not_found(req: &Request) { /* .. */ }
2029+
# #[catch(404)] fn not_found() { /* .. */ }
20372030

20382031
fn main() {
20392032
rocket::build().register("/", catchers![not_found]);
@@ -2106,8 +2099,8 @@ similarly be registered with [`register()`]:
21062099
use rocket::Request;
21072100
use rocket::http::Status;
21082101

2109-
#[catch(default)]
2110-
fn default_catcher(status: Status, request: &Request) { /* .. */ }
2102+
#[catch(default, status = "<status>")]
2103+
fn default_catcher(status: Status) { /* .. */ }
21112104

21122105
#[launch]
21132106
fn rocket() -> _ {

examples/error-handling/src/tests.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ fn forced_error() {
2525
assert_eq!(response.into_string().unwrap(), expected.0);
2626

2727
let request = client.get("/405");
28-
let expected = super::default_catcher(Status::MethodNotAllowed, request.inner());
28+
let expected = super::default_catcher(Status::MethodNotAllowed, request.uri());
2929
let response = request.dispatch();
3030
assert_eq!(response.status(), Status::MethodNotAllowed);
3131
assert_eq!(response.into_string().unwrap(), expected.1);
3232

3333
let request = client.get("/533");
34-
let expected = super::default_catcher(Status::new(533), request.inner());
34+
let expected = super::default_catcher(Status::new(533), request.uri());
3535
let response = request.dispatch();
3636
assert_eq!(response.status(), Status::new(533));
3737
assert_eq!(response.into_string().unwrap(), expected.1);
3838

3939
let request = client.get("/700");
40-
let expected = super::default_catcher(Status::InternalServerError, request.inner());
40+
let expected = super::default_catcher(Status::InternalServerError, request.uri());
4141
let response = request.dispatch();
4242
assert_eq!(response.status(), Status::InternalServerError);
4343
assert_eq!(response.into_string().unwrap(), expected.1);
@@ -51,8 +51,7 @@ fn test_hello_invalid_age() {
5151
let request = client.get(format!("/hello/{}", path));
5252
let expected = super::param_error(
5353
&IntErr(path.split_once("/").unwrap().1.parse::<i8>().unwrap_err()),
54-
Status::UnprocessableEntity,
55-
request.inner()
54+
request.uri()
5655
);
5756
let response = request.dispatch();
5857
assert_eq!(response.status(), Status::UnprocessableEntity);
@@ -62,7 +61,7 @@ fn test_hello_invalid_age() {
6261
{
6362
let path = &"foo/bar/baz";
6463
let request = client.get(format!("/hello/{}", path));
65-
let expected = super::hello_not_found(request.inner());
64+
let expected = super::hello_not_found(request.uri());
6665
let response = request.dispatch();
6766
assert_eq!(response.status(), Status::NotFound);
6867
assert_eq!(response.into_string().unwrap(), expected.0);

examples/responders/src/main.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn maybe_redir(name: &str) -> Result<&'static str, Redirect> {
122122

123123
/***************************** `content` Responders ***************************/
124124

125-
use rocket::Request;
125+
use rocket::http::{Accept, uri::Origin};
126126
use rocket::response::content;
127127

128128
// NOTE: This example explicitly uses the `RawJson` type from
@@ -144,14 +144,14 @@ fn json() -> content::RawJson<&'static str> {
144144
}
145145

146146
#[catch(404)]
147-
fn not_found(request: &Request<'_>) -> content::RawHtml<String> {
148-
let html = match request.format() {
149-
Some(ref mt) if !(mt.is_xml() || mt.is_html()) => {
147+
fn not_found(format: Option<&Accept>, uri: &Origin) -> content::RawHtml<String> {
148+
let html = match format {
149+
Some(ref mt) if !mt.media_types().any(|m| m.is_xml() || m.is_html()) => {
150150
format!("<p>'{}' requests are not supported.</p>", mt)
151151
}
152152
_ => format!("<p>Sorry, '{}' is an invalid path! Try \
153153
/hello/&lt;name&gt;/&lt;age&gt; instead.</p>",
154-
request.uri())
154+
uri)
155155
};
156156

157157
content::RawHtml(html)

examples/templating/src/hbs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rocket::Request;
1+
use rocket::http::uri::Origin;
22
use rocket::response::Redirect;
33

44
use rocket_dyn_templates::{Template, handlebars, context};
@@ -28,9 +28,9 @@ pub fn about() -> Template {
2828
}
2929

3030
#[catch(404)]
31-
pub fn not_found(req: &Request<'_>) -> Template {
31+
pub fn not_found(uri: &Origin<'_>) -> Template {
3232
Template::render("hbs/error/404", context! {
33-
uri: req.uri()
33+
uri,
3434
})
3535
}
3636

examples/templating/src/tera.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rocket::Request;
1+
use rocket::http::uri::Origin;
22
use rocket::response::Redirect;
33

44
use rocket_dyn_templates::{Template, tera::Tera, context};
@@ -25,9 +25,9 @@ pub fn about() -> Template {
2525
}
2626

2727
#[catch(404)]
28-
pub fn not_found(req: &Request<'_>) -> Template {
28+
pub fn not_found(uri: &Origin<'_>) -> Template {
2929
Template::render("tera/error/404", context! {
30-
uri: req.uri()
30+
uri,
3131
})
3232
}
3333

0 commit comments

Comments
 (0)