2019-03-18 05:02:03 +00:00
|
|
|
use actix::prelude::*;
|
2021-12-04 22:32:44 +00:00
|
|
|
use actix_http::ws::Codec;
|
|
|
|
use actix_web::{web, App, HttpRequest};
|
|
|
|
use actix_web_actors::ws;
|
2019-12-15 16:45:38 +00:00
|
|
|
use bytes::Bytes;
|
2022-12-18 01:34:48 +00:00
|
|
|
use futures_util::{SinkExt as _, StreamExt as _};
|
2019-03-18 05:02:03 +00:00
|
|
|
|
|
|
|
struct Ws;
|
|
|
|
|
|
|
|
impl Actor for Ws {
|
2019-03-18 05:11:50 +00:00
|
|
|
type Context = ws::WebsocketContext<Self>;
|
2019-03-18 05:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Ws {
|
2021-02-11 23:03:17 +00:00
|
|
|
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
2021-12-04 22:32:44 +00:00
|
|
|
match msg {
|
|
|
|
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
|
|
|
|
Ok(ws::Message::Text(text)) => ctx.text(text),
|
|
|
|
Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
|
|
|
|
Ok(ws::Message::Close(reason)) => ctx.close(reason),
|
|
|
|
_ => ctx.close(Some(ws::CloseCode::Error.into())),
|
2019-03-18 05:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
const MAX_FRAME_SIZE: usize = 10_000;
|
|
|
|
const DEFAULT_FRAME_SIZE: usize = 10;
|
2019-03-18 05:02:03 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
async fn common_test_code(mut srv: actix_test::TestServer, frame_size: usize) {
|
2019-03-18 05:02:03 +00:00
|
|
|
// client service
|
2019-12-15 16:45:38 +00:00
|
|
|
let mut framed = srv.ws().await.unwrap();
|
2019-03-18 05:02:03 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
framed.send(ws::Message::Text("text".into())).await.unwrap();
|
2019-12-15 16:45:38 +00:00
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Text(Bytes::from_static(b"text")));
|
2019-03-18 05:02:03 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
let bytes = Bytes::from(vec![0; frame_size]);
|
2019-12-15 16:45:38 +00:00
|
|
|
framed
|
2021-12-04 22:32:44 +00:00
|
|
|
.send(ws::Message::Binary(bytes.clone()))
|
2019-12-15 16:45:38 +00:00
|
|
|
.await
|
2019-03-18 05:02:03 +00:00
|
|
|
.unwrap();
|
2019-12-15 16:45:38 +00:00
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
2021-12-04 22:32:44 +00:00
|
|
|
assert_eq!(item, ws::Frame::Binary(bytes));
|
2019-12-15 16:45:38 +00:00
|
|
|
|
|
|
|
framed.send(ws::Message::Ping("text".into())).await.unwrap();
|
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Pong(Bytes::copy_from_slice(b"text")));
|
2019-03-18 05:02:03 +00:00
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
framed
|
|
|
|
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
|
|
|
.await
|
2019-03-18 05:02:03 +00:00
|
|
|
.unwrap();
|
2019-12-15 16:45:38 +00:00
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));
|
2019-03-18 05:02:03 +00:00
|
|
|
}
|
2021-04-01 05:40:10 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
2021-12-04 22:32:44 +00:00
|
|
|
async fn simple_builder() {
|
|
|
|
let srv = actix_test::start(|| {
|
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
|
|
|
ws::WsResponseBuilder::new(Ws, &req, stream).start()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
common_test_code(srv, DEFAULT_FRAME_SIZE).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn builder_with_frame_size() {
|
|
|
|
let srv = actix_test::start(|| {
|
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
|
|
|
ws::WsResponseBuilder::new(Ws, &req, stream)
|
|
|
|
.frame_size(MAX_FRAME_SIZE)
|
|
|
|
.start()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
common_test_code(srv, MAX_FRAME_SIZE).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn builder_with_frame_size_exceeded() {
|
|
|
|
const MAX_FRAME_SIZE: usize = 64;
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let mut srv = actix_test::start(|| {
|
2021-04-01 05:40:10 +00:00
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
2021-12-04 22:32:44 +00:00
|
|
|
ws::WsResponseBuilder::new(Ws, &req, stream)
|
|
|
|
.frame_size(MAX_FRAME_SIZE)
|
|
|
|
.start()
|
2021-04-01 05:40:10 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
// client service
|
|
|
|
let mut framed = srv.ws().await.unwrap();
|
2021-04-01 05:40:10 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
// create a request with a frame size larger than expected
|
|
|
|
let bytes = Bytes::from(vec![0; MAX_FRAME_SIZE + 1]);
|
|
|
|
framed.send(ws::Message::Binary(bytes)).await.unwrap();
|
2021-04-01 05:40:10 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
let frame = framed.next().await.unwrap().unwrap();
|
|
|
|
let close_reason = match frame {
|
|
|
|
ws::Frame::Close(Some(reason)) => reason,
|
|
|
|
_ => panic!("close frame expected"),
|
|
|
|
};
|
|
|
|
assert_eq!(close_reason.code, ws::CloseCode::Error);
|
|
|
|
}
|
2021-04-01 05:40:10 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn builder_with_codec() {
|
|
|
|
let srv = actix_test::start(|| {
|
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
|
|
|
ws::WsResponseBuilder::new(Ws, &req, stream)
|
|
|
|
.codec(Codec::new())
|
|
|
|
.start()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2021-04-01 05:40:10 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
common_test_code(srv, DEFAULT_FRAME_SIZE).await;
|
|
|
|
}
|
2021-04-01 05:40:10 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn builder_with_protocols() {
|
|
|
|
let srv = actix_test::start(|| {
|
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
|
|
|
ws::WsResponseBuilder::new(Ws, &req, stream)
|
|
|
|
.protocols(&["A", "B"])
|
|
|
|
.start()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2021-04-01 05:40:10 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
common_test_code(srv, DEFAULT_FRAME_SIZE).await;
|
|
|
|
}
|
2021-04-01 05:40:10 +00:00
|
|
|
|
2021-12-04 22:32:44 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn builder_with_codec_and_frame_size() {
|
|
|
|
let srv = actix_test::start(|| {
|
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
|
|
|
ws::WsResponseBuilder::new(Ws, &req, stream)
|
|
|
|
.codec(Codec::new())
|
|
|
|
.frame_size(MAX_FRAME_SIZE)
|
|
|
|
.start()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
common_test_code(srv, DEFAULT_FRAME_SIZE).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn builder_full() {
|
|
|
|
let srv = actix_test::start(|| {
|
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
|
|
|
ws::WsResponseBuilder::new(Ws, &req, stream)
|
|
|
|
.frame_size(MAX_FRAME_SIZE)
|
|
|
|
.codec(Codec::new())
|
|
|
|
.protocols(&["A", "B"])
|
|
|
|
.start()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
common_test_code(srv, MAX_FRAME_SIZE).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn simple_start() {
|
|
|
|
let srv = actix_test::start(|| {
|
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move { ws::start(Ws, &req, stream) },
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
common_test_code(srv, DEFAULT_FRAME_SIZE).await;
|
2021-04-01 05:40:10 +00:00
|
|
|
}
|