From 898ef570803c983aaf92e2d3a0ecf7e5d278cbb6 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 23 Apr 2019 21:21:49 -0700 Subject: [PATCH] Fix async web::Data factory handling --- CHANGES.md | 5 +++++ src/app.rs | 27 +++++++++++++++++++++++++-- src/app_service.rs | 2 +- src/config.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f4fdd6af7..565362263 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,11 @@ * `.to_async()` handler can return `Responder` type #792 +### Fixed + +* Fix async web::Data factory handling + + ## [1.0.0-beta.1] - 2019-04-20 ### Added diff --git a/src/app.rs b/src/app.rs index c0bbc8b2b..bb6d2aef8 100644 --- a/src/app.rs +++ b/src/app.rs @@ -431,13 +431,14 @@ where #[cfg(test)] mod tests { use actix_service::Service; + use bytes::Bytes; use futures::{Future, IntoFuture}; use super::*; use crate::http::{header, HeaderValue, Method, StatusCode}; use crate::service::{ServiceRequest, ServiceResponse}; - use crate::test::{block_on, call_service, init_service, TestRequest}; - use crate::{web, Error, HttpResponse}; + use crate::test::{block_on, call_service, init_service, read_body, TestRequest}; + use crate::{web, Error, HttpRequest, HttpResponse}; #[test] fn test_default_resource() { @@ -598,4 +599,26 @@ mod tests { HeaderValue::from_static("0001") ); } + + #[test] + fn test_external_resource() { + let mut srv = init_service( + App::new() + .external_resource("youtube", "https://youtube.com/watch/{video_id}") + .route( + "/test", + web::get().to(|req: HttpRequest| { + HttpResponse::Ok().body(format!( + "{}", + req.url_for("youtube", &["12345"]).unwrap() + )) + }), + ), + ); + let req = TestRequest::with_uri("/test").to_request(); + let resp = call_service(&mut srv, req); + assert_eq!(resp.status(), StatusCode::OK); + let body = read_body(resp); + assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345")); + } } diff --git a/src/app_service.rs b/src/app_service.rs index 63bf84e76..7229a2301 100644 --- a/src/app_service.rs +++ b/src/app_service.rs @@ -162,7 +162,7 @@ where } } - if self.endpoint.is_some() { + if self.endpoint.is_some() && self.data.is_empty() { Ok(Async::Ready(AppInitService { service: self.endpoint.take().unwrap(), rmap: self.rmap.clone(), diff --git a/src/config.rs b/src/config.rs index a8caba4d2..4c4bfa220 100644 --- a/src/config.rs +++ b/src/config.rs @@ -253,11 +253,14 @@ impl ServiceConfig { #[cfg(test)] mod tests { use actix_service::Service; + use bytes::Bytes; + use futures::Future; + use tokio_timer::sleep; use super::*; use crate::http::{Method, StatusCode}; - use crate::test::{block_on, call_service, init_service, TestRequest}; - use crate::{web, App, HttpResponse}; + use crate::test::{block_on, call_service, init_service, read_body, TestRequest}; + use crate::{web, App, HttpRequest, HttpResponse}; #[test] fn test_data() { @@ -277,7 +280,12 @@ mod tests { #[test] fn test_data_factory() { let cfg = |cfg: &mut ServiceConfig| { - cfg.data_factory(|| Ok::<_, ()>(10usize)); + cfg.data_factory(|| { + sleep(std::time::Duration::from_millis(50)).then(|_| { + println!("READY"); + Ok::<_, ()>(10usize) + }) + }); }; let mut srv = @@ -301,6 +309,33 @@ mod tests { assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); } + #[test] + fn test_external_resource() { + let mut srv = init_service( + App::new() + .configure(|cfg| { + cfg.external_resource( + "youtube", + "https://youtube.com/watch/{video_id}", + ); + }) + .route( + "/test", + web::get().to(|req: HttpRequest| { + HttpResponse::Ok().body(format!( + "{}", + req.url_for("youtube", &["12345"]).unwrap() + )) + }), + ), + ); + let req = TestRequest::with_uri("/test").to_request(); + let resp = call_service(&mut srv, req); + assert_eq!(resp.status(), StatusCode::OK); + let body = read_body(resp); + assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345")); + } + #[test] fn test_service() { let mut srv = init_service(App::new().configure(|cfg| {