1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-18 14:16:47 +00:00
actix-web/actix-framed/tests/test_server.rs

160 lines
4.6 KiB
Rust
Raw Normal View History

2019-04-10 22:06:27 +00:00
use actix_codec::{AsyncRead, AsyncWrite};
2019-04-19 04:28:23 +00:00
use actix_http::{body, http::StatusCode, ws, Error, HttpService, Response};
2019-12-12 17:08:38 +00:00
use actix_http_test::test_server;
2019-11-21 06:17:01 +00:00
use actix_service::{pipeline_factory, IntoServiceFactory, ServiceFactory};
2019-12-11 13:20:20 +00:00
use actix_utils::framed::Dispatcher;
use bytes::Bytes;
2019-11-21 06:17:01 +00:00
use futures::{future, SinkExt, StreamExt};
2019-04-10 22:06:27 +00:00
2019-04-12 18:15:58 +00:00
use actix_framed::{FramedApp, FramedRequest, FramedRoute, SendError, VerifyWebSockets};
2019-04-10 22:06:27 +00:00
2019-11-21 06:17:01 +00:00
async fn ws_service<T: AsyncRead + AsyncWrite>(
2019-04-10 22:06:27 +00:00
req: FramedRequest<T>,
2019-11-21 06:17:01 +00:00
) -> Result<(), Error> {
let (req, mut framed, _) = req.into_parts();
let res = ws::handshake(req.head()).unwrap().message_body(());
2019-04-10 22:06:27 +00:00
framed
.send((res, body::BodySize::None).into())
2019-11-21 06:17:01 +00:00
.await
.unwrap();
2019-12-11 13:20:20 +00:00
Dispatcher::new(framed.into_framed(ws::Codec::new()), service)
2019-11-21 06:17:01 +00:00
.await
.unwrap();
Ok(())
2019-04-10 22:06:27 +00:00
}
2019-11-21 06:17:01 +00:00
async fn service(msg: ws::Frame) -> Result<ws::Message, Error> {
2019-04-10 22:06:27 +00:00
let msg = match msg {
ws::Frame::Ping(msg) => ws::Message::Pong(msg),
ws::Frame::Text(text) => {
ws::Message::Text(String::from_utf8_lossy(&text).to_string())
2019-04-10 22:06:27 +00:00
}
ws::Frame::Binary(bin) => ws::Message::Binary(bin),
2019-04-10 22:06:27 +00:00
ws::Frame::Close(reason) => ws::Message::Close(reason),
_ => panic!(),
};
2019-11-21 06:17:01 +00:00
Ok(msg)
2019-04-10 22:06:27 +00:00
}
2019-11-26 05:25:50 +00:00
#[actix_rt::test]
async fn test_simple() {
2019-12-12 17:08:38 +00:00
let mut srv = test_server(|| {
2019-11-26 05:25:50 +00:00
HttpService::build()
.upgrade(
FramedApp::new().service(FramedRoute::get("/index.html").to(ws_service)),
)
.finish(|_| future::ok::<_, Error>(Response::NotFound()))
2019-12-02 11:33:11 +00:00
.tcp()
2019-11-26 05:25:50 +00:00
});
assert!(srv.ws_at("/test").await.is_err());
// client service
let mut framed = srv.ws_at("/index.html").await.unwrap();
framed
.send(ws::Message::Text("text".to_string()))
.await
.unwrap();
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Text(Bytes::from_static(b"text"))
2019-11-26 05:25:50 +00:00
);
framed
.send(ws::Message::Binary("text".into()))
.await
.unwrap();
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Binary(Bytes::from_static(b"text"))
2019-11-26 05:25:50 +00:00
);
framed.send(ws::Message::Ping("text".into())).await.unwrap();
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Pong("text".to_string().into())
);
framed
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
.await
.unwrap();
let (item, _) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Close(Some(ws::CloseCode::Normal.into()))
);
2019-04-12 18:15:58 +00:00
}
2019-11-26 05:25:50 +00:00
#[actix_rt::test]
async fn test_service() {
2019-12-12 17:08:38 +00:00
let mut srv = test_server(|| {
2019-11-26 05:25:50 +00:00
pipeline_factory(actix_http::h1::OneRequest::new().map_err(|_| ())).and_then(
pipeline_factory(
pipeline_factory(VerifyWebSockets::default())
.then(SendError::default())
.map_err(|_| ()),
2019-11-21 06:17:01 +00:00
)
2019-11-26 05:25:50 +00:00
.and_then(
FramedApp::new()
.service(FramedRoute::get("/index.html").to(ws_service))
.into_factory()
.map_err(|_| ()),
),
)
});
// non ws request
let res = srv.get("/index.html").send().await.unwrap();
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
// not found
assert!(srv.ws_at("/test").await.is_err());
// client service
let mut framed = srv.ws_at("/index.html").await.unwrap();
framed
.send(ws::Message::Text("text".to_string()))
.await
.unwrap();
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Text(Bytes::from_static(b"text"))
2019-11-26 05:25:50 +00:00
);
framed
.send(ws::Message::Binary("text".into()))
.await
.unwrap();
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Binary(Bytes::from_static(b"text"))
2019-11-26 05:25:50 +00:00
);
framed.send(ws::Message::Ping("text".into())).await.unwrap();
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Pong("text".to_string().into())
);
framed
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
.await
.unwrap();
let (item, _) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Close(Some(ws::CloseCode::Normal.into()))
);
2019-04-10 22:06:27 +00:00
}