2018-11-19 05:48:20 +00:00
|
|
|
use std::io;
|
2018-10-05 21:30:40 +00:00
|
|
|
|
2018-12-11 02:08:33 +00:00
|
|
|
use actix_codec::Framed;
|
2019-01-27 18:59:07 +00:00
|
|
|
use actix_http_test::TestServer;
|
2019-03-11 22:09:42 +00:00
|
|
|
use actix_server::Io;
|
|
|
|
use actix_service::{fn_service, NewService};
|
2018-12-11 02:08:33 +00:00
|
|
|
use actix_utils::framed::IntoFramed;
|
|
|
|
use actix_utils::stream::TakeItem;
|
2018-10-23 01:18:05 +00:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2019-02-07 21:24:24 +00:00
|
|
|
use futures::future::{ok, Either};
|
2018-11-19 05:48:20 +00:00
|
|
|
use futures::{Future, Sink, Stream};
|
2018-10-05 21:30:40 +00:00
|
|
|
|
2019-01-27 18:59:07 +00:00
|
|
|
use actix_http::{h1, ws, ResponseError, SendResponse, ServiceConfig};
|
2018-10-05 21:30:40 +00:00
|
|
|
|
2018-10-10 20:20:00 +00:00
|
|
|
fn ws_service(req: ws::Frame) -> impl Future<Item = ws::Message, Error = io::Error> {
|
2018-10-05 21:30:40 +00:00
|
|
|
match req {
|
2018-10-10 20:20:00 +00:00
|
|
|
ws::Frame::Ping(msg) => ok(ws::Message::Pong(msg)),
|
|
|
|
ws::Frame::Text(text) => {
|
|
|
|
let text = if let Some(pl) = text {
|
|
|
|
String::from_utf8(Vec::from(pl.as_ref())).unwrap()
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
ok(ws::Message::Text(text))
|
|
|
|
}
|
|
|
|
ws::Frame::Binary(bin) => ok(ws::Message::Binary(
|
|
|
|
bin.map(|e| e.freeze())
|
|
|
|
.unwrap_or_else(|| Bytes::from(""))
|
|
|
|
.into(),
|
|
|
|
)),
|
|
|
|
ws::Frame::Close(reason) => ok(ws::Message::Close(reason)),
|
2018-10-05 21:30:40 +00:00
|
|
|
_ => ok(ws::Message::Close(None)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
2019-02-19 02:46:30 +00:00
|
|
|
let mut srv = TestServer::new(|| {
|
2019-03-11 22:09:42 +00:00
|
|
|
fn_service(|io: Io<_>| Ok(io.into_parts().0))
|
|
|
|
.and_then(IntoFramed::new(|| h1::Codec::new(ServiceConfig::default())))
|
2018-11-19 05:48:20 +00:00
|
|
|
.and_then(TakeItem::new().map_err(|_| ()))
|
|
|
|
.and_then(|(req, framed): (_, Framed<_, _>)| {
|
|
|
|
// validate request
|
|
|
|
if let Some(h1::Message::Item(req)) = req {
|
|
|
|
match ws::verify_handshake(&req) {
|
|
|
|
Err(e) => {
|
|
|
|
// validation failed
|
|
|
|
Either::A(
|
|
|
|
SendResponse::send(framed, e.error_response())
|
|
|
|
.map_err(|_| ())
|
|
|
|
.map(|_| ()),
|
|
|
|
)
|
2018-10-05 22:39:35 +00:00
|
|
|
}
|
2018-11-19 05:48:20 +00:00
|
|
|
Ok(_) => {
|
|
|
|
Either::B(
|
|
|
|
// send handshake response
|
|
|
|
SendResponse::send(
|
|
|
|
framed,
|
|
|
|
ws::handshake_response(&req).finish(),
|
2018-12-06 22:32:52 +00:00
|
|
|
)
|
|
|
|
.map_err(|_| ())
|
2018-11-19 05:48:20 +00:00
|
|
|
.and_then(|framed| {
|
|
|
|
// start websocket service
|
|
|
|
let framed = framed.into_framed(ws::Codec::new());
|
|
|
|
ws::Transport::with(framed, ws_service)
|
|
|
|
.map_err(|_| ())
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
})
|
2018-10-05 21:30:40 +00:00
|
|
|
});
|
|
|
|
|
2018-10-23 01:18:05 +00:00
|
|
|
// client service
|
2018-11-19 05:48:20 +00:00
|
|
|
let framed = srv.ws().unwrap();
|
|
|
|
let framed = srv
|
2018-10-23 01:18:05 +00:00
|
|
|
.block_on(framed.send(ws::Message::Text("text".to_string())))
|
|
|
|
.unwrap();
|
2018-11-19 05:48:20 +00:00
|
|
|
let (item, framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap();
|
2018-10-23 01:18:05 +00:00
|
|
|
assert_eq!(item, Some(ws::Frame::Text(Some(BytesMut::from("text")))));
|
|
|
|
|
2018-11-19 05:48:20 +00:00
|
|
|
let framed = srv
|
2018-10-23 01:18:05 +00:00
|
|
|
.block_on(framed.send(ws::Message::Binary("text".into())))
|
|
|
|
.unwrap();
|
2018-11-19 05:48:20 +00:00
|
|
|
let (item, framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap();
|
2018-10-23 01:18:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
item,
|
|
|
|
Some(ws::Frame::Binary(Some(Bytes::from_static(b"text").into())))
|
|
|
|
);
|
|
|
|
|
2018-11-19 05:48:20 +00:00
|
|
|
let framed = srv
|
2018-10-23 01:18:05 +00:00
|
|
|
.block_on(framed.send(ws::Message::Ping("text".into())))
|
|
|
|
.unwrap();
|
2018-11-19 05:48:20 +00:00
|
|
|
let (item, framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap();
|
2018-10-23 01:18:05 +00:00
|
|
|
assert_eq!(item, Some(ws::Frame::Pong("text".to_string().into())));
|
|
|
|
|
2018-11-19 05:48:20 +00:00
|
|
|
let framed = srv
|
2018-10-23 01:18:05 +00:00
|
|
|
.block_on(framed.send(ws::Message::Close(Some(ws::CloseCode::Normal.into()))))
|
|
|
|
.unwrap();
|
|
|
|
|
2018-11-19 05:48:20 +00:00
|
|
|
let (item, _framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap();
|
2018-10-23 01:18:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
item,
|
|
|
|
Some(ws::Frame::Close(Some(ws::CloseCode::Normal.into())))
|
2018-12-11 02:08:33 +00:00
|
|
|
);
|
2018-10-05 21:30:40 +00:00
|
|
|
}
|