From 7a28b32f6d12b51d26a51e93a182e3e83504eb68 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 15 Apr 2019 07:44:07 -0700 Subject: [PATCH] Rename test::call_success to test::call_service --- CHANGES.md | 2 ++ src/app.rs | 10 +++++----- src/config.rs | 26 ++++++++++++++++++++++++-- src/middleware/cors.rs | 30 +++++++++++++++--------------- src/middleware/errhandlers.rs | 4 ++-- src/middleware/identity.rs | 8 ++++---- src/request.rs | 6 +++--- src/resource.rs | 16 ++++++++-------- src/route.rs | 12 ++++++------ src/scope.rs | 6 +++--- src/test.rs | 4 ++-- 11 files changed, 74 insertions(+), 50 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 3cc6e5ef3..8909f3e29 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,8 @@ * Rename `RouterConfig` to `ServiceConfig` +* Rename `test::call_success` to `test::call_service` + ### Fixed * Fixed `TestRequest::app_data()` diff --git a/src/app.rs b/src/app.rs index bf6f25800..c0bbc8b2b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -436,7 +436,7 @@ mod tests { use super::*; use crate::http::{header, HeaderValue, Method, StatusCode}; use crate::service::{ServiceRequest, ServiceResponse}; - use crate::test::{block_on, call_success, init_service, TestRequest}; + use crate::test::{block_on, call_service, init_service, TestRequest}; use crate::{web, Error, HttpResponse}; #[test] @@ -527,7 +527,7 @@ mod tests { .route("/test", web::get().to(|| HttpResponse::Ok())), ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), @@ -543,7 +543,7 @@ mod tests { .wrap(md), ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), @@ -567,7 +567,7 @@ mod tests { .service(web::resource("/test").to(|| HttpResponse::Ok())), ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), @@ -591,7 +591,7 @@ mod tests { }), ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), diff --git a/src/config.rs b/src/config.rs index 07bfebcf1..a8caba4d2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -255,8 +255,8 @@ mod tests { use actix_service::Service; use super::*; - use crate::http::StatusCode; - use crate::test::{block_on, init_service, TestRequest}; + use crate::http::{Method, StatusCode}; + use crate::test::{block_on, call_service, init_service, TestRequest}; use crate::{web, App, HttpResponse}; #[test] @@ -300,4 +300,26 @@ mod tests { let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); } + + #[test] + fn test_service() { + let mut srv = init_service(App::new().configure(|cfg| { + cfg.service( + web::resource("/test").route(web::get().to(|| HttpResponse::Created())), + ) + .route("/index.html", web::get().to(|| HttpResponse::Ok())); + })); + + let req = TestRequest::with_uri("/test") + .method(Method::GET) + .to_request(); + let resp = call_service(&mut srv, req); + assert_eq!(resp.status(), StatusCode::CREATED); + + let req = TestRequest::with_uri("/index.html") + .method(Method::GET) + .to_request(); + let resp = call_service(&mut srv, req); + assert_eq!(resp.status(), StatusCode::OK); + } } diff --git a/src/middleware/cors.rs b/src/middleware/cors.rs index 813822c98..12cd0b83a 100644 --- a/src/middleware/cors.rs +++ b/src/middleware/cors.rs @@ -848,7 +848,7 @@ mod tests { let req = TestRequest::with_header("Origin", "https://www.example.com") .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!(resp.status(), StatusCode::OK); } @@ -859,7 +859,7 @@ mod tests { let req = TestRequest::with_header("Origin", "https://www.example.com") .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!(resp.status(), StatusCode::OK); } @@ -879,7 +879,7 @@ mod tests { assert!(cors.inner.validate_allowed_method(req.head()).is_err()); assert!(cors.inner.validate_allowed_headers(req.head()).is_err()); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); let req = TestRequest::with_header("Origin", "https://www.example.com") @@ -899,7 +899,7 @@ mod tests { .method(Method::OPTIONS) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"*"[..], resp.headers() @@ -945,7 +945,7 @@ mod tests { .method(Method::OPTIONS) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!(resp.status(), StatusCode::OK); } @@ -984,7 +984,7 @@ mod tests { .method(Method::GET) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!(resp.status(), StatusCode::OK); } @@ -993,7 +993,7 @@ mod tests { let mut cors = Cors::new().disable_preflight().finish(test::ok_service()); let req = TestRequest::default().method(Method::GET).to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert!(resp .headers() .get(header::ACCESS_CONTROL_ALLOW_ORIGIN) @@ -1002,7 +1002,7 @@ mod tests { let req = TestRequest::with_header("Origin", "https://www.example.com") .method(Method::OPTIONS) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"https://www.example.com"[..], resp.headers() @@ -1029,7 +1029,7 @@ mod tests { .method(Method::OPTIONS) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"*"[..], resp.headers() @@ -1075,7 +1075,7 @@ mod tests { let req = TestRequest::with_header("Origin", "https://www.example.com") .method(Method::OPTIONS) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"Accept, Origin"[..], resp.headers().get(header::VARY).unwrap().as_bytes() @@ -1091,7 +1091,7 @@ mod tests { .method(Method::OPTIONS) .header(header::ACCESS_CONTROL_REQUEST_METHOD, "POST") .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); let origins_str = resp .headers() @@ -1115,7 +1115,7 @@ mod tests { .method(Method::GET) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"https://example.com"[..], resp.headers() @@ -1128,7 +1128,7 @@ mod tests { .method(Method::GET) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"https://example.org"[..], resp.headers() @@ -1151,7 +1151,7 @@ mod tests { .method(Method::OPTIONS) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"https://example.com"[..], resp.headers() @@ -1165,7 +1165,7 @@ mod tests { .method(Method::OPTIONS) .to_srv_request(); - let resp = test::call_success(&mut cors, req); + let resp = test::call_service(&mut cors, req); assert_eq!( &b"https://example.org"[..], resp.headers() diff --git a/src/middleware/errhandlers.rs b/src/middleware/errhandlers.rs index 56745630b..aa36b6a4d 100644 --- a/src/middleware/errhandlers.rs +++ b/src/middleware/errhandlers.rs @@ -172,7 +172,7 @@ mod tests { ) .unwrap(); - let resp = test::call_success(&mut mw, TestRequest::default().to_srv_request()); + let resp = test::call_service(&mut mw, TestRequest::default().to_srv_request()); assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001"); } @@ -198,7 +198,7 @@ mod tests { ) .unwrap(); - let resp = test::call_success(&mut mw, TestRequest::default().to_srv_request()); + let resp = test::call_service(&mut mw, TestRequest::default().to_srv_request()); assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001"); } } diff --git a/src/middleware/identity.rs b/src/middleware/identity.rs index 5bc3f923c..6027aaa7d 100644 --- a/src/middleware/identity.rs +++ b/src/middleware/identity.rs @@ -500,15 +500,15 @@ mod tests { })), ); let resp = - test::call_success(&mut srv, TestRequest::with_uri("/index").to_request()); + test::call_service(&mut srv, TestRequest::with_uri("/index").to_request()); assert_eq!(resp.status(), StatusCode::OK); let resp = - test::call_success(&mut srv, TestRequest::with_uri("/login").to_request()); + test::call_service(&mut srv, TestRequest::with_uri("/login").to_request()); assert_eq!(resp.status(), StatusCode::OK); let c = resp.response().cookies().next().unwrap().to_owned(); - let resp = test::call_success( + let resp = test::call_service( &mut srv, TestRequest::with_uri("/index") .cookie(c.clone()) @@ -516,7 +516,7 @@ mod tests { ); assert_eq!(resp.status(), StatusCode::CREATED); - let resp = test::call_success( + let resp = test::call_service( &mut srv, TestRequest::with_uri("/logout") .cookie(c.clone()) diff --git a/src/request.rs b/src/request.rs index 082d36b61..5823c08ca 100644 --- a/src/request.rs +++ b/src/request.rs @@ -324,7 +324,7 @@ mod tests { use super::*; use crate::dev::{ResourceDef, ResourceMap}; use crate::http::{header, StatusCode}; - use crate::test::{call_success, init_service, TestRequest}; + use crate::test::{call_service, init_service, TestRequest}; use crate::{web, App, HttpResponse}; #[test] @@ -453,7 +453,7 @@ mod tests { )); let req = TestRequest::default().to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); let mut srv = init_service(App::new().data(10u32).service( @@ -467,7 +467,7 @@ mod tests { )); let req = TestRequest::default().to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); } } diff --git a/src/resource.rs b/src/resource.rs index 15abcadaf..1f1e6e157 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -545,7 +545,7 @@ mod tests { use crate::http::{header, HeaderValue, Method, StatusCode}; use crate::service::{ServiceRequest, ServiceResponse}; - use crate::test::{call_success, init_service, TestRequest}; + use crate::test::{call_service, init_service, TestRequest}; use crate::{web, App, Error, HttpResponse}; fn md( @@ -577,7 +577,7 @@ mod tests { ), ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), @@ -603,7 +603,7 @@ mod tests { ), ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), @@ -618,7 +618,7 @@ mod tests { sleep(Duration::from_millis(100)).then(|_| HttpResponse::Ok()) }))); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); } @@ -634,13 +634,13 @@ mod tests { }), ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); let req = TestRequest::with_uri("/test") .method(Method::POST) .to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED); let mut srv = init_service( @@ -654,13 +654,13 @@ mod tests { ); let req = TestRequest::with_uri("/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); let req = TestRequest::with_uri("/test") .method(Method::POST) .to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); } } diff --git a/src/route.rs b/src/route.rs index b1c7b2ab4..7b9f36a64 100644 --- a/src/route.rs +++ b/src/route.rs @@ -422,7 +422,7 @@ mod tests { use tokio_timer::sleep; use crate::http::{Method, StatusCode}; - use crate::test::{call_success, init_service, TestRequest}; + use crate::test::{call_service, init_service, TestRequest}; use crate::{error, web, App, HttpResponse}; #[test] @@ -450,31 +450,31 @@ mod tests { let req = TestRequest::with_uri("/test") .method(Method::GET) .to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); let req = TestRequest::with_uri("/test") .method(Method::POST) .to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::CREATED); let req = TestRequest::with_uri("/test") .method(Method::PUT) .to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); let req = TestRequest::with_uri("/test") .method(Method::DELETE) .to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); let req = TestRequest::with_uri("/test") .method(Method::HEAD) .to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED); } } diff --git a/src/scope.rs b/src/scope.rs index 0ac73a2e8..81bf84d23 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -535,7 +535,7 @@ mod tests { use crate::dev::{Body, ResponseBody}; use crate::http::{header, HeaderValue, Method, StatusCode}; use crate::service::{ServiceRequest, ServiceResponse}; - use crate::test::{block_on, call_success, init_service, TestRequest}; + use crate::test::{block_on, call_service, init_service, TestRequest}; use crate::{guard, web, App, Error, HttpRequest, HttpResponse}; #[test] @@ -912,7 +912,7 @@ mod tests { web::resource("/test").route(web::get().to(|| HttpResponse::Ok())), ))); let req = TestRequest::with_uri("/app/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), @@ -938,7 +938,7 @@ mod tests { ), ); let req = TestRequest::with_uri("/app/test").to_request(); - let resp = call_success(&mut srv, req); + let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::OK); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), diff --git a/src/test.rs b/src/test.rs index 638bcdce6..89562c61a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -137,11 +137,11 @@ where /// let req = test::TestRequest::with_uri("/test").to_request(); /// /// // Call application -/// let resp = test::call_success(&mut app, req); +/// let resp = test::call_service(&mut app, req); /// assert_eq!(resp.status(), StatusCode::OK); /// } /// ``` -pub fn call_success(app: &mut S, req: R) -> S::Response +pub fn call_service(app: &mut S, req: R) -> S::Response where S: Service, Error = E>, E: std::fmt::Debug,