2018-01-31 00:13:33 +01:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate futures;
|
|
|
|
extern crate http;
|
|
|
|
extern crate bytes;
|
2018-03-19 17:30:58 +01:00
|
|
|
extern crate rand;
|
2018-01-31 00:13:33 +01:00
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::Stream;
|
2018-03-19 17:30:58 +01:00
|
|
|
use rand::Rng;
|
2018-01-31 00:13:33 +01:00
|
|
|
|
|
|
|
use actix_web::*;
|
|
|
|
use actix::prelude::*;
|
|
|
|
|
|
|
|
struct Ws;
|
|
|
|
|
|
|
|
impl Actor for Ws {
|
|
|
|
type Context = ws::WebsocketContext<Self>;
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:29:55 +01:00
|
|
|
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
2018-01-31 00:13:33 +01:00
|
|
|
|
|
|
|
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
|
|
|
|
match msg {
|
|
|
|
ws::Message::Ping(msg) => ctx.pong(&msg),
|
2018-02-10 07:26:48 +01:00
|
|
|
ws::Message::Text(text) => ctx.text(text),
|
2018-01-31 00:13:33 +01:00
|
|
|
ws::Message::Binary(bin) => ctx.binary(bin),
|
2018-02-26 22:58:23 +01:00
|
|
|
ws::Message::Close(reason) => ctx.close(reason, ""),
|
2018-01-31 00:13:33 +01:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let mut srv = test::TestServer::new(
|
|
|
|
|app| app.handler(|req| ws::start(req, Ws)));
|
|
|
|
let (reader, mut writer) = srv.ws().unwrap();
|
|
|
|
|
|
|
|
writer.text("text");
|
|
|
|
let (item, reader) = srv.execute(reader.into_future()).unwrap();
|
|
|
|
assert_eq!(item, Some(ws::Message::Text("text".to_owned())));
|
|
|
|
|
|
|
|
writer.binary(b"text".as_ref());
|
|
|
|
let (item, reader) = srv.execute(reader.into_future()).unwrap();
|
|
|
|
assert_eq!(item, Some(ws::Message::Binary(Bytes::from_static(b"text").into())));
|
|
|
|
|
|
|
|
writer.ping("ping");
|
2018-01-31 01:04:04 +01:00
|
|
|
let (item, reader) = srv.execute(reader.into_future()).unwrap();
|
2018-01-31 00:13:33 +01:00
|
|
|
assert_eq!(item, Some(ws::Message::Pong("ping".to_owned())));
|
2018-01-31 01:04:04 +01:00
|
|
|
|
|
|
|
writer.close(ws::CloseCode::Normal, "");
|
|
|
|
let (item, _) = srv.execute(reader.into_future()).unwrap();
|
2018-02-26 22:58:23 +01:00
|
|
|
assert_eq!(item, Some(ws::Message::Close(ws::CloseCode::Normal)));
|
2018-01-31 00:13:33 +01:00
|
|
|
}
|
2018-03-19 17:30:58 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_large_text() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.gen_ascii_chars()
|
|
|
|
.take(65_536)
|
|
|
|
.collect::<String>();
|
|
|
|
|
|
|
|
let mut srv = test::TestServer::new(
|
|
|
|
|app| app.handler(|req| ws::start(req, Ws)));
|
|
|
|
let (mut reader, mut writer) = srv.ws().unwrap();
|
|
|
|
|
|
|
|
for _ in 0..100 {
|
|
|
|
writer.text(data.clone());
|
|
|
|
let (item, r) = srv.execute(reader.into_future()).unwrap();
|
|
|
|
reader = r;
|
|
|
|
assert_eq!(item, Some(ws::Message::Text(data.clone())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_large_bin() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.gen_ascii_chars()
|
|
|
|
.take(65_536)
|
|
|
|
.collect::<String>();
|
|
|
|
|
|
|
|
let mut srv = test::TestServer::new(
|
|
|
|
|app| app.handler(|req| ws::start(req, Ws)));
|
|
|
|
let (mut reader, mut writer) = srv.ws().unwrap();
|
|
|
|
|
|
|
|
for _ in 0..100 {
|
|
|
|
writer.binary(data.clone());
|
|
|
|
let (item, r) = srv.execute(reader.into_future()).unwrap();
|
|
|
|
reader = r;
|
|
|
|
assert_eq!(item, Some(ws::Message::Binary(Binary::from(data.clone()))));
|
|
|
|
}
|
|
|
|
}
|