Rocket/examples/fairings/src/tests.rs
Jeb Rosen 560f0977d3 Revamp testing system for async.
* body_string_wait and body_bytes_wait are removed; use `.await` instead
* `dispatch()` is now an async fn and must be .await-ed
* Add `#[rocket::async_test]` macro, similar in purpose to `tokio::test`
* Tests now use either `rocket::async_test(async { })` or
  `#[rocket::async_test]` in order to `.await` the futures returned
  from `dispatch()` and `body_{string,bytes}()`
* Update 'test.sh' to reflect the tests that should be passing.

Broken:

* Cloned dispatch and mut_dispatch() with a live previous response now both fail, due to a (partial) check for mutable aliasing in LocalRequest.
* Some tests are still failing and need example-specific changes.
2020-07-11 09:24:28 -07:00

39 lines
1.2 KiB
Rust

use super::rocket;
use rocket::local::Client;
#[rocket::async_test]
async fn rewrite_get_put() {
let client = Client::new(rocket()).unwrap();
let mut response = client.get("/").dispatch().await;
assert_eq!(response.body_string().await, Some("Hello, fairings!".into()));
}
#[rocket::async_test]
async fn counts() {
let client = Client::new(rocket()).unwrap();
// Issue 1 GET request.
client.get("/").dispatch().await;
// Check the GET count, taking into account _this_ GET request.
let mut response = client.get("/counts").dispatch().await;
assert_eq!(response.body_string().await, Some("Get: 2\nPost: 0".into()));
// Issue 1 more GET request and a POST.
client.get("/").dispatch().await;
client.post("/").dispatch().await;
// Check the counts.
let mut response = client.get("/counts").dispatch().await;
assert_eq!(response.body_string().await, Some("Get: 4\nPost: 1".into()));
}
#[rocket::async_test]
async fn token() {
let client = Client::new(rocket()).unwrap();
// Ensure the token is '123', which is what we have in `Rocket.toml`.
let mut res = client.get("/token").dispatch().await;
assert_eq!(res.body_string().await, Some("123".into()));
}