mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
Allow to test an app that uses async actors #897
This commit is contained in:
parent
ff724e239d
commit
2ffda29f9b
@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
* Move identity middleware to `actix-identity` crate.
|
* Move identity middleware to `actix-identity` crate.
|
||||||
|
|
||||||
|
* Allow to test an app that uses async actors #897
|
||||||
|
|
||||||
|
|
||||||
## [1.0.0] - 2019-06-05
|
## [1.0.0] - 2019-06-05
|
||||||
|
|
||||||
|
@ -103,7 +103,8 @@ rustls = { version = "0.15", optional = true }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-http = { version = "0.2.3", features=["ssl", "brotli", "flate2-zlib"] }
|
actix-http = { version = "0.2.3", features=["ssl", "brotli", "flate2-zlib"] }
|
||||||
actix-http-test = { version = "0.2.0", features=["ssl"] }
|
actix-http-test = { version = "0.2.0", features=["ssl"] }
|
||||||
actix-files = { version = "0.1.1" }
|
actix-files = "0.1.1"
|
||||||
|
actix = { version = "0.8.3" }
|
||||||
rand = "0.6"
|
rand = "0.6"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## 1.0
|
## 1.0.0
|
||||||
|
|
||||||
* Resource registration. 1.0 version uses generalized resource
|
* Resource registration. 1.0 version uses generalized resource
|
||||||
registration via `.service()` method.
|
registration via `.service()` method.
|
||||||
|
46
src/test.rs
46
src/test.rs
@ -7,7 +7,7 @@ use actix_http::http::{HttpTryFrom, Method, StatusCode, Uri, Version};
|
|||||||
use actix_http::test::TestRequest as HttpTestRequest;
|
use actix_http::test::TestRequest as HttpTestRequest;
|
||||||
use actix_http::{cookie::Cookie, Extensions, Request};
|
use actix_http::{cookie::Cookie, Extensions, Request};
|
||||||
use actix_router::{Path, ResourceDef, Url};
|
use actix_router::{Path, ResourceDef, Url};
|
||||||
use actix_rt::Runtime;
|
use actix_rt::{System, SystemRunner};
|
||||||
use actix_server_config::ServerConfig;
|
use actix_server_config::ServerConfig;
|
||||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
@ -29,14 +29,14 @@ use crate::{Error, HttpRequest, HttpResponse};
|
|||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static RT: RefCell<Inner> = {
|
static RT: RefCell<Inner> = {
|
||||||
RefCell::new(Inner(Some(Runtime::new().unwrap())))
|
RefCell::new(Inner(Some(System::builder().build())))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Inner(Option<Runtime>);
|
struct Inner(Option<SystemRunner>);
|
||||||
|
|
||||||
impl Inner {
|
impl Inner {
|
||||||
fn get_mut(&mut self) -> &mut Runtime {
|
fn get_mut(&mut self) -> &mut SystemRunner {
|
||||||
self.0.as_mut().unwrap()
|
self.0.as_mut().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -714,4 +714,42 @@ mod tests {
|
|||||||
let res = block_fn(|| app.call(req)).unwrap();
|
let res = block_fn(|| app.call(req)).unwrap();
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_actor() {
|
||||||
|
use actix::Actor;
|
||||||
|
|
||||||
|
struct MyActor;
|
||||||
|
|
||||||
|
struct Num(usize);
|
||||||
|
impl actix::Message for Num {
|
||||||
|
type Result = usize;
|
||||||
|
}
|
||||||
|
impl actix::Actor for MyActor {
|
||||||
|
type Context = actix::Context<Self>;
|
||||||
|
}
|
||||||
|
impl actix::Handler<Num> for MyActor {
|
||||||
|
type Result = usize;
|
||||||
|
fn handle(&mut self, msg: Num, _: &mut Self::Context) -> Self::Result {
|
||||||
|
msg.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let addr = run_on(|| MyActor.start());
|
||||||
|
let mut app = init_service(App::new().service(
|
||||||
|
web::resource("/index.html").to_async(move || {
|
||||||
|
addr.send(Num(1)).from_err().and_then(|res| {
|
||||||
|
if res == 1 {
|
||||||
|
HttpResponse::Ok()
|
||||||
|
} else {
|
||||||
|
HttpResponse::BadRequest()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
|
||||||
|
let req = TestRequest::post().uri("/index.html").to_request();
|
||||||
|
let res = block_fn(|| app.call(req)).unwrap();
|
||||||
|
assert!(res.status().is_success());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user