Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a path as a guard in route handler macro #2771

Merged
merged 2 commits into from Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion actix-web-codegen/CHANGES.md
@@ -1,7 +1,8 @@
# Changes

## Unreleased - 2021-xx-xx

### Fixed
- Allow a path as a guard in route handler macro, such as `get`, `post`, as the `actix-web` document says

## 4.0.0 - 2022-02-24
- Version aligned with `actix-web` and will remain in sync going forward.
Expand Down
6 changes: 3 additions & 3 deletions actix-web-codegen/src/route.rs
Expand Up @@ -4,7 +4,7 @@ use actix_router::ResourceDef;
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
use syn::{parse_macro_input, AttributeArgs, Ident, LitStr, NestedMeta};
use syn::{parse_macro_input, AttributeArgs, Ident, LitStr, NestedMeta, Path};

enum ResourceType {
Async,
Expand Down Expand Up @@ -77,7 +77,7 @@ impl TryFrom<&syn::LitStr> for MethodType {
struct Args {
path: syn::LitStr,
resource_name: Option<syn::LitStr>,
guards: Vec<Ident>,
guards: Vec<Path>,
wrappers: Vec<syn::Type>,
methods: HashSet<MethodType>,
}
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Args {
}
} else if nv.path.is_ident("guard") {
if let syn::Lit::Str(lit) = nv.lit {
guards.push(Ident::new(&lit.value(), Span::call_site()));
guards.push(lit.parse::<Path>()?);
} else {
return Err(syn::Error::new_spanned(
nv.lit,
Expand Down
22 changes: 22 additions & 0 deletions actix-web-codegen/tests/test_macro.rs
Expand Up @@ -96,6 +96,21 @@ async fn custom_resource_name_test<'a>(req: actix_web::HttpRequest) -> impl Resp
HttpResponse::Ok()
}

mod guard_module {
use actix_web::{guard::GuardContext, http::header};

pub fn guard(ctx: &GuardContext) -> bool {
ctx.header::<header::Accept>()
.map(|h| h.preference() == "image/*")
.unwrap_or(false)
}
}

#[get("/test/guard", guard = "guard_module::guard")]
async fn guard_test() -> impl Responder {
HttpResponse::Ok()
}

pub struct ChangeStatusCode;

impl<S, B> Transform<S, ServiceRequest> for ChangeStatusCode
Expand Down Expand Up @@ -187,6 +202,7 @@ async fn test_body() {
.service(test_handler)
.service(route_test)
.service(custom_resource_name_test)
.service(guard_test)
});
let request = srv.request(http::Method::GET, srv.url("/test"));
let response = request.send().await.unwrap();
Expand Down Expand Up @@ -245,6 +261,12 @@ async fn test_body() {
let request = srv.request(http::Method::GET, srv.url("/custom_resource_name"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());

let request = srv
.request(http::Method::GET, srv.url("/test/guard"))
.insert_header(("Accept", "image/*"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}

#[actix_rt::test]
Expand Down