actix_web/
service.rs

1use std::{
2    cell::{Ref, RefMut},
3    fmt, net,
4    rc::Rc,
5};
6
7use actix_http::{
8    body::{BoxBody, EitherBody, MessageBody},
9    header::HeaderMap,
10    BoxedPayloadStream, Extensions, HttpMessage, Method, Payload, RequestHead, Response,
11    ResponseHead, StatusCode, Uri, Version,
12};
13use actix_router::{IntoPatterns, Path, Patterns, Resource, ResourceDef, Url};
14use actix_service::{
15    boxed::{BoxService, BoxServiceFactory},
16    IntoServiceFactory, ServiceFactory,
17};
18#[cfg(feature = "cookies")]
19use cookie::{Cookie, ParseError as CookieParseError};
20
21use crate::{
22    config::{AppConfig, AppService},
23    dev::ensure_leading_slash,
24    guard::{Guard, GuardContext},
25    info::ConnectionInfo,
26    rmap::ResourceMap,
27    Error, FromRequest, HttpRequest, HttpResponse,
28};
29
30pub(crate) type BoxedHttpService = BoxService<ServiceRequest, ServiceResponse<BoxBody>, Error>;
31pub(crate) type BoxedHttpServiceFactory =
32    BoxServiceFactory<(), ServiceRequest, ServiceResponse<BoxBody>, Error, ()>;
33
34pub trait HttpServiceFactory {
35    fn register(self, config: &mut AppService);
36}
37
38impl<T: HttpServiceFactory> HttpServiceFactory for Vec<T> {
39    fn register(self, config: &mut AppService) {
40        self.into_iter()
41            .for_each(|factory| factory.register(config));
42    }
43}
44
45pub(crate) trait AppServiceFactory {
46    fn register(&mut self, config: &mut AppService);
47}
48
49pub(crate) struct ServiceFactoryWrapper<T> {
50    factory: Option<T>,
51}
52
53impl<T> ServiceFactoryWrapper<T> {
54    pub fn new(factory: T) -> Self {
55        Self {
56            factory: Some(factory),
57        }
58    }
59}
60
61impl<T> AppServiceFactory for ServiceFactoryWrapper<T>
62where
63    T: HttpServiceFactory,
64{
65    fn register(&mut self, config: &mut AppService) {
66        if let Some(item) = self.factory.take() {
67            item.register(config)
68        }
69    }
70}
71
72/// A service level request wrapper.
73///
74/// Allows mutable access to request's internal structures.
75pub struct ServiceRequest {
76    req: HttpRequest,
77    payload: Payload,
78}
79
80impl ServiceRequest {
81    /// Construct `ServiceRequest` from parts.
82    pub(crate) fn new(req: HttpRequest, payload: Payload) -> Self {
83        Self { req, payload }
84    }
85
86    /// Deconstruct `ServiceRequest` into inner parts.
87    #[inline]
88    pub fn into_parts(self) -> (HttpRequest, Payload) {
89        (self.req, self.payload)
90    }
91
92    /// Returns mutable accessors to inner parts.
93    #[inline]
94    pub fn parts_mut(&mut self) -> (&mut HttpRequest, &mut Payload) {
95        (&mut self.req, &mut self.payload)
96    }
97
98    /// Returns immutable accessors to inner parts.
99    #[inline]
100    pub fn parts(&self) -> (&HttpRequest, &Payload) {
101        (&self.req, &self.payload)
102    }
103
104    /// Returns immutable accessor to inner [`HttpRequest`].
105    #[inline]
106    pub fn request(&self) -> &HttpRequest {
107        &self.req
108    }
109
110    /// Derives a type from this request using an [extractor](crate::FromRequest).
111    ///
112    /// Returns the `T` extractor's `Future` type which can be `await`ed. This is particularly handy
113    /// when you want to use an extractor in a middleware implementation.
114    ///
115    /// # Examples
116    /// ```
117    /// use actix_web::{
118    ///     dev::{ServiceRequest, ServiceResponse},
119    ///     web::Path, Error
120    /// };
121    ///
122    /// async fn my_helper(mut srv_req: ServiceRequest) -> Result<ServiceResponse, Error> {
123    ///     let path = srv_req.extract::<Path<(String, u32)>>().await?;
124    ///     // [...]
125    /// #   todo!()
126    /// }
127    /// ```
128    pub fn extract<T>(&mut self) -> <T as FromRequest>::Future
129    where
130        T: FromRequest,
131    {
132        T::from_request(&self.req, &mut self.payload)
133    }
134
135    /// Construct request from parts.
136    pub fn from_parts(req: HttpRequest, payload: Payload) -> Self {
137        #[cfg(debug_assertions)]
138        if Rc::strong_count(&req.inner) > 1 {
139            log::warn!("Cloning an `HttpRequest` might cause panics.");
140        }
141
142        Self { req, payload }
143    }
144
145    /// Construct `ServiceRequest` with no payload from given `HttpRequest`.
146    #[inline]
147    pub fn from_request(req: HttpRequest) -> Self {
148        ServiceRequest {
149            req,
150            payload: Payload::None,
151        }
152    }
153
154    /// Create `ServiceResponse` from this request and given response.
155    #[inline]
156    pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
157        let res = HttpResponse::from(res.into());
158        ServiceResponse::new(self.req, res)
159    }
160
161    /// Create `ServiceResponse` from this request and given error.
162    #[inline]
163    pub fn error_response<E: Into<Error>>(self, err: E) -> ServiceResponse {
164        let res = HttpResponse::from_error(err.into());
165        ServiceResponse::new(self.req, res)
166    }
167
168    /// Returns a reference to the request head.
169    #[inline]
170    pub fn head(&self) -> &RequestHead {
171        self.req.head()
172    }
173
174    /// Returns a mutable reference to the request head.
175    #[inline]
176    pub fn head_mut(&mut self) -> &mut RequestHead {
177        self.req.head_mut()
178    }
179
180    /// Returns the request URI.
181    #[inline]
182    pub fn uri(&self) -> &Uri {
183        &self.head().uri
184    }
185
186    /// Returns the request method.
187    #[inline]
188    pub fn method(&self) -> &Method {
189        &self.head().method
190    }
191
192    /// Returns the request version.
193    #[inline]
194    pub fn version(&self) -> Version {
195        self.head().version
196    }
197
198    /// Returns a reference to request headers.
199    #[inline]
200    pub fn headers(&self) -> &HeaderMap {
201        &self.head().headers
202    }
203
204    /// Returns a mutable reference to request headers.
205    #[inline]
206    pub fn headers_mut(&mut self) -> &mut HeaderMap {
207        &mut self.head_mut().headers
208    }
209
210    /// Returns request path.
211    #[inline]
212    pub fn path(&self) -> &str {
213        self.head().uri.path()
214    }
215
216    /// Counterpart to [`HttpRequest::query_string`].
217    #[inline]
218    pub fn query_string(&self) -> &str {
219        self.req.query_string()
220    }
221
222    /// Returns peer's socket address.
223    ///
224    /// See [`HttpRequest::peer_addr`] for more details.
225    ///
226    /// [`HttpRequest::peer_addr`]: crate::HttpRequest::peer_addr
227    #[inline]
228    pub fn peer_addr(&self) -> Option<net::SocketAddr> {
229        self.head().peer_addr
230    }
231
232    /// Returns a reference to connection info.
233    #[inline]
234    pub fn connection_info(&self) -> Ref<'_, ConnectionInfo> {
235        self.req.connection_info()
236    }
237
238    /// Counterpart to [`HttpRequest::match_info`].
239    #[inline]
240    pub fn match_info(&self) -> &Path<Url> {
241        self.req.match_info()
242    }
243
244    /// Returns a mutable reference to the path match information.
245    #[inline]
246    pub fn match_info_mut(&mut self) -> &mut Path<Url> {
247        self.req.match_info_mut()
248    }
249
250    /// Counterpart to [`HttpRequest::match_name`].
251    #[inline]
252    pub fn match_name(&self) -> Option<&str> {
253        self.req.match_name()
254    }
255
256    /// Counterpart to [`HttpRequest::match_pattern`].
257    #[inline]
258    pub fn match_pattern(&self) -> Option<String> {
259        self.req.match_pattern()
260    }
261
262    /// Returns a reference to the application's resource map.
263    /// Counterpart to [`HttpRequest::resource_map`].
264    #[inline]
265    pub fn resource_map(&self) -> &ResourceMap {
266        self.req.resource_map()
267    }
268
269    /// Counterpart to [`HttpRequest::app_config`].
270    #[inline]
271    pub fn app_config(&self) -> &AppConfig {
272        self.req.app_config()
273    }
274
275    /// Counterpart to [`HttpRequest::app_data`].
276    #[inline]
277    pub fn app_data<T: 'static>(&self) -> Option<&T> {
278        for container in self.req.inner.app_data.iter().rev() {
279            if let Some(data) = container.get::<T>() {
280                return Some(data);
281            }
282        }
283
284        None
285    }
286
287    /// Counterpart to [`HttpRequest::conn_data`].
288    #[inline]
289    pub fn conn_data<T: 'static>(&self) -> Option<&T> {
290        self.req.conn_data()
291    }
292
293    /// Return request cookies.
294    #[cfg(feature = "cookies")]
295    #[inline]
296    pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
297        self.req.cookies()
298    }
299
300    /// Return request cookie.
301    #[cfg(feature = "cookies")]
302    #[inline]
303    pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
304        self.req.cookie(name)
305    }
306
307    /// Set request payload.
308    #[inline]
309    pub fn set_payload(&mut self, payload: Payload) {
310        self.payload = payload;
311    }
312
313    /// Add data container to request's resolution set.
314    ///
315    /// In middleware, prefer [`extensions_mut`](ServiceRequest::extensions_mut) for request-local
316    /// data since it is assumed that the same app data is presented for every request.
317    pub fn add_data_container(&mut self, extensions: Rc<Extensions>) {
318        Rc::get_mut(&mut (self.req).inner)
319            .unwrap()
320            .app_data
321            .push(extensions);
322    }
323
324    /// Creates a context object for use with a routing [guard](crate::guard).
325    #[inline]
326    pub fn guard_ctx(&self) -> GuardContext<'_> {
327        GuardContext { req: self }
328    }
329}
330
331impl Resource for ServiceRequest {
332    type Path = Url;
333
334    #[inline]
335    fn resource_path(&mut self) -> &mut Path<Self::Path> {
336        self.match_info_mut()
337    }
338}
339
340impl HttpMessage for ServiceRequest {
341    type Stream = BoxedPayloadStream;
342
343    #[inline]
344    fn headers(&self) -> &HeaderMap {
345        &self.head().headers
346    }
347
348    #[inline]
349    fn extensions(&self) -> Ref<'_, Extensions> {
350        self.req.extensions()
351    }
352
353    #[inline]
354    fn extensions_mut(&self) -> RefMut<'_, Extensions> {
355        self.req.extensions_mut()
356    }
357
358    #[inline]
359    fn take_payload(&mut self) -> Payload<Self::Stream> {
360        self.payload.take()
361    }
362}
363
364impl fmt::Debug for ServiceRequest {
365    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
366        writeln!(
367            f,
368            "\nServiceRequest {:?} {}:{}",
369            self.head().version,
370            self.head().method,
371            self.path()
372        )?;
373        if !self.query_string().is_empty() {
374            writeln!(f, "  query: ?{:?}", self.query_string())?;
375        }
376        if !self.match_info().is_empty() {
377            writeln!(f, "  params: {:?}", self.match_info())?;
378        }
379        writeln!(f, "  headers:")?;
380        for (key, val) in self.headers().iter() {
381            writeln!(f, "    {:?}: {:?}", key, val)?;
382        }
383        Ok(())
384    }
385}
386
387/// A service level response wrapper.
388pub struct ServiceResponse<B = BoxBody> {
389    request: HttpRequest,
390    response: HttpResponse<B>,
391}
392
393impl ServiceResponse<BoxBody> {
394    /// Create service response from the error
395    pub fn from_err<E: Into<Error>>(err: E, request: HttpRequest) -> Self {
396        let response = HttpResponse::from_error(err);
397        ServiceResponse { request, response }
398    }
399}
400
401impl<B> ServiceResponse<B> {
402    /// Create service response instance
403    pub fn new(request: HttpRequest, response: HttpResponse<B>) -> Self {
404        ServiceResponse { request, response }
405    }
406
407    /// Create service response for error
408    #[inline]
409    pub fn error_response<E: Into<Error>>(self, err: E) -> ServiceResponse {
410        ServiceResponse::from_err(err, self.request)
411    }
412
413    /// Create service response
414    #[inline]
415    pub fn into_response<B1>(self, response: HttpResponse<B1>) -> ServiceResponse<B1> {
416        ServiceResponse::new(self.request, response)
417    }
418
419    /// Returns reference to original request.
420    #[inline]
421    pub fn request(&self) -> &HttpRequest {
422        &self.request
423    }
424
425    /// Returns reference to response.
426    #[inline]
427    pub fn response(&self) -> &HttpResponse<B> {
428        &self.response
429    }
430
431    /// Returns mutable reference to response.
432    #[inline]
433    pub fn response_mut(&mut self) -> &mut HttpResponse<B> {
434        &mut self.response
435    }
436
437    /// Returns response status code.
438    #[inline]
439    pub fn status(&self) -> StatusCode {
440        self.response.status()
441    }
442
443    /// Returns response's headers.
444    #[inline]
445    pub fn headers(&self) -> &HeaderMap {
446        self.response.headers()
447    }
448
449    /// Returns mutable response's headers.
450    #[inline]
451    pub fn headers_mut(&mut self) -> &mut HeaderMap {
452        self.response.headers_mut()
453    }
454
455    /// Destructures `ServiceResponse` into request and response components.
456    #[inline]
457    pub fn into_parts(self) -> (HttpRequest, HttpResponse<B>) {
458        (self.request, self.response)
459    }
460
461    /// Map the current body type to another using a closure. Returns a new response.
462    ///
463    /// Closure receives the response head and the current body type.
464    #[inline]
465    pub fn map_body<F, B2>(self, f: F) -> ServiceResponse<B2>
466    where
467        F: FnOnce(&mut ResponseHead, B) -> B2,
468    {
469        let response = self.response.map_body(f);
470
471        ServiceResponse {
472            response,
473            request: self.request,
474        }
475    }
476
477    #[inline]
478    pub fn map_into_left_body<R>(self) -> ServiceResponse<EitherBody<B, R>> {
479        self.map_body(|_, body| EitherBody::left(body))
480    }
481
482    #[inline]
483    pub fn map_into_right_body<L>(self) -> ServiceResponse<EitherBody<L, B>> {
484        self.map_body(|_, body| EitherBody::right(body))
485    }
486
487    #[inline]
488    pub fn map_into_boxed_body(self) -> ServiceResponse<BoxBody>
489    where
490        B: MessageBody + 'static,
491    {
492        self.map_body(|_, body| body.boxed())
493    }
494
495    /// Consumes the response and returns its body.
496    #[inline]
497    pub fn into_body(self) -> B {
498        self.response.into_body()
499    }
500}
501
502impl<B> From<ServiceResponse<B>> for HttpResponse<B> {
503    fn from(res: ServiceResponse<B>) -> HttpResponse<B> {
504        res.response
505    }
506}
507
508impl<B> From<ServiceResponse<B>> for Response<B> {
509    fn from(res: ServiceResponse<B>) -> Response<B> {
510        res.response.into()
511    }
512}
513
514impl<B> fmt::Debug for ServiceResponse<B>
515where
516    B: MessageBody,
517    B::Error: Into<Error>,
518{
519    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
520        let res = writeln!(
521            f,
522            "\nServiceResponse {:?} {}{}",
523            self.response.head().version,
524            self.response.head().status,
525            self.response.head().reason.unwrap_or(""),
526        );
527        let _ = writeln!(f, "  headers:");
528        for (key, val) in self.response.head().headers.iter() {
529            let _ = writeln!(f, "    {:?}: {:?}", key, val);
530        }
531        let _ = writeln!(f, "  body: {:?}", self.response.body().size());
532        res
533    }
534}
535
536pub struct WebService {
537    rdef: Patterns,
538    name: Option<String>,
539    guards: Vec<Box<dyn Guard>>,
540}
541
542impl WebService {
543    /// Create new `WebService` instance.
544    pub fn new<T: IntoPatterns>(path: T) -> Self {
545        WebService {
546            rdef: path.patterns(),
547            name: None,
548            guards: Vec::new(),
549        }
550    }
551
552    /// Set service name.
553    ///
554    /// Name is used for URL generation.
555    pub fn name(mut self, name: &str) -> Self {
556        self.name = Some(name.to_string());
557        self
558    }
559
560    /// Add match guard to a web service.
561    ///
562    /// ```
563    /// use actix_web::{web, guard, dev, App, Error, HttpResponse};
564    ///
565    /// async fn index(req: dev::ServiceRequest) -> Result<dev::ServiceResponse, Error> {
566    ///     Ok(req.into_response(HttpResponse::Ok().finish()))
567    /// }
568    ///
569    /// let app = App::new()
570    ///     .service(
571    ///         web::service("/app")
572    ///             .guard(guard::Header("content-type", "text/plain"))
573    ///             .finish(index)
574    ///     );
575    /// ```
576    pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
577        self.guards.push(Box::new(guard));
578        self
579    }
580
581    /// Set a service factory implementation and generate web service.
582    pub fn finish<T, F>(self, service: F) -> impl HttpServiceFactory
583    where
584        F: IntoServiceFactory<T, ServiceRequest>,
585        T: ServiceFactory<
586                ServiceRequest,
587                Config = (),
588                Response = ServiceResponse,
589                Error = Error,
590                InitError = (),
591            > + 'static,
592    {
593        WebServiceImpl {
594            srv: service.into_factory(),
595            rdef: self.rdef,
596            name: self.name,
597            guards: self.guards,
598        }
599    }
600}
601
602struct WebServiceImpl<T> {
603    srv: T,
604    rdef: Patterns,
605    name: Option<String>,
606    guards: Vec<Box<dyn Guard>>,
607}
608
609impl<T> HttpServiceFactory for WebServiceImpl<T>
610where
611    T: ServiceFactory<
612            ServiceRequest,
613            Config = (),
614            Response = ServiceResponse,
615            Error = Error,
616            InitError = (),
617        > + 'static,
618{
619    fn register(mut self, config: &mut AppService) {
620        let guards = if self.guards.is_empty() {
621            None
622        } else {
623            Some(std::mem::take(&mut self.guards))
624        };
625
626        let mut rdef = if config.is_root() || !self.rdef.is_empty() {
627            ResourceDef::new(ensure_leading_slash(self.rdef))
628        } else {
629            ResourceDef::new(self.rdef)
630        };
631
632        if let Some(ref name) = self.name {
633            rdef.set_name(name);
634        }
635
636        config.register_service(rdef, guards, self.srv, None)
637    }
638}
639
640/// Macro to help register different types of services at the same time.
641///
642/// The max number of services that can be grouped together is 12 and all must implement the
643/// [`HttpServiceFactory`] trait.
644///
645/// # Examples
646/// ```
647/// use actix_web::{services, web, App};
648///
649/// let services = services![
650///     web::resource("/test2").to(|| async { "test2" }),
651///     web::scope("/test3").route("/", web::get().to(|| async { "test3" }))
652/// ];
653///
654/// let app = App::new().service(services);
655///
656/// // services macro just convert multiple services to a tuple.
657/// // below would also work without importing the macro.
658/// let app = App::new().service((
659///     web::resource("/test2").to(|| async { "test2" }),
660///     web::scope("/test3").route("/", web::get().to(|| async { "test3" }))
661/// ));
662/// ```
663#[macro_export]
664macro_rules! services {
665    () => {()};
666    ($($x:expr),+ $(,)?) => {
667        ($($x,)+)
668    }
669}
670
671/// HttpServiceFactory trait impl for tuples
672macro_rules! service_tuple ({ $($T:ident)+ } => {
673    impl<$($T: HttpServiceFactory),+> HttpServiceFactory for ($($T,)+) {
674        #[allow(non_snake_case)]
675        fn register(self, config: &mut AppService) {
676            let ($($T,)*) = self;
677            $($T.register(config);)+
678        }
679    }
680});
681
682service_tuple! { A }
683service_tuple! { A B }
684service_tuple! { A B C }
685service_tuple! { A B C D }
686service_tuple! { A B C D E }
687service_tuple! { A B C D E F }
688service_tuple! { A B C D E F G }
689service_tuple! { A B C D E F G H }
690service_tuple! { A B C D E F G H I }
691service_tuple! { A B C D E F G H I J }
692service_tuple! { A B C D E F G H I J K }
693service_tuple! { A B C D E F G H I J K L }
694
695#[cfg(test)]
696mod tests {
697    use actix_service::Service;
698    use actix_utils::future::ok;
699
700    use super::*;
701    use crate::{
702        guard, http,
703        test::{self, init_service, TestRequest},
704        web, App,
705    };
706
707    #[actix_rt::test]
708    async fn test_service() {
709        let srv =
710            init_service(
711                App::new().service(web::service("/test").name("test").finish(
712                    |req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())),
713                )),
714            )
715            .await;
716        let req = TestRequest::with_uri("/test").to_request();
717        let resp = srv.call(req).await.unwrap();
718        assert_eq!(resp.status(), http::StatusCode::OK);
719
720        let srv =
721            init_service(
722                App::new().service(web::service("/test").guard(guard::Get()).finish(
723                    |req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())),
724                )),
725            )
726            .await;
727        let req = TestRequest::with_uri("/test")
728            .method(http::Method::PUT)
729            .to_request();
730        let resp = srv.call(req).await.unwrap();
731        assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);
732    }
733
734    // allow deprecated App::data
735    #[allow(deprecated)]
736    #[actix_rt::test]
737    async fn test_service_data() {
738        let srv = init_service(
739            App::new()
740                .data(42u32)
741                .service(
742                    web::service("/test")
743                        .name("test")
744                        .finish(|req: ServiceRequest| {
745                            assert_eq!(req.app_data::<web::Data<u32>>().unwrap().as_ref(), &42);
746                            ok(req.into_response(HttpResponse::Ok().finish()))
747                        }),
748                ),
749        )
750        .await;
751        let req = TestRequest::with_uri("/test").to_request();
752        let resp = srv.call(req).await.unwrap();
753        assert_eq!(resp.status(), http::StatusCode::OK);
754    }
755
756    #[test]
757    fn test_fmt_debug() {
758        let req = TestRequest::get()
759            .uri("/index.html?test=1")
760            .insert_header(("x-test", "111"))
761            .to_srv_request();
762        let s = format!("{:?}", req);
763        assert!(s.contains("ServiceRequest"));
764        assert!(s.contains("test=1"));
765        assert!(s.contains("x-test"));
766
767        let res = HttpResponse::Ok().insert_header(("x-test", "111")).finish();
768        let res = TestRequest::post()
769            .uri("/index.html?test=1")
770            .to_srv_response(res);
771
772        let s = format!("{:?}", res);
773        assert!(s.contains("ServiceResponse"));
774        assert!(s.contains("x-test"));
775    }
776
777    #[actix_rt::test]
778    async fn test_services_macro() {
779        let scoped = services![
780            web::service("/scoped_test1").name("scoped_test1").finish(
781                |req: ServiceRequest| async { Ok(req.into_response(HttpResponse::Ok().finish())) }
782            ),
783            web::resource("/scoped_test2").to(|| async { "test2" }),
784        ];
785
786        let services = services![
787            web::service("/test1")
788                .name("test")
789                .finish(|req: ServiceRequest| async {
790                    Ok(req.into_response(HttpResponse::Ok().finish()))
791                }),
792            web::resource("/test2").to(|| async { "test2" }),
793            web::scope("/test3").service(scoped)
794        ];
795
796        let srv = init_service(App::new().service(services)).await;
797
798        let req = TestRequest::with_uri("/test1").to_request();
799        let resp = srv.call(req).await.unwrap();
800        assert_eq!(resp.status(), http::StatusCode::OK);
801
802        let req = TestRequest::with_uri("/test2").to_request();
803        let resp = srv.call(req).await.unwrap();
804        assert_eq!(resp.status(), http::StatusCode::OK);
805
806        let req = TestRequest::with_uri("/test3/scoped_test1").to_request();
807        let resp = srv.call(req).await.unwrap();
808        assert_eq!(resp.status(), http::StatusCode::OK);
809
810        let req = TestRequest::with_uri("/test3/scoped_test2").to_request();
811        let resp = srv.call(req).await.unwrap();
812        assert_eq!(resp.status(), http::StatusCode::OK);
813    }
814
815    #[actix_rt::test]
816    async fn test_services_vec() {
817        let services = vec![
818            web::resource("/test1").to(|| async { "test1" }),
819            web::resource("/test2").to(|| async { "test2" }),
820        ];
821
822        let scoped = vec![
823            web::resource("/scoped_test1").to(|| async { "test1" }),
824            web::resource("/scoped_test2").to(|| async { "test2" }),
825        ];
826
827        let srv = init_service(
828            App::new()
829                .service(services)
830                .service(web::scope("/test3").service(scoped)),
831        )
832        .await;
833
834        let req = TestRequest::with_uri("/test1").to_request();
835        let resp = srv.call(req).await.unwrap();
836        assert_eq!(resp.status(), http::StatusCode::OK);
837
838        let req = TestRequest::with_uri("/test2").to_request();
839        let resp = srv.call(req).await.unwrap();
840        assert_eq!(resp.status(), http::StatusCode::OK);
841
842        let req = TestRequest::with_uri("/test3/scoped_test1").to_request();
843        let resp = srv.call(req).await.unwrap();
844        assert_eq!(resp.status(), http::StatusCode::OK);
845
846        let req = TestRequest::with_uri("/test3/scoped_test2").to_request();
847        let resp = srv.call(req).await.unwrap();
848        assert_eq!(resp.status(), http::StatusCode::OK);
849    }
850
851    #[actix_rt::test]
852    #[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
853    async fn cloning_request_panics() {
854        async fn index(_name: web::Path<(String,)>) -> &'static str {
855            ""
856        }
857
858        let app = test::init_service(
859            App::new()
860                .wrap_fn(|req, svc| {
861                    let (req, pl) = req.into_parts();
862                    let _req2 = req.clone();
863                    let req = ServiceRequest::from_parts(req, pl);
864                    svc.call(req)
865                })
866                .route("/", web::get().to(|| async { "" }))
867                .service(web::resource("/resource1/{name}/index.html").route(web::get().to(index))),
868        )
869        .await;
870
871        let req = test::TestRequest::default().to_request();
872        let _res = test::call_service(&app, req).await;
873    }
874
875    #[test]
876    fn define_services_macro_with_multiple_arguments() {
877        let result = services!(1, 2, 3);
878        assert_eq!(result, (1, 2, 3));
879    }
880
881    #[test]
882    fn define_services_macro_with_single_argument() {
883        let result = services!(1);
884        assert_eq!(result, (1,));
885    }
886
887    #[test]
888    fn define_services_macro_with_no_arguments() {
889        let result = services!();
890        let () = result;
891    }
892
893    #[test]
894    fn define_services_macro_with_trailing_comma() {
895        let result = services!(1, 2, 3,);
896        assert_eq!(result, (1, 2, 3));
897    }
898
899    #[test]
900    fn define_services_macro_with_comments_in_arguments() {
901        let result = services!(
902            1, // First comment
903            2, // Second comment
904            3  // Third comment
905        );
906
907        // Assert that comments are ignored and it correctly returns a tuple.
908        assert_eq!(result, (1, 2, 3));
909    }
910}