1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-26 15:07:42 +02:00

added support for creating custom methods with route macro (#2969)

Co-authored-by: Rob Ede <robjtede@icloud.com>
Closes https://github.com/actix/actix-web/issues/2893
This commit is contained in:
edgerunnergit
2023-02-06 18:10:41 +05:30
committed by GitHub
parent b933ed4456
commit 65c0545a7a
10 changed files with 169 additions and 59 deletions

View File

@ -0,0 +1,19 @@
use actix_web_codegen::*;
use actix_web::http::Method;
use std::str::FromStr;
#[route("/", method = "hello")]
async fn index() -> String {
"Hello World!".to_owned()
}
#[actix_web::main]
async fn main() {
use actix_web::App;
let srv = actix_test::start(|| App::new().service(index));
let request = srv.request(Method::from_str("hello").unwrap(), srv.url("/"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}

View File

@ -0,0 +1,19 @@
error: HTTP method must be uppercase: `hello`
--> tests/trybuild/route-custom-lowercase.rs:5:23
|
5 | #[route("/", method = "hello")]
| ^^^^^^^
error[E0277]: the trait bound `fn() -> impl std::future::Future<Output = String> {index}: HttpServiceFactory` is not satisfied
--> tests/trybuild/route-custom-lowercase.rs:14:55
|
14 | let srv = actix_test::start(|| App::new().service(index));
| ------- ^^^^^ the trait `HttpServiceFactory` is not implemented for `fn() -> impl std::future::Future<Output = String> {index}`
| |
| required by a bound introduced by this call
|
note: required by a bound in `App::<T>::service`
--> $WORKSPACE/actix-web/src/app.rs
|
| F: HttpServiceFactory + 'static,
| ^^^^^^^^^^^^^^^^^^ required by this bound in `App::<T>::service`

View File

@ -1,4 +1,6 @@
use actix_web_codegen::*;
use actix_web::http::Method;
use std::str::FromStr;
#[route("/", method="UNEXPECTED")]
async fn index() -> String {
@ -11,7 +13,7 @@ async fn main() {
let srv = actix_test::start(|| App::new().service(index));
let request = srv.get("/");
let request = srv.request(Method::from_str("UNEXPECTED").unwrap(), srv.url("/"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}

View File

@ -1,19 +0,0 @@
error: Unexpected HTTP method: `UNEXPECTED`
--> tests/trybuild/route-unexpected-method-fail.rs:3:21
|
3 | #[route("/", method="UNEXPECTED")]
| ^^^^^^^^^^^^
error[E0277]: the trait bound `fn() -> impl std::future::Future<Output = String> {index}: HttpServiceFactory` is not satisfied
--> tests/trybuild/route-unexpected-method-fail.rs:12:55
|
12 | let srv = actix_test::start(|| App::new().service(index));
| ------- ^^^^^ the trait `HttpServiceFactory` is not implemented for `fn() -> impl std::future::Future<Output = String> {index}`
| |
| required by a bound introduced by this call
|
note: required by a bound in `App::<T>::service`
--> $WORKSPACE/actix-web/src/app.rs
|
| F: HttpServiceFactory + 'static,
| ^^^^^^^^^^^^^^^^^^ required by this bound in `App::<T>::service`