1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-26 15:07:42 +02:00

fix state factory support, tests for state and state factory

This commit is contained in:
Nikolay Kim
2019-03-03 21:40:03 -08:00
parent 34171fa7f5
commit 5c61321565
3 changed files with 208 additions and 6 deletions

View File

@ -272,3 +272,40 @@ where
Ok(self.0.poll().map_err(|e| e.into())?)
}
}
#[cfg(test)]
mod tests {
// use actix_http::body::Body;
use actix_http::body::{Body, ResponseBody};
use actix_http::http::StatusCode;
use actix_service::{IntoNewService, NewService, Service};
use bytes::Bytes;
use crate::test::TestRequest;
use crate::App;
#[test]
fn test_option_responder() {
let mut rt = actix_rt::Runtime::new().unwrap();
let app = App::new()
.resource("/none", |r| r.to(|| -> Option<&'static str> { None }))
.resource("/some", |r| r.to(|| Some("some")))
.into_new_service();
let mut srv = rt.block_on(app.new_service(&())).unwrap();
let req = TestRequest::with_uri("/none").to_request();
let resp = rt.block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/some").to_request();
let resp = rt.block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::OK);
match resp.body() {
ResponseBody::Body(Body::Bytes(ref b)) => {
let bytes: Bytes = b.clone().into();
assert_eq!(bytes, Bytes::from_static(b"some"));
}
_ => panic!(),
}
}
}