2020-07-05 18:35:36 +00:00
|
|
|
use rocket::local::blocking::Client;
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::http::Status;
|
2017-01-02 06:55:08 +00:00
|
|
|
|
2020-07-30 06:07:22 +00:00
|
|
|
#[test]
|
|
|
|
fn test_hello() {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(super::rocket()).unwrap();
|
2017-01-02 06:55:08 +00:00
|
|
|
|
2020-07-30 06:07:22 +00:00
|
|
|
let (name, age) = ("Arthur", 42);
|
|
|
|
let uri = format!("/hello/{}/{}", name, age);
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get(uri).dispatch();
|
2020-07-30 06:07:22 +00:00
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
2024-03-20 07:00:33 +00:00
|
|
|
assert_eq!(response.into_string().unwrap(), super::hello(name, age));
|
2017-01-02 06:55:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
2021-03-26 04:36:00 +00:00
|
|
|
fn forced_error() {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(super::rocket()).unwrap();
|
2020-07-30 06:07:22 +00:00
|
|
|
|
|
|
|
let request = client.get("/404");
|
2021-03-26 04:36:00 +00:00
|
|
|
let expected = super::general_not_found();
|
2020-07-30 06:07:22 +00:00
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
assert_eq!(response.into_string().unwrap(), expected.0);
|
|
|
|
|
|
|
|
let request = client.get("/405");
|
|
|
|
let expected = super::default_catcher(Status::MethodNotAllowed, request.inner());
|
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::MethodNotAllowed);
|
|
|
|
assert_eq!(response.into_string().unwrap(), expected.1);
|
|
|
|
|
|
|
|
let request = client.get("/533");
|
2021-04-28 11:54:06 +00:00
|
|
|
let expected = super::default_catcher(Status::new(533), request.inner());
|
2020-07-30 06:07:22 +00:00
|
|
|
let response = request.dispatch();
|
2021-04-28 11:54:06 +00:00
|
|
|
assert_eq!(response.status(), Status::new(533));
|
2020-07-30 06:07:22 +00:00
|
|
|
assert_eq!(response.into_string().unwrap(), expected.1);
|
|
|
|
|
|
|
|
let request = client.get("/700");
|
|
|
|
let expected = super::default_catcher(Status::InternalServerError, request.inner());
|
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::InternalServerError);
|
|
|
|
assert_eq!(response.into_string().unwrap(), expected.1);
|
2017-01-02 06:55:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_hello_invalid_age() {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(super::rocket()).unwrap();
|
2020-07-30 06:07:22 +00:00
|
|
|
|
Improve forwarding status code precision.
Previously, the `NotFound` status code was used to signal many kinds of
recoverable, forwarding errors. This included validation errors, incorrect
Content-Type errors, and more.
This commit modifies the status code used to forward in these instances to more
precisely indicate the forwarding condition. In particular:
* Parameter `FromParam` errors now forward as 422 (`UnprocessableEntity`).
* Query paramater errors now forward as 422 (`UnprocessableEntity`).
* Use of incorrect form content-type forwards as 413 (`UnsupportedMediaType`).
* `WebSocket` guard now forwards as 400 (`BadRequest`).
* `&Host`, `&Accept`, `&ContentType`, `IpAddr`, and `SocketAddr` all forward
with a 500 (`InternalServerError`).
Additionally, the `IntoOutcome` trait was overhauled to support functionality
previously offered by methods on `Outcome`. The `Outcome::forward()` method now
requires a status code to use for the forwarding outcome.
Finally, logging of `Outcome`s now includes the relevant status code.
Resolves #2626.
2023-10-31 23:27:03 +00:00
|
|
|
for path in &["Ford/-129", "Trillian/128"] {
|
|
|
|
let request = client.get(format!("/hello/{}", path));
|
|
|
|
let expected = super::default_catcher(Status::UnprocessableEntity, request.inner());
|
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::UnprocessableEntity);
|
|
|
|
assert_eq!(response.into_string().unwrap(), expected.1);
|
|
|
|
}
|
|
|
|
|
2024-03-20 07:00:33 +00:00
|
|
|
{
|
|
|
|
let path = &"foo/bar/baz";
|
2021-03-26 04:36:00 +00:00
|
|
|
let request = client.get(format!("/hello/{}", path));
|
|
|
|
let expected = super::hello_not_found(request.inner());
|
2020-07-30 06:07:22 +00:00
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
assert_eq!(response.into_string().unwrap(), expected.0);
|
2017-01-02 06:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 04:36:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hello_sergio() {
|
|
|
|
let client = Client::tracked(super::rocket()).unwrap();
|
|
|
|
|
Improve forwarding status code precision.
Previously, the `NotFound` status code was used to signal many kinds of
recoverable, forwarding errors. This included validation errors, incorrect
Content-Type errors, and more.
This commit modifies the status code used to forward in these instances to more
precisely indicate the forwarding condition. In particular:
* Parameter `FromParam` errors now forward as 422 (`UnprocessableEntity`).
* Query paramater errors now forward as 422 (`UnprocessableEntity`).
* Use of incorrect form content-type forwards as 413 (`UnsupportedMediaType`).
* `WebSocket` guard now forwards as 400 (`BadRequest`).
* `&Host`, `&Accept`, `&ContentType`, `IpAddr`, and `SocketAddr` all forward
with a 500 (`InternalServerError`).
Additionally, the `IntoOutcome` trait was overhauled to support functionality
previously offered by methods on `Outcome`. The `Outcome::forward()` method now
requires a status code to use for the forwarding outcome.
Finally, logging of `Outcome`s now includes the relevant status code.
Resolves #2626.
2023-10-31 23:27:03 +00:00
|
|
|
for path in &["oops", "-129"] {
|
|
|
|
let request = client.get(format!("/hello/Sergio/{}", path));
|
|
|
|
let expected = super::sergio_error();
|
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::UnprocessableEntity);
|
|
|
|
assert_eq!(response.into_string().unwrap(), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
for path in &["foo/bar", "/foo/bar/baz"] {
|
2021-03-26 04:36:00 +00:00
|
|
|
let request = client.get(format!("/hello/Sergio/{}", path));
|
|
|
|
let expected = super::sergio_error();
|
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
assert_eq!(response.into_string().unwrap(), expected);
|
|
|
|
}
|
|
|
|
}
|