2018-10-05 23:30:40 +02:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_http;
|
|
|
|
extern crate actix_net;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate bytes;
|
|
|
|
extern crate futures;
|
|
|
|
|
|
|
|
use std::{io, thread};
|
|
|
|
|
|
|
|
use actix::System;
|
|
|
|
use actix_net::codec::Framed;
|
2018-10-06 00:39:35 +02:00
|
|
|
use actix_net::framed::IntoFramed;
|
2018-10-05 23:30:40 +02:00
|
|
|
use actix_net::server::Server;
|
2018-10-06 00:39:35 +02:00
|
|
|
use actix_net::service::NewServiceExt;
|
2018-10-08 16:55:01 +02:00
|
|
|
use actix_net::stream::TakeItem;
|
2018-10-05 23:30:40 +02:00
|
|
|
use actix_web::{test, ws as web_ws};
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::future::{ok, Either};
|
|
|
|
use futures::{Future, Sink, Stream};
|
|
|
|
|
2018-10-09 00:52:12 +02:00
|
|
|
use actix_http::{h1, ws, ResponseError, ServiceConfig};
|
2018-10-05 23:30:40 +02:00
|
|
|
|
2018-10-10 22:20:00 +02:00
|
|
|
fn ws_service(req: ws::Frame) -> impl Future<Item = ws::Message, Error = io::Error> {
|
2018-10-05 23:30:40 +02:00
|
|
|
match req {
|
2018-10-10 22:20:00 +02: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 23:30:40 +02:00
|
|
|
_ => ok(ws::Message::Close(None)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let addr = test::TestServer::unused_addr();
|
|
|
|
thread::spawn(move || {
|
|
|
|
Server::new()
|
|
|
|
.bind("test", addr, move || {
|
2018-10-09 00:52:12 +02:00
|
|
|
IntoFramed::new(|| h1::Codec::new(ServiceConfig::default()))
|
2018-10-08 16:55:01 +02:00
|
|
|
.and_then(TakeItem::new().map_err(|_| ()))
|
2018-10-06 00:39:35 +02:00
|
|
|
.and_then(|(req, framed): (_, Framed<_, _>)| {
|
|
|
|
// validate request
|
2018-10-14 08:57:31 +02:00
|
|
|
if let Some(h1::InMessage::Message(req, _)) = req {
|
2018-10-22 18:59:20 +02:00
|
|
|
match ws::verify_handshake(&req) {
|
2018-10-06 00:39:35 +02:00
|
|
|
Err(e) => {
|
|
|
|
// validation failed
|
|
|
|
let resp = e.error_response();
|
|
|
|
Either::A(
|
2018-10-05 23:30:40 +02:00
|
|
|
framed
|
2018-10-06 00:39:35 +02:00
|
|
|
.send(h1::OutMessage::Response(resp))
|
|
|
|
.map_err(|_| ())
|
|
|
|
.map(|_| ()),
|
|
|
|
)
|
2018-10-05 23:30:40 +02:00
|
|
|
}
|
2018-10-22 18:59:20 +02:00
|
|
|
Ok(_) => Either::B(
|
2018-10-06 00:39:35 +02:00
|
|
|
// send response
|
|
|
|
framed
|
2018-10-22 18:59:20 +02:00
|
|
|
.send(h1::OutMessage::Response(
|
|
|
|
ws::handshake_response(&req).finish(),
|
|
|
|
)).map_err(|_| ())
|
2018-10-06 00:39:35 +02:00
|
|
|
.and_then(|framed| {
|
|
|
|
// start websocket service
|
|
|
|
let framed =
|
|
|
|
framed.into_framed(ws::Codec::new());
|
|
|
|
ws::Transport::with(framed, ws_service)
|
|
|
|
.map_err(|_| ())
|
|
|
|
}),
|
|
|
|
),
|
2018-10-05 23:30:40 +02:00
|
|
|
}
|
2018-10-06 00:39:35 +02:00
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
})
|
2018-10-05 23:30:40 +02:00
|
|
|
}).unwrap()
|
|
|
|
.run();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut sys = System::new("test");
|
|
|
|
{
|
|
|
|
let (reader, mut writer) = sys
|
|
|
|
.block_on(web_ws::Client::new(format!("http://{}/", addr)).connect())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
writer.text("text");
|
|
|
|
let (item, reader) = sys.block_on(reader.into_future()).unwrap();
|
|
|
|
assert_eq!(item, Some(web_ws::Message::Text("text".to_owned())));
|
|
|
|
|
|
|
|
writer.binary(b"text".as_ref());
|
|
|
|
let (item, reader) = sys.block_on(reader.into_future()).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
item,
|
|
|
|
Some(web_ws::Message::Binary(Bytes::from_static(b"text").into()))
|
|
|
|
);
|
|
|
|
|
|
|
|
writer.ping("ping");
|
|
|
|
let (item, reader) = sys.block_on(reader.into_future()).unwrap();
|
|
|
|
assert_eq!(item, Some(web_ws::Message::Pong("ping".to_owned())));
|
|
|
|
|
|
|
|
writer.close(Some(web_ws::CloseCode::Normal.into()));
|
|
|
|
let (item, _) = sys.block_on(reader.into_future()).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
item,
|
|
|
|
Some(web_ws::Message::Close(Some(
|
|
|
|
web_ws::CloseCode::Normal.into()
|
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|