1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-12-18 09:43:11 +01:00
actix-extras/actix-framed/tests/test_server.rs

164 lines
5.1 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-11-21 07:17:01 +01:00
use actix_http_test::{block_on, TestServer};
use actix_service::{pipeline_factory, IntoServiceFactory, ServiceFactory};
2019-04-11 00:06:27 +02:00
use actix_utils::framed::FramedTransport;
use bytes::{Bytes, BytesMut};
2019-11-21 07:17:01 +01:00
use futures::{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();
FramedTransport::new(framed.into_framed(ws::Codec::new()), service)
.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.unwrap()).to_string())
}
ws::Frame::Binary(bin) => ws::Message::Binary(bin.unwrap().freeze()),
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
}
#[test]
fn test_simple() {
2019-11-21 07:17:01 +01:00
block_on(async {
let mut srv = TestServer::start(|| {
HttpService::build()
.upgrade(
FramedApp::new()
.service(FramedRoute::get("/index.html").to(ws_service)),
)
.finish(|_| future::ok::<_, Error>(Response::NotFound()))
});
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(Some(BytesMut::from("text")))
);
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(Some(Bytes::from_static(b"text").into()))
);
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
}
#[test]
fn test_service() {
2019-11-21 07:17:01 +01:00
block_on(async {
let mut srv = TestServer::start(|| {
pipeline_factory(actix_http::h1::OneRequest::new().map_err(|_| ())).and_then(
pipeline_factory(
pipeline_factory(VerifyWebSockets::default())
.then(SendError::default())
.map_err(|_| ()),
)
2019-04-12 20:15:58 +02:00
.and_then(
FramedApp::new()
.service(FramedRoute::get("/index.html").to(ws_service))
2019-11-21 07:17:01 +01:00
.into_factory()
2019-04-12 20:15:58 +02:00
.map_err(|_| ()),
),
2019-11-21 07:17:01 +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(Some(BytesMut::from("text")))
);
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(Some(Bytes::from_static(b"text").into()))
);
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
}