mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 10:32:55 +01:00
update tests to async handlers
This commit is contained in:
parent
e668acc596
commit
57981ca04a
@ -11,7 +11,7 @@
|
||||
//! use actix_cors::Cors;
|
||||
//! use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer};
|
||||
//!
|
||||
//! fn index(req: HttpRequest) -> &'static str {
|
||||
//! async fn index(req: HttpRequest) -> &'static str {
|
||||
//! "Hello world"
|
||||
//! }
|
||||
//!
|
||||
|
@ -1176,9 +1176,11 @@ mod tests {
|
||||
let mut srv =
|
||||
test::init_service(App::new().wrap(Compress::default()).service(
|
||||
web::resource("/").to(|| {
|
||||
NamedFile::open("Cargo.toml")
|
||||
.unwrap()
|
||||
.set_content_encoding(header::ContentEncoding::Identity)
|
||||
async {
|
||||
NamedFile::open("Cargo.toml")
|
||||
.unwrap()
|
||||
.set_content_encoding(header::ContentEncoding::Identity)
|
||||
}
|
||||
}),
|
||||
))
|
||||
.await;
|
||||
@ -1199,9 +1201,11 @@ mod tests {
|
||||
let mut srv =
|
||||
test::init_service(App::new().wrap(Compress::default()).service(
|
||||
web::resource("/").to(|| {
|
||||
NamedFile::open("Cargo.toml")
|
||||
.unwrap()
|
||||
.set_content_encoding(header::ContentEncoding::Gzip)
|
||||
async {
|
||||
NamedFile::open("Cargo.toml")
|
||||
.unwrap()
|
||||
.set_content_encoding(header::ContentEncoding::Gzip)
|
||||
}
|
||||
}),
|
||||
))
|
||||
.await;
|
||||
|
@ -388,6 +388,12 @@ impl BoxedResponseHead {
|
||||
pub fn new(status: StatusCode) -> Self {
|
||||
RESPONSE_POOL.with(|p| p.get_message(status))
|
||||
}
|
||||
|
||||
pub(crate) fn take(&mut self) -> Self {
|
||||
BoxedResponseHead {
|
||||
head: self.head.take(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for BoxedResponseHead {
|
||||
@ -406,7 +412,9 @@ impl std::ops::DerefMut for BoxedResponseHead {
|
||||
|
||||
impl Drop for BoxedResponseHead {
|
||||
fn drop(&mut self) {
|
||||
RESPONSE_POOL.with(|p| p.release(self.head.take().unwrap()))
|
||||
if let Some(head) = self.head.take() {
|
||||
RESPONSE_POOL.with(move |p| p.release(head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -288,10 +288,7 @@ impl Future for Response {
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
|
||||
Poll::Ready(Ok(Response {
|
||||
head: std::mem::replace(
|
||||
&mut self.head,
|
||||
BoxedResponseHead::new(StatusCode::OK),
|
||||
),
|
||||
head: self.head.take(),
|
||||
body: self.body.take_body(),
|
||||
error: self.error.take(),
|
||||
}))
|
||||
|
@ -16,7 +16,7 @@
|
||||
//! use actix_web::*;
|
||||
//! use actix_identity::{Identity, CookieIdentityPolicy, IdentityService};
|
||||
//!
|
||||
//! fn index(id: Identity) -> String {
|
||||
//! async fn index(id: Identity) -> String {
|
||||
//! // access request identity
|
||||
//! if let Some(id) = id.identity() {
|
||||
//! format!("Welcome! {}", id)
|
||||
@ -25,12 +25,12 @@
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! fn login(id: Identity) -> HttpResponse {
|
||||
//! async fn login(id: Identity) -> HttpResponse {
|
||||
//! id.remember("User1".to_owned()); // <- remember identity
|
||||
//! HttpResponse::Ok().finish()
|
||||
//! }
|
||||
//!
|
||||
//! fn logout(id: Identity) -> HttpResponse {
|
||||
//! async fn logout(id: Identity) -> HttpResponse {
|
||||
//! id.forget(); // <- remove identity
|
||||
//! HttpResponse::Ok().finish()
|
||||
//! }
|
||||
@ -764,11 +764,13 @@ mod tests {
|
||||
.secure(false)
|
||||
.name(COOKIE_NAME))))
|
||||
.service(web::resource("/").to(|id: Identity| {
|
||||
let identity = id.identity();
|
||||
if identity.is_none() {
|
||||
id.remember(COOKIE_LOGIN.to_string())
|
||||
async move {
|
||||
let identity = id.identity();
|
||||
if identity.is_none() {
|
||||
id.remember(COOKIE_LOGIN.to_string())
|
||||
}
|
||||
web::Json(identity)
|
||||
}
|
||||
web::Json(identity)
|
||||
})),
|
||||
)
|
||||
.await
|
||||
|
@ -371,8 +371,10 @@ mod tests {
|
||||
App::new()
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.service(web::resource("/").to(|ses: Session| {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
async move {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
}
|
||||
})),
|
||||
)
|
||||
.await;
|
||||
@ -394,8 +396,10 @@ mod tests {
|
||||
App::new()
|
||||
.wrap(CookieSession::private(&[0; 32]).secure(false))
|
||||
.service(web::resource("/").to(|ses: Session| {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
async move {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
}
|
||||
})),
|
||||
)
|
||||
.await;
|
||||
@ -417,8 +421,10 @@ mod tests {
|
||||
App::new()
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.service(web::resource("/").to(|ses: Session| {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
async move {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
}
|
||||
})),
|
||||
)
|
||||
.await;
|
||||
@ -448,12 +454,16 @@ mod tests {
|
||||
.max_age(100),
|
||||
)
|
||||
.service(web::resource("/").to(|ses: Session| {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
async move {
|
||||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
}
|
||||
}))
|
||||
.service(web::resource("/test/").to(|ses: Session| {
|
||||
let val: usize = ses.get("counter").unwrap().unwrap();
|
||||
format!("counter: {}", val)
|
||||
async move {
|
||||
let val: usize = ses.get("counter").unwrap().unwrap();
|
||||
format!("counter: {}", val)
|
||||
}
|
||||
})),
|
||||
)
|
||||
.await;
|
||||
|
@ -5,42 +5,42 @@ use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, t
|
||||
use futures::{future, Future};
|
||||
|
||||
#[get("/test")]
|
||||
fn test() -> impl Responder {
|
||||
async fn test() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
#[put("/test")]
|
||||
fn put_test() -> impl Responder {
|
||||
async fn put_test() -> impl Responder {
|
||||
HttpResponse::Created()
|
||||
}
|
||||
|
||||
#[patch("/test")]
|
||||
fn patch_test() -> impl Responder {
|
||||
async fn patch_test() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
#[post("/test")]
|
||||
fn post_test() -> impl Responder {
|
||||
async fn post_test() -> impl Responder {
|
||||
HttpResponse::NoContent()
|
||||
}
|
||||
|
||||
#[head("/test")]
|
||||
fn head_test() -> impl Responder {
|
||||
async fn head_test() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
#[connect("/test")]
|
||||
fn connect_test() -> impl Responder {
|
||||
async fn connect_test() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
#[options("/test")]
|
||||
fn options_test() -> impl Responder {
|
||||
async fn options_test() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
#[trace("/test")]
|
||||
fn trace_test() -> impl Responder {
|
||||
async fn trace_test() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
@ -55,17 +55,17 @@ fn auto_sync() -> impl Future<Output = Result<HttpResponse, actix_web::Error>> {
|
||||
}
|
||||
|
||||
#[put("/test/{param}")]
|
||||
fn put_param_test(_: Path<String>) -> impl Responder {
|
||||
async fn put_param_test(_: Path<String>) -> impl Responder {
|
||||
HttpResponse::Created()
|
||||
}
|
||||
|
||||
#[delete("/test/{param}")]
|
||||
fn delete_param_test(_: Path<String>) -> impl Responder {
|
||||
async fn delete_param_test(_: Path<String>) -> impl Responder {
|
||||
HttpResponse::NoContent()
|
||||
}
|
||||
|
||||
#[get("/test/{param}")]
|
||||
fn get_param_test(_: Path<String>) -> impl Responder {
|
||||
async fn get_param_test(_: Path<String>) -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
|
@ -115,14 +115,14 @@ fn test_form() {
|
||||
fn test_timeout() {
|
||||
block_on(async {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::new(App::new().service(web::resource("/").route(
|
||||
web::to_async(|| {
|
||||
HttpService::new(App::new().service(web::resource("/").route(web::to(
|
||||
|| {
|
||||
async {
|
||||
tokio_timer::delay_for(Duration::from_millis(200)).await;
|
||||
Ok::<_, Error>(HttpResponse::Ok().body(STR))
|
||||
}
|
||||
}),
|
||||
)))
|
||||
},
|
||||
))))
|
||||
});
|
||||
|
||||
let connector = awc::Connector::new()
|
||||
@ -149,14 +149,14 @@ fn test_timeout() {
|
||||
fn test_timeout_override() {
|
||||
block_on(async {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::new(App::new().service(web::resource("/").route(
|
||||
web::to_async(|| {
|
||||
HttpService::new(App::new().service(web::resource("/").route(web::to(
|
||||
|| {
|
||||
async {
|
||||
tokio_timer::delay_for(Duration::from_millis(200)).await;
|
||||
Ok::<_, Error>(HttpResponse::Ok().body(STR))
|
||||
}
|
||||
}),
|
||||
)))
|
||||
},
|
||||
))))
|
||||
});
|
||||
|
||||
let client = awc::Client::build()
|
||||
@ -693,12 +693,9 @@ fn test_client_brotli_encoding() {
|
||||
|
||||
#[test]
|
||||
fn test_client_cookie_handling() {
|
||||
use std::io::{Error as IoError, ErrorKind};
|
||||
|
||||
block_on(async {
|
||||
fn err() -> Error {
|
||||
use std::io::{Error as IoError, ErrorKind};
|
||||
// stub some generic error
|
||||
Error::from(IoError::from(ErrorKind::NotFound))
|
||||
}
|
||||
let cookie1 = Cookie::build("cookie1", "value1").finish();
|
||||
let cookie2 = Cookie::build("cookie2", "value2")
|
||||
.domain("www.example.org")
|
||||
@ -717,31 +714,45 @@ fn test_client_cookie_handling() {
|
||||
HttpService::new(App::new().route(
|
||||
"/",
|
||||
web::to(move |req: HttpRequest| {
|
||||
// Check cookies were sent correctly
|
||||
req.cookie("cookie1")
|
||||
.ok_or_else(err)
|
||||
.and_then(|c1| {
|
||||
if c1.value() == "value1" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(err())
|
||||
}
|
||||
})
|
||||
.and_then(|()| req.cookie("cookie2").ok_or_else(err))
|
||||
.and_then(|c2| {
|
||||
if c2.value() == "value2" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(err())
|
||||
}
|
||||
})
|
||||
// Send some cookies back
|
||||
.map(|_| {
|
||||
HttpResponse::Ok()
|
||||
.cookie(cookie1.clone())
|
||||
.cookie(cookie2.clone())
|
||||
.finish()
|
||||
})
|
||||
let cookie1 = cookie1.clone();
|
||||
let cookie2 = cookie2.clone();
|
||||
|
||||
async move {
|
||||
// Check cookies were sent correctly
|
||||
let res: Result<(), Error> = req
|
||||
.cookie("cookie1")
|
||||
.ok_or(())
|
||||
.and_then(|c1| {
|
||||
if c1.value() == "value1" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
})
|
||||
.and_then(|()| req.cookie("cookie2").ok_or(()))
|
||||
.and_then(|c2| {
|
||||
if c2.value() == "value2" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
})
|
||||
.map_err(|_| {
|
||||
Error::from(IoError::from(ErrorKind::NotFound))
|
||||
});
|
||||
|
||||
if let Err(e) = res {
|
||||
Err(e)
|
||||
} else {
|
||||
// Send some cookies back
|
||||
Ok::<_, Error>(
|
||||
HttpResponse::Ok()
|
||||
.cookie(cookie1)
|
||||
.cookie(cookie2)
|
||||
.finish(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}),
|
||||
))
|
||||
});
|
||||
|
@ -35,7 +35,7 @@ pub use actix_testing::*;
|
||||
/// let mut srv = TestServer::start(
|
||||
/// || HttpService::new(
|
||||
/// App::new().service(
|
||||
/// web::resource("/").to_async(my_handler))
|
||||
/// web::resource("/").to(my_handler))
|
||||
/// )
|
||||
/// );
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user