From 5801cb945d5af8a02faacdb39179edab31167da4 Mon Sep 17 00:00:00 2001 From: Matt Gathu Date: Sun, 13 Sep 2020 18:20:23 +0200 Subject: [PATCH] Add trybuild tests for route macro --- actix-web-codegen/tests/trybuild.rs | 5 +++++ .../tests/trybuild/route-duplicate-method-fail.rs | 15 +++++++++++++++ .../trybuild/route-duplicate-method-fail.stderr | 11 +++++++++++ .../tests/trybuild/route-missing-method-fail.rs | 15 +++++++++++++++ .../trybuild/route-missing-method-fail.stderr | 11 +++++++++++ actix-web-codegen/tests/trybuild/route-ok.rs | 15 +++++++++++++++ .../trybuild/route-unexpected-method-fail.rs | 15 +++++++++++++++ .../trybuild/route-unexpected-method-fail.stderr | 11 +++++++++++ 8 files changed, 98 insertions(+) create mode 100644 actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs create mode 100644 actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr create mode 100644 actix-web-codegen/tests/trybuild/route-missing-method-fail.rs create mode 100644 actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr create mode 100644 actix-web-codegen/tests/trybuild/route-ok.rs create mode 100644 actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs create mode 100644 actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr diff --git a/actix-web-codegen/tests/trybuild.rs b/actix-web-codegen/tests/trybuild.rs index b675947d3..3bdc616da 100644 --- a/actix-web-codegen/tests/trybuild.rs +++ b/actix-web-codegen/tests/trybuild.rs @@ -4,4 +4,9 @@ fn compile_macros() { t.pass("tests/trybuild/simple.rs"); t.compile_fail("tests/trybuild/simple-fail.rs"); + + t.pass("tests/trybuild/route-ok.rs"); + t.compile_fail("tests/trybuild/route-missing-method-fail.rs"); + t.compile_fail("tests/trybuild/route-duplicate-method-fail.rs"); + t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs"); } diff --git a/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs new file mode 100644 index 000000000..9ce980251 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs @@ -0,0 +1,15 @@ +use actix_web::*; + +#[route("/", method="GET", method="GET")] +async fn index() -> impl Responder { + HttpResponse::Ok() +} + +#[actix_web::main] +async fn main() { + let srv = test::start(|| App::new().service(index)); + + let request = srv.get("/"); + let response = request.send().await.unwrap(); + assert!(response.status().is_success()); +} diff --git a/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr new file mode 100644 index 000000000..8bf857c4d --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr @@ -0,0 +1,11 @@ +error: HTTP Method defined more than once: `GET` + --> $DIR/route-duplicate-method-fail.rs:3:35 + | +3 | #[route("/", method="GET", method="GET")] + | ^^^^^ + +error[E0425]: cannot find value `index` in this scope + --> $DIR/route-duplicate-method-fail.rs:10:49 + | +10 | let srv = test::start(|| App::new().service(index)); + | ^^^^^ not found in this scope diff --git a/actix-web-codegen/tests/trybuild/route-missing-method-fail.rs b/actix-web-codegen/tests/trybuild/route-missing-method-fail.rs new file mode 100644 index 000000000..5c30b57ce --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-missing-method-fail.rs @@ -0,0 +1,15 @@ +use actix_web::*; + +#[route("/")] +async fn index() -> impl Responder { + HttpResponse::Ok() +} + +#[actix_web::main] +async fn main() { + let srv = test::start(|| App::new().service(index)); + + let request = srv.get("/"); + let response = request.send().await.unwrap(); + assert!(response.status().is_success()); +} diff --git a/actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr b/actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr new file mode 100644 index 000000000..a90f04edb --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr @@ -0,0 +1,11 @@ +error: The #[route(..)] macro requires at least one `method` attribute + --> $DIR/route-missing-method-fail.rs:3:1 + | +3 | #[route("/")] + | ^^^^^^^^^^^^^ + +error[E0425]: cannot find value `index` in this scope + --> $DIR/route-missing-method-fail.rs:10:49 + | +10 | let srv = test::start(|| App::new().service(index)); + | ^^^^^ not found in this scope diff --git a/actix-web-codegen/tests/trybuild/route-ok.rs b/actix-web-codegen/tests/trybuild/route-ok.rs new file mode 100644 index 000000000..bfac56e12 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-ok.rs @@ -0,0 +1,15 @@ +use actix_web::*; + +#[route("/", method="GET", method="HEAD")] +async fn index() -> impl Responder { + HttpResponse::Ok() +} + +#[actix_web::main] +async fn main() { + let srv = test::start(|| App::new().service(index)); + + let request = srv.get("/"); + let response = request.send().await.unwrap(); + assert!(response.status().is_success()); +} diff --git a/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs new file mode 100644 index 000000000..f4d8d9445 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs @@ -0,0 +1,15 @@ +use actix_web::*; + +#[route("/", method="UNEXPECTED")] +async fn index() -> impl Responder { + HttpResponse::Ok() +} + +#[actix_web::main] +async fn main() { + let srv = test::start(|| App::new().service(index)); + + let request = srv.get("/"); + let response = request.send().await.unwrap(); + assert!(response.status().is_success()); +} diff --git a/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr new file mode 100644 index 000000000..3fe49f774 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr @@ -0,0 +1,11 @@ +error: Unexpected HTTP Method: `UNEXPECTED` + --> $DIR/route-unexpected-method-fail.rs:3:21 + | +3 | #[route("/", method="UNEXPECTED")] + | ^^^^^^^^^^^^ + +error[E0425]: cannot find value `index` in this scope + --> $DIR/route-unexpected-method-fail.rs:10:49 + | +10 | let srv = test::start(|| App::new().service(index)); + | ^^^^^ not found in this scope