Rocket/examples/raw_upload/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.3 KiB
Rust

use rocket::local::Client;
use rocket::http::{Status, ContentType};
use std::env;
use std::io::Read;
use std::fs::{self, File};
const UPLOAD_CONTENTS: &str = "Hey! I'm going to be uploaded. :D Yay!";
#[rocket::async_test]
async fn test_index() {
let client = Client::new(super::rocket()).unwrap();
let mut res = client.get("/").dispatch().await;
assert_eq!(res.body_string().await, Some(super::index().to_string()));
}
#[rocket::async_test]
async fn test_raw_upload() {
// Delete the upload file before we begin.
let upload_file = env::temp_dir().join("upload.txt");
let _ = fs::remove_file(&upload_file);
// Do the upload. Make sure we get the expected results.
let client = Client::new(super::rocket()).unwrap();
let mut res = client.post("/upload")
.header(ContentType::Plain)
.body(UPLOAD_CONTENTS)
.dispatch().await;
assert_eq!(res.status(), Status::Ok);
assert_eq!(res.body_string().await, Some(UPLOAD_CONTENTS.len().to_string()));
// Ensure we find the body in the /tmp/upload.txt file.
let mut file_contents = String::new();
let mut file = File::open(&upload_file).expect("open upload.txt file");
file.read_to_string(&mut file_contents).expect("read upload.txt");
assert_eq!(&file_contents, UPLOAD_CONTENTS);
}