From 06975bfaea63af971ea59b161c2a5e660ffd0e4d Mon Sep 17 00:00:00 2001 From: Jeb Rosen Date: Sun, 5 Jul 2020 11:35:36 -0700 Subject: [PATCH] Use the blocking testing API everywhere. Co-authored-by: Sergio Benitez --- contrib/lib/src/templates/mod.rs | 6 +- .../lib/tests/compress_responder.rs.disabled | 2 +- .../lib/tests/compression_fairing.rs.disabled | 2 +- contrib/lib/tests/helmet.rs | 22 ++--- contrib/lib/tests/static_files.rs | 86 +++++++++---------- contrib/lib/tests/templates.rs | 44 +++++----- core/codegen/tests/expansion.rs | 30 +++---- core/codegen/tests/route-data.rs | 20 ++--- core/codegen/tests/route-format.rs | 68 +++++++-------- core/codegen/tests/route-ranking.rs | 30 +++---- core/codegen/tests/route.rs | 22 ++--- core/lib/src/response/status.rs | 28 +++--- .../lib/tests/absolute-uris-okay-issue-443.rs | 12 +-- .../conditionally-set-server-header-996.rs | 14 +-- core/lib/tests/derive-reexports.rs | 16 ++-- .../fairing_before_head_strip-issue-546.rs | 18 ++-- .../lib/tests/flash-lazy-removes-issue-466.rs | 24 +++--- core/lib/tests/form_method-issue-45.rs | 20 ++--- .../lib/tests/form_value_decoding-issue-82.rs | 28 +++--- core/lib/tests/head_handling.rs | 26 +++--- core/lib/tests/limits.rs | 40 ++++----- .../local-request-content-type-issue-505.rs | 38 ++++---- .../local_request_private_cookie-issue-368.rs | 20 ++--- core/lib/tests/nested-fairing-attaches.rs | 24 +++--- .../tests/precise-content-type-matching.rs | 16 ++-- .../tests/redirect_from_catcher-issue-113.rs | 10 +-- core/lib/tests/route_guard.rs | 22 ++--- core/lib/tests/segments-issues-41-86.rs | 12 +-- core/lib/tests/strict_and_lenient_forms.rs | 32 +++---- core/lib/tests/unsound-local-request-1312.rs | 66 +++++++------- .../tests/uri-percent-encoding-issue-808.rs | 22 ++--- examples/config/tests/common/mod.rs | 10 +-- examples/content_types/src/tests.rs | 36 ++++---- examples/cookies/src/tests.rs | 30 +++---- examples/errors/src/tests.rs | 22 ++--- examples/fairings/src/tests.rs | 42 ++++----- examples/form_validation/src/tests.rs | 40 ++++----- examples/handlebars_templates/src/tests.rs | 24 +++--- examples/hello_2018/src/tests.rs | 34 ++++---- examples/hello_person/src/tests.rs | 36 ++++---- examples/hello_world/src/tests.rs | 12 +-- examples/json/src/tests.rs | 40 ++++----- examples/managed_queue/src/tests.rs | 14 +-- examples/manual_routes/src/tests.rs | 24 +++--- examples/msgpack/src/tests.rs | 22 ++--- examples/optional_redirect/src/tests.rs | 32 +++---- examples/pastebin/src/tests.rs | 60 ++++++------- examples/query_params/src/tests.rs | 48 +++++------ examples/ranking/src/tests.rs | 24 +++--- examples/raw_sqlite/src/tests.rs | 12 +-- examples/raw_upload/src/tests.rs | 22 ++--- examples/redirect/src/tests.rs | 24 +++--- examples/request_guard/src/main.rs | 16 ++-- examples/request_local_state/src/tests.rs | 12 +-- examples/session/src/tests.rs | 54 ++++++------ examples/state/src/tests.rs | 28 +++--- examples/static_files/src/tests.rs | 48 +++++------ examples/stream/src/tests.rs | 22 ++--- examples/tera_templates/src/tests.rs | 24 +++--- examples/testing/src/main.rs | 15 +++- examples/tls/src/tests.rs | 12 +-- examples/todo/src/tests.rs | 34 ++++---- examples/uuid/src/tests.rs | 30 +++---- site/guide/8-testing.md | 18 ++-- 64 files changed, 864 insertions(+), 877 deletions(-) diff --git a/contrib/lib/src/templates/mod.rs b/contrib/lib/src/templates/mod.rs index 7a5df83b..4e879764 100644 --- a/contrib/lib/src/templates/mod.rs +++ b/contrib/lib/src/templates/mod.rs @@ -328,12 +328,11 @@ impl Template { /// use std::collections::HashMap; /// /// use rocket_contrib::templates::Template; - /// use rocket::local::asynchronous::Client; + /// use rocket::local::blocking::Client; /// /// fn main() { - /// # rocket::async_test(async { /// let rocket = rocket::ignite().attach(Template::fairing()); - /// let client = Client::new(rocket).await.expect("valid rocket"); + /// let client = Client::new(rocket).expect("valid rocket"); /// /// // Create a `context`. Here, just an empty `HashMap`. /// let mut context = HashMap::new(); @@ -341,7 +340,6 @@ impl Template { /// # context.insert("test", "test"); /// # #[allow(unused_variables)] /// let template = Template::show(client.cargo(), "index", context); - /// # }); /// } /// ``` #[inline] diff --git a/contrib/lib/tests/compress_responder.rs.disabled b/contrib/lib/tests/compress_responder.rs.disabled index 73384803..ee47f989 100644 --- a/contrib/lib/tests/compress_responder.rs.disabled +++ b/contrib/lib/tests/compress_responder.rs.disabled @@ -9,7 +9,7 @@ mod compress_responder_tests { use rocket::http::hyper::header::{ContentEncoding, Encoding}; use rocket::http::Status; use rocket::http::{ContentType, Header}; - use rocket::local::asynchronous::Client; + use rocket::local::blocking::Client; use rocket::response::{Content, Response}; use rocket_contrib::compression::Compress; diff --git a/contrib/lib/tests/compression_fairing.rs.disabled b/contrib/lib/tests/compression_fairing.rs.disabled index cabab825..91b280a1 100644 --- a/contrib/lib/tests/compression_fairing.rs.disabled +++ b/contrib/lib/tests/compression_fairing.rs.disabled @@ -10,7 +10,7 @@ mod compression_fairing_tests { use rocket::http::hyper::header::{ContentEncoding, Encoding}; use rocket::http::Status; use rocket::http::{ContentType, Header}; - use rocket::local::asynchronous::Client; + use rocket::local::blocking::Client; use rocket::response::{Content, Response}; use rocket_contrib::compression::Compression; diff --git a/contrib/lib/tests/helmet.rs b/contrib/lib/tests/helmet.rs index 192449a0..ca68a799 100644 --- a/contrib/lib/tests/helmet.rs +++ b/contrib/lib/tests/helmet.rs @@ -7,7 +7,7 @@ extern crate rocket; #[cfg(feature = "helmet")] mod helmet_tests { use rocket::http::{Status, uri::Uri}; - use rocket::local::asynchronous::{Client, LocalResponse}; + use rocket::local::blocking::{Client, LocalResponse}; use rocket_contrib::helmet::*; use time::Duration; @@ -34,15 +34,15 @@ mod helmet_tests { macro_rules! dispatch { ($helmet:expr, $closure:expr) => {{ let rocket = rocket::ignite().mount("/", routes![hello]).attach($helmet); - let client = Client::new(rocket).await.unwrap(); - let response = client.get("/").dispatch().await; + let client = Client::new(rocket).unwrap(); + let response = client.get("/").dispatch(); assert_eq!(response.status(), Status::Ok); $closure(response) }} } - #[rocket::async_test] - async fn default_headers_test() { + #[test] + fn default_headers_test() { dispatch!(SpaceHelmet::default(), |response: LocalResponse<'_>| { assert_header!(response, "X-XSS-Protection", "1"); assert_header!(response, "X-Frame-Options", "SAMEORIGIN"); @@ -50,8 +50,8 @@ mod helmet_tests { }) } - #[rocket::async_test] - async fn disable_headers_test() { + #[test] + fn disable_headers_test() { let helmet = SpaceHelmet::default().disable::(); dispatch!(helmet, |response: LocalResponse<'_>| { assert_header!(response, "X-Frame-Options", "SAMEORIGIN"); @@ -84,8 +84,8 @@ mod helmet_tests { }); } - #[rocket::async_test] - async fn additional_headers_test() { + #[test] + fn additional_headers_test() { let helmet = SpaceHelmet::default() .enable(Hsts::default()) .enable(ExpectCt::default()) @@ -108,8 +108,8 @@ mod helmet_tests { }) } - #[rocket::async_test] - async fn uri_test() { + #[test] + fn uri_test() { let allow_uri = Uri::parse("https://www.google.com").unwrap(); let report_uri = Uri::parse("https://www.google.com").unwrap(); let enforce_uri = Uri::parse("https://www.google.com").unwrap(); diff --git a/contrib/lib/tests/static_files.rs b/contrib/lib/tests/static_files.rs index e1685441..d12755a4 100644 --- a/contrib/lib/tests/static_files.rs +++ b/contrib/lib/tests/static_files.rs @@ -8,7 +8,7 @@ mod static_tests { use rocket::{self, Rocket, Route}; use rocket_contrib::serve::{StaticFiles, Options}; use rocket::http::Status; - use rocket::local::asynchronous::Client; + use rocket::local::blocking::Client; fn static_root() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")) @@ -45,9 +45,9 @@ mod static_tests { "inner/", ]; - async fn assert_file(client: &Client, prefix: &str, path: &str, exists: bool) { + fn assert_file(client: &Client, prefix: &str, path: &str, exists: bool) { let full_path = format!("/{}/{}", prefix, path); - let response = client.get(full_path).dispatch().await; + let response = client.get(full_path).dispatch(); if exists { assert_eq!(response.status(), Status::Ok); @@ -59,52 +59,52 @@ mod static_tests { let mut file = File::open(path).expect("open file"); let mut expected_contents = String::new(); file.read_to_string(&mut expected_contents).expect("read file"); - assert_eq!(response.into_string().await, Some(expected_contents)); + assert_eq!(response.into_string(), Some(expected_contents)); } else { assert_eq!(response.status(), Status::NotFound); } } - async fn assert_all(client: &Client, prefix: &str, paths: &[&str], exist: bool) { + fn assert_all(client: &Client, prefix: &str, paths: &[&str], exist: bool) { for path in paths.iter() { - assert_file(client, prefix, path, exist).await; + assert_file(client, prefix, path, exist); } } - #[rocket::async_test] - async fn test_static_no_index() { - let client = Client::new(rocket()).await.expect("valid rocket"); - assert_all(&client, "no_index", REGULAR_FILES, true).await; - assert_all(&client, "no_index", HIDDEN_FILES, false).await; - assert_all(&client, "no_index", INDEXED_DIRECTORIES, false).await; + #[test] + fn test_static_no_index() { + let client = Client::new(rocket()).expect("valid rocket"); + assert_all(&client, "no_index", REGULAR_FILES, true); + assert_all(&client, "no_index", HIDDEN_FILES, false); + assert_all(&client, "no_index", INDEXED_DIRECTORIES, false); } - #[rocket::async_test] - async fn test_static_hidden() { - let client = Client::new(rocket()).await.expect("valid rocket"); - assert_all(&client, "dots", REGULAR_FILES, true).await; - assert_all(&client, "dots", HIDDEN_FILES, true).await; - assert_all(&client, "dots", INDEXED_DIRECTORIES, false).await; + #[test] + fn test_static_hidden() { + let client = Client::new(rocket()).expect("valid rocket"); + assert_all(&client, "dots", REGULAR_FILES, true); + assert_all(&client, "dots", HIDDEN_FILES, true); + assert_all(&client, "dots", INDEXED_DIRECTORIES, false); } - #[rocket::async_test] - async fn test_static_index() { - let client = Client::new(rocket()).await.expect("valid rocket"); - assert_all(&client, "index", REGULAR_FILES, true).await; - assert_all(&client, "index", HIDDEN_FILES, false).await; - assert_all(&client, "index", INDEXED_DIRECTORIES, true).await; + #[test] + fn test_static_index() { + let client = Client::new(rocket()).expect("valid rocket"); + assert_all(&client, "index", REGULAR_FILES, true); + assert_all(&client, "index", HIDDEN_FILES, false); + assert_all(&client, "index", INDEXED_DIRECTORIES, true); - assert_all(&client, "default", REGULAR_FILES, true).await; - assert_all(&client, "default", HIDDEN_FILES, false).await; - assert_all(&client, "default", INDEXED_DIRECTORIES, true).await; + assert_all(&client, "default", REGULAR_FILES, true); + assert_all(&client, "default", HIDDEN_FILES, false); + assert_all(&client, "default", INDEXED_DIRECTORIES, true); } - #[rocket::async_test] - async fn test_static_all() { - let client = Client::new(rocket()).await.expect("valid rocket"); - assert_all(&client, "both", REGULAR_FILES, true).await; - assert_all(&client, "both", HIDDEN_FILES, true).await; - assert_all(&client, "both", INDEXED_DIRECTORIES, true).await; + #[test] + fn test_static_all() { + let client = Client::new(rocket()).expect("valid rocket"); + assert_all(&client, "both", REGULAR_FILES, true); + assert_all(&client, "both", HIDDEN_FILES, true); + assert_all(&client, "both", INDEXED_DIRECTORIES, true); } #[test] @@ -121,8 +121,8 @@ mod static_tests { } } - #[rocket::async_test] - async fn test_forwarding() { + #[test] + fn test_forwarding() { use rocket::http::RawStr; use rocket::{get, routes}; @@ -133,19 +133,19 @@ mod static_tests { fn catch_two(a: &RawStr, b: &RawStr) -> String { format!("{}/{}", a, b) } let rocket = rocket().mount("/default", routes![catch_one, catch_two]); - let client = Client::new(rocket).await.expect("valid rocket"); + let client = Client::new(rocket).expect("valid rocket"); - let response = client.get("/default/ireallydontexist").dispatch().await; + let response = client.get("/default/ireallydontexist").dispatch(); assert_eq!(response.status(), Status::Ok); - assert_eq!(response.into_string().await.unwrap(), "ireallydontexist"); + assert_eq!(response.into_string().unwrap(), "ireallydontexist"); - let response = client.get("/default/idont/exist").dispatch().await; + let response = client.get("/default/idont/exist").dispatch(); assert_eq!(response.status(), Status::Ok); - assert_eq!(response.into_string().await.unwrap(), "idont/exist"); + assert_eq!(response.into_string().unwrap(), "idont/exist"); - assert_all(&client, "both", REGULAR_FILES, true).await; - assert_all(&client, "both", HIDDEN_FILES, true).await; - assert_all(&client, "both", INDEXED_DIRECTORIES, true).await; + assert_all(&client, "both", REGULAR_FILES, true); + assert_all(&client, "both", HIDDEN_FILES, true); + assert_all(&client, "both", INDEXED_DIRECTORIES, true); } #[test] diff --git a/contrib/lib/tests/templates.rs b/contrib/lib/tests/templates.rs index 08c568b2..b8e7900b 100644 --- a/contrib/lib/tests/templates.rs +++ b/contrib/lib/tests/templates.rs @@ -42,7 +42,7 @@ mod templates_tests { use super::*; use std::collections::HashMap; use rocket::http::Status; - use rocket::local::asynchronous::Client; + use rocket::local::blocking::Client; const UNESCAPED_EXPECTED: &'static str = "\nh_start\ntitle: _test_\nh_end\n\n\n