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

84 lines
2.8 KiB
Rust
Raw Normal View History

2018-11-19 06:48:20 +01:00
use std::io;
2018-10-05 23:30:40 +02:00
2018-12-11 03:08:33 +01:00
use actix_codec::Framed;
2019-05-12 17:34:51 +02:00
use actix_http::{body::BodySize, h1, ws, Error, HttpService, Request, Response};
2019-11-20 19:35:07 +01:00
use actix_http_test::{block_on, TestServer};
use bytes::{Bytes, BytesMut};
2019-05-12 17:34:51 +02:00
use futures::future::ok;
2019-11-20 19:35:07 +01:00
use futures::{SinkExt, StreamExt};
2018-10-05 23:30:40 +02:00
2019-11-20 19:35:07 +01:00
async fn ws_service(req: ws::Frame) -> Result<ws::Message, io::Error> {
2018-10-05 23:30:40 +02:00
match req {
2019-11-20 19:35:07 +01:00
ws::Frame::Ping(msg) => Ok(ws::Message::Pong(msg)),
2018-10-10 22:20:00 +02:00
ws::Frame::Text(text) => {
let text = if let Some(pl) = text {
String::from_utf8(Vec::from(pl.as_ref())).unwrap()
} else {
String::new()
};
2019-11-20 19:35:07 +01:00
Ok(ws::Message::Text(text))
2018-10-10 22:20:00 +02:00
}
2019-11-20 19:35:07 +01:00
ws::Frame::Binary(bin) => Ok(ws::Message::Binary(
2018-10-10 22:20:00 +02:00
bin.map(|e| e.freeze())
.unwrap_or_else(|| Bytes::from(""))
.into(),
)),
2019-11-20 19:35:07 +01:00
ws::Frame::Close(reason) => Ok(ws::Message::Close(reason)),
_ => Ok(ws::Message::Close(None)),
2018-10-05 23:30:40 +02:00
}
}
#[test]
fn test_simple() {
2019-11-20 19:35:07 +01:00
block_on(async {
let mut srv = TestServer::start(|| {
HttpService::build()
.upgrade(|(req, mut framed): (Request, Framed<_, _>)| {
async move {
let res = ws::handshake_response(req.head()).finish();
// send handshake response
framed
.send(h1::Message::Item((res.drop_body(), BodySize::None)))
.await?;
2019-05-12 17:34:51 +02:00
// start websocket service
let framed = framed.into_framed(ws::Codec::new());
2019-11-20 19:35:07 +01:00
ws::Transport::with(framed, ws_service).await
}
})
.finish(|_| ok::<_, Error>(Response::NotFound()))
});
2018-10-05 23:30:40 +02:00
2019-11-20 19:35:07 +01:00
// client service
let mut framed = srv.ws().await.unwrap();
framed
.send(ws::Message::Text("text".to_string()))
.await
.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Text(Some(BytesMut::from("text"))));
2019-11-20 19:35:07 +01:00
framed
.send(ws::Message::Binary("text".into()))
.await
.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(
item,
ws::Frame::Binary(Some(Bytes::from_static(b"text").into()))
);
2019-11-20 19:35:07 +01:00
framed.send(ws::Message::Ping("text".into())).await.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Pong("text".to_string().into()));
2019-11-20 19:35:07 +01:00
framed
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
.await
.unwrap();
2019-11-20 19:35:07 +01:00
let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));
})
2018-10-05 23:30:40 +02:00
}