1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00
actix-web/actix-framed/tests/test_server.rs

162 lines
4.6 KiB
Rust
Raw Normal View History

2019-04-11 00:06:27 +02:00
use actix_codec::{AsyncRead, AsyncWrite};
2019-04-19 06:28:23 +02:00
use actix_http::{body, http::StatusCode, ws, Error, HttpService, Response};
2019-12-12 18:08:38 +01:00
use actix_http_test::test_server;
2019-11-21 07:17:01 +01:00
use actix_service::{pipeline_factory, IntoServiceFactory, ServiceFactory};
2019-12-11 14:20:20 +01:00
use actix_utils::framed::Dispatcher;
use bytes::Bytes;
use futures_util::{future, SinkExt, StreamExt};
2019-04-11 00:06:27 +02:00
2019-04-12 20:15:58 +02:00
use actix_framed::{FramedApp, FramedRequest, FramedRoute, SendError, VerifyWebSockets};
2019-04-11 00:06:27 +02:00
2019-11-21 07:17:01 +01:00
async fn ws_service<T: AsyncRead + AsyncWrite>(
2019-04-11 00:06:27 +02:00
req: FramedRequest<T>,
2019-11-21 07:17:01 +01:00
) -> Result<(), Error> {
let (req, mut framed, _) = req.into_parts();
let res = ws::handshake(req.head()).unwrap().message_body(());
2019-04-11 00:06:27 +02:00
framed
.send((res, body::BodySize::None).into())
2019-11-21 07:17:01 +01:00
.await
.unwrap();
2019-12-11 14:20:20 +01:00
Dispatcher::new(framed.into_framed(ws::Codec::new()), service)
2019-11-21 07:17:01 +01:00
.await
.unwrap();
Ok(())
2019-04-11 00:06:27 +02:00
}
2019-11-21 07:17:01 +01:00
async fn service(msg: ws::Frame) -> Result<ws::Message, Error> {
2019-04-11 00:06:27 +02: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-11 00:06:27 +02:00
}
ws::Frame::Binary(bin) => ws::Message::Binary(bin),
2019-04-11 00:06:27 +02:00
ws::Frame::Close(reason) => ws::Message::Close(reason),
_ => panic!(),
};
2019-11-21 07:17:01 +01:00
Ok(msg)
2019-04-11 00:06:27 +02:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_simple() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(|| {
2019-11-26 06:25:50 +01:00
HttpService::build()
.upgrade(
FramedApp::new().service(FramedRoute::get("/index.html").to(ws_service)),
)
.finish(|_| future::ok::<_, Error>(Response::NotFound()))
2019-12-02 12:33:11 +01:00
.tcp()
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01: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 06:25:50 +01: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 06:25:50 +01: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 20:15:58 +02:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_service() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(|| {
2019-11-26 06:25:50 +01: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 07:17:01 +01:00
)
2019-11-26 06:25:50 +01:00
.and_then(
FramedApp::new()
.service(FramedRoute::get("/index.html").to(ws_service))
.into_factory()
.map_err(|_| ()),
),
)
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
// 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 06:25:50 +01: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 06:25:50 +01: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-11 00:06:27 +02:00
}